@@ -287,26 +287,38 @@ def get_python_long_version(path):
287287# =============================================================================
288288# Patch chebang line (courtesy of Christoph Gohlke)
289289# =============================================================================
290- def patch_shebang_line (fname , pad = b' ' ):
291- """Remove absolute path to python.exe in shebang lines."""
290+ def patch_shebang_line (fname , pad = b' ' , to_movable = True ):
291+ """Remove absolute path to python.exe in shebang lines, or re-add it"""
292+
293+ import re
294+ import sys
295+ target_dir = ""
296+ if to_movable == False :
297+ import os
298+ target_dir = os .path .abspath (os .path .dirname (fname ))
299+ target_dir = os .path .abspath (os .path .join (target_dir , r'..' )) + '\\ '
300+
301+ executable = sys .executable
292302 if sys .version_info [0 ] == 2 :
293- shebang_line = re .compile (r"(#!.+ pythonw?\.exe)" ) # Python2.7
303+ shebang_line = re .compile (r"(#!.* pythonw?\.exe)" ) # Python2.7
294304 else :
295- shebang_line = re .compile (b"(#!.+ pythonw?\.exe)" ) # Python3+
296-
305+ shebang_line = re .compile (b"(#!.* pythonw?\.exe)" ) # Python3+
306+ target_dir = target_dir . encode ( 'utf-8' )
297307 with open (fname , 'rb' ) as fh :
298- content = fh .read ()
308+ initial_content = fh .read ()
299309
300- content = shebang_line .split (content , maxsplit = 1 )
310+ content = shebang_line .split (initial_content , maxsplit = 1 )
301311 if len (content ) != 3 :
302312 return
303- exe = os .path .basename (content [1 ][2 :])
304- content [1 ] = b'#!' + exe + (pad * (len (content [1 ]) - len (exe ) - 2 ))
305- content = b'' .join (content )
306313
314+ exe = os .path .basename (content [1 ][2 :])
315+ content [1 ] = b'#!' + target_dir + exe + (pad * (len (content [1 ]) - len (exe ) - 2 ))
316+ final_content = b'' .join (content )
317+ if initial_content == final_content :
318+ return
307319 try :
308320 with open (fname , 'wb' ) as fh :
309- fh .write (content )
321+ fh .write (final_content )
310322 print ("patched" , fname )
311323 except Exception :
312324 print ("failed to patch" , fname )
0 commit comments