From 806dfd4d1a98a28769fa3ae9ad11a82107d42505 Mon Sep 17 00:00:00 2001 From: stonebig Date: Wed, 17 Feb 2016 23:46:20 +0100 Subject: [PATCH] shebang "..\python.exe" vs "python.exe" --- make.py | 14 +++++++------- winpython/utils.py | 15 +++++++++------ winpython/wppm.py | 26 ++++++++++++++++++++------ 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/make.py b/make.py index addb035f..f5ff6dbe 100644 --- a/make.py +++ b/make.py @@ -515,8 +515,10 @@ def _create_launchers(self): """Create launchers""" self._print("Creating launchers") self.create_launcher('WinPython Command Prompt.exe', 'cmd.ico', - command='$SYSDIR\cmd.exe', - args='/k', workdir='${WINPYDIR}') + command=r'${WINPYDIR}\Scripts\ptpython.exe', + args='/k', workdir=r'${WINPYDIR}\Scripts') + # command='$SYSDIR\cmd.exe', + # args='/k', workdir='${WINPYDIR}') self.create_launcher('WinPython Interpreter.exe', 'python.ico') #self.create_launcher('IDLE (Python GUI).exe', 'python.ico', # args='idle.pyw', @@ -688,19 +690,17 @@ def _create_batch_scripts(self): self.create_batch_script('make_winpython_movable.bat',r"""@echo off call %~dp0env.bat -echo patch pip and current launchers fopr move -rem %WINPYDIR%\python.exe -c "from winpython.utils import patch_sourcefile;patch_sourcefile(r'%WINPYDIR%\Lib\site-packages\pip\_vendor\distlib\scripts.py', 'executable = get_executable()', 'executable = os.path.join(os.path.basename(get_executable()))' )" +echo patch pip and current launchers for move -%WINPYDIR%\python.exe -c "from winpython import wppm;dist=wppm.Distribution(r'%WINPYDIR%');dist.patch_standard_packages('pip');dist.patch_all_shebang(to_movable=True)" +%WINPYDIR%\python.exe -c "from winpython import wppm;dist=wppm.Distribution(r'%WINPYDIR%');dist.patch_standard_packages('pip', to_movable=True);dist.patch_all_shebang(to_movable=True)" pause """) self.create_batch_script('make_winpython_fix.bat',r"""@echo off call %~dp0env.bat echo patch pip and current launchers for non-move -%WINPYDIR%\python.exe -c "from winpython.utils import patch_sourcefile;patch_sourcefile(r'%WINPYDIR%\Lib\site-packages\pip\_vendor\distlib\scripts.py', 'executable = os.path.join(os.path.basename(get_executable()))', 'executable = get_executable()' )" -%WINPYDIR%\python.exe -c "from winpython import wppm;dist=wppm.Distribution(r'%WINPYDIR%');dist.patch_all_shebang(to_movable=False)" +%WINPYDIR%\python.exe -c "from winpython import wppm;dist=wppm.Distribution(r'%WINPYDIR%');dist.patch_standard_packages('pip', to_movable=False);dist.patch_all_shebang(to_movable=False)" pause """) diff --git a/winpython/utils.py b/winpython/utils.py index cebc6aa5..71c50a39 100644 --- a/winpython/utils.py +++ b/winpython/utils.py @@ -290,13 +290,13 @@ def get_python_long_version(path): # ============================================================================= # Patch chebang line (courtesy of Christoph Gohlke) # ============================================================================= -def patch_shebang_line(fname, pad=b' ', to_movable=True): +def patch_shebang_line(fname, pad=b' ', to_movable=True, targetdir="..\\"): """Remove absolute path to python.exe in shebang lines, or re-add it""" import re import sys import os - target_dir = "" + target_dir = targetdir # movable option if to_movable == False: target_dir = os.path.abspath(os.path.dirname(fname)) target_dir = os.path.abspath(os.path.join(target_dir, r'..')) + '\\' @@ -309,19 +309,22 @@ def patch_shebang_line(fname, pad=b' ', to_movable=True): target_dir = target_dir.encode('utf-8') with open(fname, 'rb') as fh: initial_content = fh.read() - + fh.close + fh = None content = shebang_line.split(initial_content, maxsplit=1) if len(content) != 3: return exe = os.path.basename(content[1][2:]) - content[1] = b'#!' + target_dir + exe + (pad * (len(content[1]) - len(exe) - 2)) + content[1] = b'#!' + target_dir + exe #+ (pad * (len(content[1]) - len(exe) - 2)) final_content = b''.join(content) if initial_content == final_content: return try: - with open(fname, 'wb') as fh: - fh.write(final_content) + with open(fname, 'wb') as fo: + fo.write(final_content) + fo.close + fo = None print("patched", fname) except Exception: print("failed to patch", fname) diff --git a/winpython/wppm.py b/winpython/wppm.py index d3266750..dd86ce27 100644 --- a/winpython/wppm.py +++ b/winpython/wppm.py @@ -442,7 +442,7 @@ def do_pip_action(self, actions=None, install_options=None): print("Failed!") raise - def patch_standard_packages(self, package_name=''): + def patch_standard_packages(self, package_name='', to_movable=True): """patch Winpython packages in need""" import filecmp # 'pywin32' minimal post-install (pywin32_postinstall.py do too much) @@ -459,11 +459,25 @@ def patch_standard_packages(self, package_name=''): # rational: https://github.com/pypa/pip/issues/2328 if package_name.lower() == "pip" or package_name == '': # ensure pip will create movable launchers - utils.patch_sourcefile( - self.target + ( - r"\Lib\site-packages\pip\_vendor\distlib\scripts.py"), - " executable = get_executable()", - " executable = os.path.join(os.path.basename(get_executable()))") + # sheb_mov1 = old way up to WinPython 2016-01, sheb_mov2 = new way + sheb_fix = " executable = get_executable()" + sheb_mov1 = " executable = os.path.join(os.path.basename(get_executable()))" + sheb_mov2 = " executable = os.path.join('..',os.path.basename(get_executable()))" + if to_movable: + utils.patch_sourcefile(self.target + + r"\Lib\site-packages\pip\_vendor\distlib\scripts.py", + sheb_fix, sheb_mov2) + utils.patch_sourcefile(self.target + + r"\Lib\site-packages\pip\_vendor\distlib\scripts.py", + sheb_mov1, sheb_mov2) + else: + utils.patch_sourcefile(self.target + + r"\Lib\site-packages\pip\_vendor\distlib\scripts.py", + sheb_mov1, sheb_fix) + utils.patch_sourcefile(self.target + + r"\Lib\site-packages\pip\_vendor\distlib\scripts.py", + sheb_mov2, sheb_fix) + # ensure pip wheel will register relative PATH in 'RECORD' files utils.patch_sourcefile( self.target + (