Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sign the python2 exe on Darwin arm64 #2233

Merged
merged 4 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ Bugfixes - 20.0.8
- Having `distutils configuration <https://docs.python.org/3/install/index.html#distutils-configuration-files>`_
files that set ``prefix`` and ``install_scripts`` cause installation of packages in the wrong location -
by :user:`gaborbernat`. (`#1663 <https://github.com/pypa/virtualenv/issues/1663>`_)
- Fix ``PYTHONPATH`` being overridden on Python 2 — by :user:`jd`. (`#1673 <https://github.com/pypa/virtualenv/issues/1673>`_)
- Fix ``PYTHONPATH`` being overridden on Python 2 — by :user:`jd`. (`#1673 <https://github.com/pypa/virtualenv/issues/1673>`_)
- Fix list configuration value parsing from config file or environment variable - by :user:`gaborbernat`. (`#1674 <https://github.com/pypa/virtualenv/issues/1674>`_)
- Fix Batch activation script shell prompt to display environment name by default - by :user:`spetafree`. (`#1679 <https://github.com/pypa/virtualenv/issues/1679>`_)
- Fix startup on Python 2 is slower for virtualenv - this was due to setuptools calculating it's working set distribution
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ virtualenv.create =
cpython3-win = virtualenv.create.via_global_ref.builtin.cpython.cpython3:CPython3Windows
cpython2-posix = virtualenv.create.via_global_ref.builtin.cpython.cpython2:CPython2Posix
cpython2-mac-framework = virtualenv.create.via_global_ref.builtin.cpython.mac_os:CPython2macOsFramework
cpython2-mac-arm-framework = virtualenv.create.via_global_ref.builtin.cpython.mac_os:CPython2macOsArmFramework
cpython3-mac-framework = virtualenv.create.via_global_ref.builtin.cpython.mac_os:CPython3macOsFramework
cpython2-win = virtualenv.create.via_global_ref.builtin.cpython.cpython2:CPython2Windows
pypy2-posix = virtualenv.create.via_global_ref.builtin.pypy.pypy2:PyPy2Posix
Expand Down
38 changes: 34 additions & 4 deletions src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -103,12 +107,38 @@ def reload_code(self):
)
return result


class CPython2macOsArmFramework(CPython2macOsFramework, CPythonmacOsFramework, CPython2PosixBase):
@classmethod
def can_create(cls, interpreter):
if IS_MAC_ARM64:
return False
else:
return super(CPythonmacOsFramework, cls).can_create(interpreter)
return IS_MAC_ARM64 and super(CPythonmacOsFramework, cls).can_describe(interpreter)

def create(self):
super(CPython2macOsFramework, self).create()
self.fix_signature()

def fix_signature(self):
"""
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 creation process.
"""
exe = self.exe
try:
logging.debug("Changing signature of copied python exe %s", exe)
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", 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


class CPython3macOsFramework(CPythonmacOsFramework, CPython3, CPythonPosix):
Expand Down