Skip to content

Commit

Permalink
Merge pull request #26 from gocept/flake8-appenv.py
Browse files Browse the repository at this point in the history
Fix appenv.py to make flake8 happy.
  • Loading branch information
ctheune authored Jul 18, 2021
2 parents 390110a + 6661f93 commit ec75f51
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/appenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import http.client
import os
import os.path
import shlex
import shutil
import subprocess
import sys
Expand All @@ -30,12 +29,12 @@

def cmd(c, merge_stderr=True, quiet=False):
# TODO revisit the cmd() architecture w/ python 3
# XXX better IO management for interactive output and seeing original errors
# and output at appropriate places ...
# XXX better IO management for interactive output and seeing original
# errors and output at appropriate places ...
try:
kwargs = {"shell": True}
if merge_stderr:
kwargs["stderr"] = stderr = subprocess.STDOUT
kwargs["stderr"] = subprocess.STDOUT
return subprocess.check_output([c], **kwargs)
except subprocess.CalledProcessError as e:
print("{} returned with exit code {}".format(c, e.returncode))
Expand Down Expand Up @@ -74,8 +73,8 @@ def ensure_venv(target):
# This is trying to detect whether we're on a proper Python stdlib
# or on a broken Debian. See various StackOverflow questions about
# this.
import distutils.util
import ensurepip
import distutils.util # noqa: F401 imported but unused
import ensurepip # noqa: F401 imported but unused
except ImportError:
# Okay, lets repair this, if we can. May need privilege escalation
# at some point.
Expand Down Expand Up @@ -107,8 +106,8 @@ def ensure_venv(target):
"python{}.{}".format(*sys.version_info[:2]),
"site-packages", module))

# (always) prepend the site packages so we can actually have a fixed
# distutils installation.
# (always) prepend the site packages so we can actually have a
# fixed distutils installation.
site_packages = os.path.abspath(
os.path.join(target, "lib", "python" + python_maj_min,
"site-packages"))
Expand Down Expand Up @@ -287,8 +286,8 @@ def run(self, command, argv):
os.execv(cmd, argv)

def _prepare(self):
# copy used requirements.txt into the target directory so we can use that
# to check later
# copy used requirements.txt into the target directory so we can use
# that to check later
# - when to clean up old versions? keep like one or two old revisions?
# - enumerate the revisions and just copy the requirements.txt, check
# for ones that are clean or rebuild if necessary
Expand All @@ -298,8 +297,8 @@ def _prepare(self):
env_dir = os.path.join(self.appenv_dir, 'unclean')
ensure_venv(env_dir)
print('Ensuring unclean install ...')
cmd('{env_dir}/bin/python -m pip install -r requirements.txt --upgrade'
.format(env_dir=env_dir))
cmd('{env_dir}/bin/python -m pip install -r requirements.txt'
' --upgrade'.format(env_dir=env_dir))
else:
hash_content = []
requirements = open("requirements.lock", "rb").read()
Expand All @@ -314,18 +313,18 @@ def _prepare(self):
env_dir, os.path.join(self.appenv_dir, "unclean")])
for path in glob.glob(
"{appenv_dir}/*".format(appenv_dir=self.appenv_dir)):
if not path in whitelist:
if path not in whitelist:
print(
"Removing expired path: {path} ...".format(path=path))
if not os.path.isdir(path):
os.unlink(path)
else:
shutil.rmtree(path)
if os.path.exists(env_dir):
# check whether the existing environment is OK, it might be nice
# to rebuild in a separate place if necessary to avoid interruptions
# to running services, but that isn't what we're using it for at the
# moment
# check whether the existing environment is OK, it might be
# nice to rebuild in a separate place if necessary to avoid
# interruptions to running services, but that isn't what we're
# using it for at the moment
try:
if not os.path.exists(
"{env_dir}/appenv.ready".format(env_dir=env_dir)):
Expand All @@ -342,8 +341,8 @@ def _prepare(self):
f.write(requirements)

print("Installing ...")
cmd("{env_dir}/bin/python -m pip install --no-deps -r {env_dir}/requirements.lock"
.format(env_dir=env_dir))
cmd("{env_dir}/bin/python -m pip install --no-deps -r"
" {env_dir}/requirements.lock".format(env_dir=env_dir))

cmd("{env_dir}/bin/python -m pip check".format(
env_dir=env_dir))
Expand Down Expand Up @@ -389,7 +388,8 @@ def init(self, args=None, remaining=None):
requirements_txt.write(dependency + "\n")
print()
print(
"Done. You can now `cd {}` and call `./{}` to bootstrap and run it."
"Done. You can now `cd {}` and call"
" `./{}` to bootstrap and run it."
.format(os.path.relpath(target, self.original_cwd), command))

def python(self, args, remaining):
Expand Down Expand Up @@ -418,7 +418,8 @@ def update_lockfile(self, args=None, remaining=None):

# Hack because we might not have pkg_resources, but the venv should
tmp_paths = cmd(
"{tmpdir}/bin/python -c 'import sys; print(\"\\n\".join(sys.path))'"
"{tmpdir}/bin/python -c"
" 'import sys; print(\"\\n\".join(sys.path))'"
.format(tmpdir=tmpdir),
merge_stderr=False).decode(sys.getfilesystemencoding())
for line in tmp_paths.splitlines():
Expand Down Expand Up @@ -474,8 +475,8 @@ def update_lockfile(self, args=None, remaining=None):
def main():
ensure_best_python()
# clear PYTHONPATH variable to get a defined environment
# XXX this is a bit of history. not sure whether its still needed. keeping it
# for good measure
# XXX this is a bit of history. not sure whether its still needed. keeping
# it for good measure
if "PYTHONPATH" in os.environ:
del os.environ["PYTHONPATH"]

Expand Down

0 comments on commit ec75f51

Please sign in to comment.