Skip to content

Commit 702060d

Browse files
committed
Refine example
1 parent 8964dd6 commit 702060d

File tree

1 file changed

+5
-92
lines changed

1 file changed

+5
-92
lines changed

src/examples/helloworld/foo.py

Lines changed: 5 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,11 @@
11
import os
22
import sys
33

4-
#-----------------------------------------------------------
5-
#
6-
# Part 1: protect dynamic library
7-
#
8-
#-----------------------------------------------------------
9-
104
import pytransform
11-
from hashlib import md5
12-
13-
MD5SUM_LIB_PYTRANSFORM = {
14-
'windows32': '0f0f533b2b3568bef4ffbad22d93cec2',
15-
'windows64': 'fe454a42cd146bfea7f4c833d863e5fe',
16-
'linux32': '3f194b707001f4132ae144946cc0a914',
17-
'linux32/arm': '727f9a40ba5ccc72ccc9a530caa35214',
18-
'linux64': '74aa1e1ee07961f2c42ae8d9af9f7b64',
19-
'linux64/arm': 'dacce677af832c5360872dc3086c6acb',
20-
'darwin64': '23da1bbc5b7768657d8a76a1a403a8c8',
21-
}
22-
23-
def check_lib_pytransform(filename):
24-
print("Check md5sum for %s..." % filename)
25-
expected = MD5SUM_LIB_PYTRANSFORM[pytransform.format_platname()]
26-
with open(filename, 'rb') as f:
27-
if not md5(f.read()).hexdigest() == expected:
28-
print("Check failed")
29-
sys.exit(1)
30-
print("Check OK.")
31-
32-
def check_obfuscated_script():
33-
CO_SIZES = 46, 36
34-
CO_NAMES = set(['pytransform', 'pyarmor_runtime', '__pyarmor__',
35-
'__name__', '__file__'])
36-
print('Check obfuscated script %s ...' % __file__)
37-
co = sys._getframe(3).f_code
38-
if not ((set(co.co_names) <= CO_NAMES)
39-
and (len(co.co_code) in CO_SIZES)):
40-
print('Obfuscated script has been changed by others')
41-
sys.exit(1)
42-
print('Check OK.')
43-
44-
def check_mod_pytransform():
45-
CO_NAMES = set(['Exception', 'LoadLibrary', 'None', 'PYFUNCTYPE',
46-
'PytransformError', '__file__', '_debug_mode',
47-
'_get_error_msg', '_handle', '_load_library',
48-
'_pytransform', 'abspath', 'basename', 'byteorder',
49-
'c_char_p', 'c_int', 'c_void_p', 'calcsize', 'cdll',
50-
'dirname', 'encode', 'exists', 'exit',
51-
'format_platname', 'get_error_msg', 'init_pytransform',
52-
'init_runtime', 'int', 'isinstance', 'join', 'lower',
53-
'normpath', 'os', 'path', 'platform', 'print',
54-
'pyarmor_init', 'pythonapi', 'restype', 'set_option',
55-
'str', 'struct', 'sys', 'system', 'version_info'])
56-
57-
colist = []
58-
59-
code = '__code__' if sys.version_info[0] == 3 else 'func_code'
60-
for name in ('dllmethod', 'init_pytransform', 'init_runtime',
61-
'_load_library', 'pyarmor_init', 'pyarmor_runtime'):
62-
colist.append(getattr(getattr(pytransform, name), code))
63-
64-
closure = '__closure__' if sys.version_info[0] == 3 else 'func_closure'
65-
for name in ('init_pytransform', 'init_runtime'):
66-
colist.append(getattr(getattr(getattr(pytransform, name),
67-
closure)[0].cell_contents, code))
68-
colist.append(getattr(pytransform.dllmethod, code).co_consts[1])
69-
70-
for co in colist:
71-
print('Check %s ...' % co.co_name)
72-
if not (set(co.co_names) < CO_NAMES):
73-
print('Check failed')
74-
sys.exit(1)
75-
print('Check OK.')
76-
77-
def protect_pytransform():
78-
try:
79-
# Be sure obfuscated script is not changed
80-
check_obfuscated_script()
81-
82-
# Be sure '_pytransform._name' in 'pytransform.py' is not changed
83-
check_mod_pytransform()
84-
85-
# Be sure '_pytransform.so' is not changed
86-
check_lib_pytransform(pytransform._pytransform._name)
87-
except Exception as e:
88-
print(e)
89-
sys.exit(1)
905

916
#-----------------------------------------------------------
927
#
93-
# Part 2: check internet time by ntp server
8+
# Part 1: check internet time by ntp server
949
#
9510
#-----------------------------------------------------------
9611

@@ -112,7 +27,7 @@ def check_expired_date_by_ntp():
11227

11328
#-----------------------------------------------------------
11429
#
115-
# Part 3: show license information of obfuscated scripts
30+
# Part 2: show license information of obfuscated scripts
11631
#
11732
#-----------------------------------------------------------
11833

@@ -131,25 +46,23 @@ def show_left_days_of_license():
13146

13247
#-----------------------------------------------------------
13348
#
134-
# Part 4: business code
49+
# Part 3: business code
13550
#
13651
#-----------------------------------------------------------
13752

13853
def hello():
13954
print('Hello world!')
14055

141-
def sum(a, b):
56+
def sum2(a, b):
14257
return a + b
14358

14459
def main():
14560
hello()
146-
print('1 + 1 = %d' % sum(1, 1))
61+
print('1 + 1 = %d' % sum2(1, 1))
14762

14863
if __name__ == '__main__':
14964

150-
protect_pytransform()
15165
show_left_days_of_license()
152-
15366
# check_expired_date_by_ntp()
15467

15568
main()

0 commit comments

Comments
 (0)