Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions virtualenv_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'activate.csh',
'activate.fish'
]
_pybin_match = re.compile(r'^python\d+\.\d+$')
_pybin_match = re.compile(r'^lib[/-]python/?\d+\.\d+$')
_activation_path_re = re.compile(r'^(?:set -gx |setenv |)VIRTUAL_ENV[ =]"(.*?)"\s*$')


Expand Down Expand Up @@ -52,8 +52,10 @@ def _handle_sub(match):
f.writelines(lines)


def update_script(script_filename, new_path):
def update_script(script_filename, bin_dir, new_path):
"""Updates shebang lines for actual scripts."""
if not os.path.isfile(script_filename):
return
with open(script_filename) as f:
lines = list(f)
if not lines:
Expand All @@ -65,11 +67,15 @@ def update_script(script_filename, new_path):
if not args:
return

if not args[0].endswith('/bin/python') or \
'/usr/bin/env python' in args[0]:
if args[0].endswith("bin/env"):
bin_file = args[1]
else:
_, bin_file = os.path.split(args[0])

if not os.path.exists(os.path.join(bin_dir, bin_file)):
return

new_bin = os.path.join(new_path, 'bin', 'python')
new_bin = os.path.join(new_path, 'bin', bin_file)
if new_bin == args[0]:
return

Expand All @@ -86,7 +92,7 @@ def update_scripts(bin_dir, new_path):
if fn in ACTIVATION_SCRIPTS:
update_activation_script(os.path.join(bin_dir, fn), new_path)
else:
update_script(os.path.join(bin_dir, fn), new_path)
update_script(os.path.join(bin_dir, fn), bin_dir, new_path)


def update_pyc(filename, new_path):
Expand Down Expand Up @@ -167,19 +173,23 @@ def update_paths(base, new_path):
return False

bin_dir = os.path.join(base, 'bin')
base_lib_dir = os.path.join(base, 'lib')
lib_dir = None
lib_name = None

if os.path.isdir(base_lib_dir):
for folder in os.listdir(base_lib_dir):
if _pybin_match.match(folder):
lib_name = folder
lib_dir = os.path.join(base_lib_dir, folder)
break
for base_lib_dir in os.listdir(base):
if base_lib_dir.startswith("lib"):
subdir = os.path.join(base, base_lib_dir)
if os.path.isdir(subdir):
for folder in os.listdir(subdir):
if _pybin_match.match("/".join((base_lib_dir, folder))):
lib_name = folder
lib_dir = os.path.join(subdir, folder)
break
if lib_dir:
break

if lib_dir is None or not os.path.isdir(bin_dir) \
or not os.path.isfile(os.path.join(bin_dir, 'python')):
or not os.path.exists(os.path.join(bin_dir, 'python')):
print 'error: %s does not refer to a python installation' % base
return False

Expand Down