Skip to content

Commit 99ab450

Browse files
committed
Codesign the runtime binary in Darwin
1 parent 659c84e commit 99ab450

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/protect_code.pt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def protect_pytransform():
3333

3434
def check_lib_pytransform():
3535
platname = pytransform.sys.platform
36+
if platname.startswith('darwin'):
37+
return
3638
libname = '_pytransform{suffix}.dylib' if platname.startswith('darwin') else \
3739
'_pytransform{suffix}.dll' if platname.startswith('win') else \
3840
'_pytransform{suffix}.dll' if platname.startswith('cygwin') else \

src/protect_code2.pt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def protect_pytransform():
1717
raise RuntimeError('unexpected obfuscated script')
1818

1919
def check_lib_pytransform():
20+
if sys.platform == 'darwin':
21+
return
2022
{relative}import pytransform{suffix} as pytransform
2123
filename = pytransform.__file__
2224
with open(filename, 'rb') as f:

src/utils.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from codecs import BOM_UTF8
3535
from glob import glob
3636
from json import dumps as json_dumps, loads as json_loads
37-
from subprocess import Popen, check_output
37+
from subprocess import PIPE, Popen, check_output
3838
from time import gmtime, strftime
3939
from zipfile import ZipFile
4040

@@ -607,8 +607,6 @@ def copy3(src, dst, onlycopy=False):
607607

608608
logging.info('Patch library %s', target)
609609
data = _patch_extension(target, keylist, suffix, supermode=False)
610-
with open(target, 'wb') as f:
611-
f.write(data)
612610
checklist.append(sum(bytearray(data)))
613611

614612
if not platforms:
@@ -1456,8 +1454,6 @@ def write_integer(data, offset, value):
14561454
data[offset:offset+size] = keylist[j]
14571455
offset += size
14581456

1459-
logging.info('Patch library file OK')
1460-
14611457
if suffix:
14621458
marker = bytes(b'_vax_000000')
14631459
k = len(marker)
@@ -1471,6 +1467,12 @@ def write_integer(data, offset, value):
14711467
raise RuntimeError('Failed to add symbol suffix for library %s'
14721468
% filename)
14731469

1470+
with open(filename, 'wb') as f:
1471+
f.write(data)
1472+
1473+
sign_binary(filename)
1474+
1475+
logging.info('Patch library file OK')
14741476
return data
14751477

14761478

@@ -1537,9 +1539,6 @@ def _make_super_runtime(capsule, output, platforms, licfile=None, suffix=''):
15371539

15381540
logging.info('Patch extension %s', target)
15391541
data = _patch_extension(target, keylist, suffix)
1540-
1541-
with open(target, 'wb') as f:
1542-
f.write(data)
15431542
checklist.append(sum(bytearray(data)))
15441543

15451544
logging.info('Generate runtime files OK')
@@ -1570,9 +1569,6 @@ def _package_super_runtime(output, platforms, filelist, keylist, suffix):
15701569

15711570
logging.info('Patch extension %s', target)
15721571
data = _patch_extension(target, keylist, suffix)
1573-
1574-
with open(target, 'wb') as f:
1575-
f.write(data)
15761572
checklist.append(sum(bytearray(data)))
15771573

15781574
logging.info('Generate super runtime package OK')
@@ -1782,3 +1778,26 @@ def get_sppmode_files(timeout=None):
17821778
logging.info('Download sppmode library "%s" OK', platid)
17831779

17841780
return libname, licfile
1781+
1782+
1783+
def sign_binary(filename):
1784+
if not sys.platform.startswith('darwin'):
1785+
return
1786+
1787+
# Maybe cross platform
1788+
output = check_output(['file', filename])
1789+
if output.find(b' Mach-O ') == -1:
1790+
return
1791+
1792+
logging.info("Signing file %s", filename)
1793+
identity = '-'
1794+
cmdlist = ['codesign', '-s', identity, '--force', '--all-architectures',
1795+
'--timestamp', filename]
1796+
p = Popen(cmdlist, stdout=PIPE, stderr=PIPE)
1797+
stdout, stderr = p.communicate()
1798+
if p.returncode != 0:
1799+
logging.warning("codesign command (%r) failed with error code %d!\n"
1800+
"stdout: %r\n"
1801+
"stderr: %r",
1802+
cmdlist, p.returncode, stdout, stderr)
1803+
raise SystemError("codesign failure!")

0 commit comments

Comments
 (0)