X Tutup
Skip to content

Commit fe432f2

Browse files
committed
Add suffix for runtime package and builtin functions
1 parent 3b6d605 commit fe432f2

File tree

6 files changed

+109
-46
lines changed

6 files changed

+109
-46
lines changed

src/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from sys import platform
22

3-
version = '5.8.6'
3+
version = '5.8.7'
44

55
# The corresponding version of _pytransform.so
6-
core_version = 'r11.5'
6+
core_version = 'r12.6'
77

88
version_info = '''
99
PyArmor is a command line tool used to obfuscate python scripts, bind
@@ -25,7 +25,7 @@
2525
else '.dll' if platform in ('win32', 'cygwin') else '.so'
2626

2727

28-
entry_lines = 'from %spytransform import pyarmor_runtime\n', \
28+
entry_lines = 'from %spytransform%s import pyarmor_runtime\n', \
2929
'pyarmor_runtime(%s)\n'
3030
protect_code_template = 'protect_code.pt'
3131

src/project.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# 1.2.1: Add advanced_mode
3333
# 1.2.2: Remove disable_restrice_mode, add restrict_mode
3434
# 1.2.3: Add package_runtime
35+
# 1.2.4: Add enable_suffix
3536
#
3637
import os
3738
import time
@@ -75,6 +76,7 @@ class Project(dict):
7576
('plugins', None), \
7677
('platform', None), \
7778
('package_runtime', 1), \
79+
('enable_suffix', 0), \
7880
('build_time', 0.)
7981

8082
def __init__(self, *args, **kwargs):

src/protect_code.pt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def protect_pytransform():
1313

1414
def check_obfuscated_script():
1515
CO_SIZES = 49, 46, 38, 36
16-
CO_NAMES = set(['pytransform', 'pyarmor_runtime', '__pyarmor__',
17-
'__name__', '__file__'])
16+
CO_NAMES = set(['pytransform{suffix}', 'pyarmor_runtime',
17+
'__pyarmor{suffix}__', '__name__', '__file__'])
1818
co = pytransform.sys._getframe(3).f_code
1919
if not ((set(co.co_names) <= CO_NAMES)
2020
and (len(co.co_code) in CO_SIZES)):
@@ -36,9 +36,9 @@ def protect_pytransform():
3636

3737
def check_lib_pytransform():
3838
platname = pytransform.sys.platform
39-
libname = '_pytransform.dylib' if platname.startswith('darwin') else \
40-
'_pytransform.dll' if platname.startswith('win') else \
41-
'_pytransform.so'
39+
libname = '_pytransform{suffix}.dylib' if platname.startswith('darwin') else \
40+
'_pytransform{suffix}.dll' if platname.startswith('win') else \
41+
'_pytransform{suffix}.so'
4242
if getattr(pytransform.sys, 'frozen', False):
4343
filename = pytransform.os.path.join(
4444
pytransform.sys._MEIPASS, libname)

src/pyarmor.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
pytransform_bootstrap, encrypt_script, search_plugins, \
5151
get_product_key, register_keyfile, query_keyinfo, \
5252
get_platform_list, download_pytransform, update_pytransform,\
53-
check_cross_platform, compatible_platform_names
53+
check_cross_platform, compatible_platform_names, \
54+
get_name_suffix
5455

5556
import packer
5657

@@ -181,6 +182,8 @@ def _build(args):
181182
capsule = build_path(project.capsule, pro_path)
182183
logging.info('Use capsule: %s', capsule)
183184

185+
suffix = get_name_suffix() if args.enable_suffix else ''
186+
184187
output = build_path(project.output, pro_path) \
185188
if args.output is None else os.path.normpath(args.output)
186189
logging.info('Output path is: %s', output)
@@ -271,7 +274,7 @@ def v(t):
271274
wrap_mode=wrap_mode, adv_mode=vmode,
272275
rest_mode=restrict, protection=pcode,
273276
platforms=platforms, plugins=plugins,
274-
rpath=project.runtime_path)
277+
rpath=project.runtime_path, suffix=suffix)
275278

276279
logging.info('%d scripts has been obfuscated', len(files))
277280
project['build_time'] = time.time()
@@ -285,7 +288,8 @@ def v(t):
285288
relative = True if x == 3 else \
286289
False if (x == 2 or args.no_runtime) else None
287290
make_entry(project.entry, project.src, soutput,
288-
rpath=project.runtime_path, relative=relative)
291+
rpath=project.runtime_path, relative=relative,
292+
suffix=suffix)
289293

290294
if not args.no_runtime:
291295
routput = output if args.output is not None and args.only_runtime \
@@ -297,7 +301,8 @@ def v(t):
297301

298302
package = project.get('package_runtime', 0) \
299303
if args.package_runtime is None else args.package_runtime
300-
make_runtime(capsule, routput, platforms=platforms, package=package)
304+
make_runtime(capsule, routput, platforms=platforms, package=package,
305+
suffix=suffix)
301306

302307
if not restrict:
303308
licode = '*FLAGS:%c*CODE:PyArmor-Project' % chr(1)
@@ -465,6 +470,8 @@ def _obfuscate(args):
465470
if os.path.abspath(output) == path:
466471
raise RuntimeError('Output path can not be same as src')
467472

473+
suffix = get_name_suffix() if args.enable_suffix else ''
474+
468475
if args.recursive:
469476
logging.info('Recursive mode is on')
470477
pats = ['global-include *.py']
@@ -531,25 +538,27 @@ def _obfuscate(args):
531538
vmode = advanced | (8 if is_entry else 0)
532539
encrypt_script(prokey, a, b, adv_mode=vmode, rest_mode=restrict,
533540
protection=protection, platforms=platforms,
534-
plugins=plugins)
541+
plugins=plugins, suffix=suffix)
535542
logging.info('%d scripts have been obfuscated', len(files))
536543

537544
if (not args.no_bootstrap) and entry and os.path.exists(entry):
538545
x = args.package_runtime
539546
relative = True if x == 3 else False if x == 2 else None
540547
entryname = entry if args.src else os.path.basename(entry)
541548
if os.path.exists(os.path.join(output, entryname)):
542-
make_entry(entryname, path, output, relative=relative)
549+
make_entry(entryname, path, output, relative=relative,
550+
suffix=suffix)
543551
else:
544552
logging.info('Use outer entry script "%s"', entry)
545-
make_entry(entry, path, output, relative=relative)
553+
make_entry(entry, path, output, relative=relative,
554+
suffix=suffix)
546555

547556
if args.no_runtime:
548557
logging.info('Obfuscate %d scripts OK.', len(files))
549558
return
550559

551560
make_runtime(capsule, output, platforms=platforms,
552-
package=args.package_runtime)
561+
package=args.package_runtime, suffix=suffix)
553562

554563
logging.info('Obfuscate scripts with restrict mode %s',
555564
'on' if args.restrict else 'off')
@@ -697,8 +706,9 @@ def _runtime(args):
697706
output = os.path.join(args.output, name) if args.inside else args.output
698707
package = not args.no_package
699708
platforms = compatible_platform_names(args.platforms)
709+
suffix = get_name_suffix() if args.enable_suffix else ''
700710
make_runtime(capsule, output, licfile=args.with_license,
701-
platforms=platforms, package=package)
711+
platforms=platforms, package=package, suffix=suffix)
702712

703713
filename = os.path.join(output, '__init__.py') if args.inside else \
704714
os.path.join(args.output, name + '.py')
@@ -805,6 +815,8 @@ def _parser():
805815
'and how to make bootstrap code')
806816
cparser.add_argument('-n', '--no-runtime', action='store_true',
807817
help='DO NOT generate runtime files')
818+
cparser.add_argument('--enable-suffix', action='store_true',
819+
help='Generate runtime package with unique name')
808820
cparser.set_defaults(func=_obfuscate)
809821

810822
#
@@ -932,6 +944,8 @@ def _parser():
932944
cparser.add_argument('--package-runtime', choices=(0, 1, 2, 3), type=int,
933945
help='Where to save runtime files, '
934946
'and how to make bootstrap code')
947+
cparser.add_argument('--enable-suffix', type=int, choices=(0, 1),
948+
help='Generate runtime package with unique name')
935949
cparser.set_defaults(func=_config)
936950

937951
#
@@ -1093,6 +1107,8 @@ def _parser():
10931107
action='append',
10941108
help='Generate runtime package for this platform, '
10951109
'use this option multiple times for more platforms')
1110+
cparser.add_argument('--enable-suffix', action='store_true',
1111+
help='Generate runtime package with unique name')
10961112
cparser.add_argument('pkgname', nargs='?', default='pytransform',
10971113
help=argparse.SUPPRESS)
10981114
cparser.set_defaults(func=_runtime)

src/pytransform.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def init_runtime():
8484

8585

8686
@dllmethod
87-
def encrypt_code_object(pubkey, co, flags):
87+
def encrypt_code_object(pubkey, co, flags, suffix=''):
88+
_pytransform.set_option(6, suffix.encode())
8889
prototype = PYFUNCTYPE(py_object, py_object, py_object, c_int)
8990
dlfunc = prototype(('encrypt_code_object', _pytransform))
9091
return dlfunc(pubkey, co, flags)
@@ -220,19 +221,20 @@ def format_platform(platid=None):
220221

221222

222223
# Load _pytransform library
223-
def _load_library(path=None, is_runtime=0, platid=None):
224+
def _load_library(path=None, is_runtime=0, platid=None, suffix=''):
224225
path = os.path.dirname(__file__) if path is None \
225226
else os.path.normpath(path)
226227

227228
plat = platform.system().lower()
229+
name = '_pytransform' + suffix
228230
if plat == 'linux':
229-
filename = os.path.abspath(os.path.join(path, '_pytransform.so'))
231+
filename = os.path.abspath(os.path.join(path, name + '.so'))
230232
elif plat == 'darwin':
231-
filename = os.path.join(path, '_pytransform.dylib')
233+
filename = os.path.join(path, name + '.dylib')
232234
elif plat == 'windows':
233-
filename = os.path.join(path, '_pytransform.dll')
235+
filename = os.path.join(path, name + '.dll')
234236
elif plat == 'freebsd':
235-
filename = os.path.join(path, '_pytransform.so')
237+
filename = os.path.join(path, name + '.so')
236238
else:
237239
raise PytransformError('Platform %s not supported' % plat)
238240

@@ -266,26 +268,35 @@ def _load_library(path=None, is_runtime=0, platid=None):
266268
# Disable advanced mode if required
267269
# m.set_option(5, c_char_p(1))
268270

271+
# Set suffix for private package
272+
if suffix:
273+
m.set_option(6, suffix.encode())
274+
269275
return m
270276

271277

272-
def pyarmor_init(path=None, is_runtime=0, platid=None):
278+
def pyarmor_init(path=None, is_runtime=0, platid=None, suffix=''):
273279
global _pytransform
274-
_pytransform = _load_library(path, is_runtime, platid)
280+
_pytransform = _load_library(path, is_runtime, platid, suffix)
275281
return init_pytransform()
276282

277283

278-
def pyarmor_runtime(path=None):
284+
def pyarmor_runtime(path=None, suffix=''):
279285
try:
280-
pyarmor_init(path, is_runtime=1)
286+
pyarmor_init(path, is_runtime=1, suffix=suffix)
281287
init_runtime()
282288
except Exception as e:
283289
raise PytransformError(e)
284290

291+
# ----------------------------------------------------------
292+
# End of pytransform
293+
# ----------------------------------------------------------
285294

286295
#
287296
# Not available from v5.6
288297
#
298+
299+
289300
def generate_capsule(licfile):
290301
prikey, pubkey, prolic = _generate_project_capsule()
291302
capkey, newkey = _generate_pytransform_key(licfile, pubkey)

0 commit comments

Comments
 (0)
X Tutup