25 from .
import run_command
27 PYTHON_INTERPRETERS = [
"python",
"pypy",
"ipy",
"jython"]
28 _PYTHON_INTERPRETERS_REGEX =
r"\(" +
r"\|".join(PYTHON_INTERPRETERS) +
r"\)" 32 """Find list of files containing python shebangs in the bin directory. """ 38 r"^#!.*bin/\(env \)\?{0}".format(_PYTHON_INTERPRETERS_REGEX),
40 r"^'''exec.*bin/{0}".format(_PYTHON_INTERPRETERS_REGEX),
41 os.path.join(venv_dir,
"bin"),
43 files =
run_command(command, check=
True, capture_output=
True).stdout
44 return {f
for f
in files.decode(
"utf-8").strip().split(
"\n")
if f}
48 """Translate /usr/bin/python and /usr/bin/env python shebang 49 lines to point to our virtualenv python. 51 pythonpath = os.path.join(target_dir,
"bin/python")
54 r"s-^#!.*bin/\(env \)\?{names}\"\?-#!{pythonpath}-;" r"s-^'''exec'.*bin/{names}-'''exec' {pythonpath}-" 55 ).format(names=_PYTHON_INTERPRETERS_REGEX, pythonpath=re.escape(pythonpath))
60 """Replace the `VIRTUAL_ENV` path in bin/activate to reflect the 61 post-install path of the virtualenv. 64 [
'VIRTUAL_ENV="{0}"'.format(target_dir),
r"^VIRTUAL_ENV=.*$",
"activate"],
65 [
'setenv VIRTUAL_ENV "{0}"'.format(target_dir),
r"^setenv VIRTUAL_ENV.*$",
"activate.csh"],
66 [
'set -gx VIRTUAL_ENV "{0}"'.format(target_dir),
r"^set -gx VIRTUAL_ENV.*$",
"activate.fish"],
69 for activate_args
in activate_settings:
70 virtualenv_path = activate_args[0]
71 pattern = re.compile(activate_args[1], flags=re.M)
72 activate_file = activate_args[2]
74 with open(os.path.join(venv_dir,
"bin", activate_file),
"r+")
as fh:
75 content = pattern.sub(virtualenv_path, fh.read())
86 local_dir = os.path.join(venv_dir,
"local")
87 if not os.path.isdir(local_dir):
89 elif os.path.samefile(venv_dir, local_dir):
92 os.symlink(
".", local_dir)
95 for d
in os.listdir(local_dir):
96 path = os.path.join(local_dir, d)
97 if not os.path.islink(path):
100 existing_target = os.readlink(path)
101 if not os.path.isabs(existing_target):
106 new_target = os.path.relpath(existing_target, local_dir)
108 os.symlink(new_target, path)
def find_script_files(venv_dir)
def fix_activate_path(venv_dir, target_dir)
def fix_local_symlinks(venv_dir)
def run_command(cmd, args, kwargs)
def fix_shebangs(venv_dir, target_dir)