From f37d735ca1feef8ad462a1355289c12bb2dba006 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Wed, 13 Jun 2018 22:00:46 +0200 Subject: [PATCH] Fix for weird shebangs in recent pip versions See https://github.com/pypa/pip/blob/52749614ea527c684a2682df532501298b91406c/src/pip/_vendor/distlib/scripts.py#L139-L149 --- virtualenv_tools.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/virtualenv_tools.py b/virtualenv_tools.py index f5671c1..1a37209 100644 --- a/virtualenv_tools.py +++ b/virtualenv_tools.py @@ -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):