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
37 changes: 29 additions & 8 deletions virtualenv_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,44 @@ def update_script(script_filename, new_path):

if not lines[0].startswith('#!'):
return
args = lines[0][2:].strip().split()

if lines[0].startswith('#!/bin/sh') and len(lines) > 1 and \
lines[1].startswith("'''exec' "):
# Deal with a special form of contrived shebang as generated by pip in
# the case of too long shebangs (i.e. when a normal shebang would
# exceed the char limit for Linux or Darwin).
args = lines[1][9:].strip().split()
if not fix_args(args, new_path):
return

lines[1] = "'''exec' %s\n" % ' '.join(args)
else:
args = lines[0][2:].strip().split()
if not fix_args(args, new_path):
return

lines[0] = '#!%s\n' % ' '.join(args)

print 'S %s' % script_filename
with open(script_filename, 'w') as f:
f.writelines(lines)


def fix_args(args, new_path):
"""update an argv as found in a shebang"""
if not args:
return
return False

if not args[0].endswith('/bin/python') or \
'/usr/bin/env python' in args[0]:
return
return False

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

args[0] = new_bin
lines[0] = '#!%s\n' % ' '.join(args)
print 'S %s' % script_filename
with open(script_filename, 'w') as f:
f.writelines(lines)
return True


def update_scripts(bin_dir, new_path):
Expand Down