Skip to content

Commit

Permalink
Review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
tmspicer authored and gaborbernat committed Dec 28, 2021
1 parent 8074a00 commit 5f16bc0
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,33 @@ 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")
fix_signature(python_exe)
self.fix_signature(python_exe)

def fix_signature(self, exe):
"""
On Apple M1 machines (arm64 chips), rewriting the python executable invalidates it's 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.
"""
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)
subprocess.check_call(["cp", exe, bak_dir])
subprocess.check_call(["mv", bk_python, exe])
os.rmdir(bak_dir)

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 Expand Up @@ -181,33 +207,6 @@ def fix_mach_o(exe, current, new, max_size):
raise


def fix_signature(exe):
"""
On Apple M1 machines (arm64 chips), rewriting the python executable invalidates it's 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.
"""
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)
subprocess.check_call(["cp", exe, bak_dir])
subprocess.check_call(["mv", bk_python, exe])
os.rmdir(bak_dir)

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


def _builtin_change_mach_o(maxint):
MH_MAGIC = 0xFEEDFACE
MH_CIGAM = 0xCEFAEDFE
Expand Down

0 comments on commit 5f16bc0

Please sign in to comment.