From 04000b61a0cd3e89d13e740d590600c55ce86e0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Fri, 3 Dec 2021 18:56:37 +0000 Subject: [PATCH] PR Feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bernát Gábor --- docs/changelog.rst | 2 +- .../via_global_ref/builtin/cpython/mac_os.py | 33 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 893b52c74..5668e88b0 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -630,7 +630,7 @@ Bugfixes - 20.0.8 - Having `distutils configuration `_ files that set ``prefix`` and ``install_scripts`` cause installation of packages in the wrong location - by :user:`gaborbernat`. (`#1663 `_) -- Fix ``PYTHONPATH`` being overridden on Python 2 — by :user:`jd`. (`#1673 `_) +- Fix ``PYTHONPATH`` being overridden on Python 2 — by :user:`jd`. (`#1673 `_) - Fix list configuration value parsing from config file or environment variable - by :user:`gaborbernat`. (`#1674 `_) - Fix Batch activation script shell prompt to display environment name by default - by :user:`spetafree`. (`#1679 `_) - Fix startup on Python 2 is slower for virtualenv - this was due to setuptools calculating it's working set distribution diff --git a/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py b/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py index 117f16adb..4f7f646c5 100644 --- a/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py +++ b/src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py @@ -68,6 +68,10 @@ def image_ref(cls, interpreter): class CPython2macOsFramework(CPythonmacOsFramework, CPython2PosixBase): + @classmethod + def can_create(cls, interpreter): + return not IS_MAC_ARM64 and super(CPython2macOsFramework, cls).can_describe(interpreter) + @classmethod def image_ref(cls, interpreter): return Path(interpreter.prefix) / "Python" @@ -106,37 +110,32 @@ def reload_code(self): class CPython2macOsArmFramework(CPython2macOsFramework, CPythonmacOsFramework, CPython2PosixBase): @classmethod - def can_describe(cls, interpreter): + def can_create(cls, interpreter): return IS_MAC_ARM64 and super(CPythonmacOsFramework, cls).can_describe(interpreter) def create(self): super(CPython2macOsFramework, self).create() - # re-sign the newly copied/modified python binary - python_exe = os.path.join(str(self.dest), "bin", "python") - self.fix_signature(python_exe) + self.fix_signature() - def fix_signature(self, exe): + def fix_signature(self): """ - On Apple M1 machines (arm64 chips), rewriting the python executable invalidates it's signature. + On Apple M1 machines (arm64 chips), rewriting the python executable invalidates its signature. In python2 this results in a unusable python exe which just dies. - As a temporary workaround we can codesign the python exe during the createion process. + As a temporary workaround we can codesign the python exe during the creation process. """ + exe = self.exe try: logging.debug("Changing signature of copied python exe %s", exe) - bak_dir = os.path.join(os.path.dirname(exe), "bk") - bk_python = os.path.join(bak_dir, os.path.basename(exe)) - - # hack to reset the signing on Darwin since the exe has been modified. - # codesign fails on the original exe, it needs to be copied and moved back. - os.makedirs(bak_dir) + bak_dir = exe.parent / "bk" + # Reset the signing on Darwin since the exe has been modified. + # Note codesign fails on the original exe, it needs to be copied and moved back. + bak_dir.mkdir(parents=True, exist_ok=True) subprocess.check_call(["cp", exe, bak_dir]) - subprocess.check_call(["mv", bk_python, exe]) - os.rmdir(bak_dir) - + subprocess.check_call(["mv", bak_dir / exe.name, exe]) + bak_dir.unlink() cmd = ["codesign", "-s", "-", "--preserve-metadata=identifier,entitlements,flags,runtime", "-f", exe] logging.debug("Changing Signature: %s", cmd) subprocess.check_call(cmd) - except Exception: logging.fatal("Could not change MacOS code signing on copied python exe at %s", exe) raise