forked from cckuailong/py2sec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
py2sec.py
338 lines (287 loc) · 11.6 KB
/
py2sec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# -*- coding: utf-8 -*-
import getopt
import os
import platform
import shutil
import subprocess
import sys
class OptionsOfBuild():
def __init__(self):
self.pyVer = ''
self.fileName = ''
self.rootName = ''
self.excludeFiles = []
self.nthread = '1'
self.quiet = "False"
self.release = False
isWindows = True if platform.system() == 'Windows' else False
py2sec_version = ('0', '3', '1')
HELP_TEXT = '''
py2sec is a Cross-Platform, Fast and Flexible tool to compile the .py to .so(Linux and Mac) or .pdy(Win).
You can use it to hide the source code of py
It can be called by the main func as "from module import * "
py2sec can be used in the environment of python2 or python3
Usage: python py2sec.py [options] ...
Options:
-v, --version Show the version of the py2sec
-h, --help Show the help info
-p, --python Python version, default is based on the version of python you bind with command "python"
Example: -p 3 (means you tends to encrypt python3)
-d, --directory Directory of your project (if use -d, you encrypt the whole directory)
-f, --file File to be transfered (if use -f, you only encrypt one file)
-m, --maintain List the file or the directory you don't want to transfer
Note: The directories should be suffix by path separate char ('\\' in Windows or '/'),
and must be the relative path to -d's value
Example: -m setup.py,mod/__init__.py,exclude_dir/
-x, --nthread number of parallel thread to build jobs
-q --quiet Quiet Mode, Default: False
-r --release Release Mode, clear all the tmp files, only output the result, Default: False
Example:
python py2sec.py -f test.py
python py2sec.py -f example/test1.py -r
python py2sec.py -d example/ -m test1.py,bbb/
# some OS use command "python3" to run python3, like Ubuntu, you can use -p to solve it
python3 py2sec.py -p 3 -d example/
'''
buildingScript_fileName = 'tmp_py2sec_build.py'
buildingScript_template_fileName = 'py2sec_build.py.template'
def getFiles_inDir(dir_path,
includeSubfolder=True,
path_type=0,
ext_names="*"):
'''获得指定目录下的所有文件,
:param dir_path: 指定的目录路径
:param includeSubfolder: 是否包含子文件夹里的文件,默认 True
:param path_type: 返回的文件路径形式
0 绝对路径,默认值
1 相对路径
2 文件名
:param ext_names: "*" | string | list
可以指定文件扩展名类型,支持以列表形式指定多个扩展名。默认为 "*",即所有扩展名。
举例:".txt" 或 [".jpg",".png"]
:return: 以 yield 方式返回结果
Updated:
2020-04-21
Author:
nodewee (https://nodewee.github.io)
'''
# ext_names
if type(ext_names) is str:
if ext_names != "*":
ext_names = [ext_names]
# lower ext name letters
if type(ext_names) is list:
for i in range(len(ext_names)):
ext_names[i] = ext_names[i].lower()
def willKeep_thisFile_by_ExtName(file_name):
if type(ext_names) is list:
if file_name[0] == '.':
file_ext = file_name
else:
file_ext = os.path.splitext(file_name)[1]
#
if file_ext.lower() not in ext_names:
return False
else:
return True
return True
if includeSubfolder:
len_of_inpath = len(dir_path)
for root, dirs, files in os.walk(dir_path):
for file_name in files:
if not willKeep_thisFile_by_ExtName(file_name):
continue
if path_type == 0: # absolute path
yield os.path.join(root, file_name)
elif path_type == 1: # relative path
yield os.path.join(
root[len_of_inpath:].lstrip(os.path.sep), file_name)
else: # filen ame
yield file_name
else:
for file_name in os.listdir(dir_path):
filepath = os.path.join(dir_path, file_name)
if os.path.isfile(filepath):
#
if not willKeep_thisFile_by_ExtName(file_name):
continue
#
if path_type == 0:
yield filepath
else:
yield file_name
def makeDirs(dirpath):
'''
创建目录
支持多级目录,若目录已存在自动忽略
Updated: 2020-02-27
Author: nodewee (https://nodewee.github.io)
'''
dirpath = dirpath.strip().rstrip(os.path.sep)
if dirpath:
if not os.path.exists(dirpath):
os.makedirs(dirpath)
def getCommandOptions(opts):
try:
options, _ = getopt.getopt(sys.argv[1:], "vhp:d:f:m:x:qr", [
"version", "help", "python=", "directory=", "file=", "maintain=",
"nthread=", "quiet", "release"
])
except getopt.GetoptError:
print("Get options Error")
print(HELP_TEXT)
sys.exit(1)
for key, value in options:
if key in ["-h", "--help"]:
print(HELP_TEXT)
sys.exit(0)
elif key in ["-v", "--version"]:
print("py2sec version {0}".format('.'.join(py2sec_version)))
sys.exit(0)
elif key in ["-p", "--python"]:
opts.pyVer = value
elif key in ["-d", "--directory"]:
if opts.fileName:
print("Canceled. Do not use -d -f at the same time")
sys.exit(1)
if value[-1] == '/':
opts.rootName = value[:-1]
else:
opts.rootName = value
elif key in ["-f", "--file"]:
if opts.rootName:
print("Canceled. Do not use -d -f at the same time")
sys.exit(1)
opts.fileName = value
elif key in ["-m", "--maintain"]:
for path_assign in value.split(","):
if not path_assign[-1:] in ['/', '\\']: # if last char is not a path sep, consider it's assign a file
opts.excludeFiles.append(path_assign)
else: # assign a dir
assign_dir = path_assign.strip('/').strip('\\')
tmp_dir = os.path.join(opts.rootName, assign_dir)
files = getFiles_inDir(dir_path=tmp_dir,
includeSubfolder=True,
path_type=1)
#
for file in files:
fpath = os.path.join(assign_dir, file)
opts.excludeFiles.append(fpath)
elif key in ["-x", "--nthread"]:
opts.nthread = value
elif key in ["-q", "--quiet"]:
opts.quiet = "True"
elif key in ["-r", "--release"]:
opts.release = True
return opts
def getEncryptFileList(opts):
will_compile_files = []
#
if opts.rootName != '':
if not os.path.exists(opts.rootName):
print("No such Directory, please check or use the Absolute Path")
sys.exit(1)
#
pyfiles = getFiles_inDir(dir_path=opts.rootName,
includeSubfolder=True,
path_type=1,
ext_names='.py')
# exclude __init__.py file
pyfiles = [pyfile for pyfile in pyfiles if not pyfile.endswith('__init__.py')]
# filter maintain files
tmp_files = list(set(pyfiles) - set(opts.excludeFiles))
will_compile_files = []
for file in tmp_files:
will_compile_files.append(os.path.join(opts.rootName, file))
#
if opts.fileName != '':
if opts.fileName.endswith(".py"):
will_compile_files.append(opts.fileName)
else:
print("Make sure you give the right name of py file")
return will_compile_files
def genSetup(opts, will_compile_files):
if os.path.exists(buildingScript_fileName):
os.remove(buildingScript_fileName)
with open(buildingScript_template_fileName, "r") as f:
template = f.read()
files = '", r"'.join(will_compile_files)
cont = template % (files, opts.pyVer, opts.nthread, opts.quiet)
with open(buildingScript_fileName, "w") as f:
f.write(cont)
def clearBuildFolders():
if os.path.isdir("build"):
shutil.rmtree("build")
if os.path.isdir("tmp_build"):
shutil.rmtree("tmp_build")
if os.path.isdir("result"):
shutil.rmtree("result")
def clearTmpFiles():
if os.path.isdir("build"):
shutil.rmtree("build")
if os.path.isdir("tmp_build"):
shutil.rmtree("tmp_build")
if os.path.isfile("tmp_py2sec_build.py"):
os.remove("tmp_py2sec_build.py")
def pyEncrypt(opts):
# prepare folders
makeDirs('build')
makeDirs('tmp_build')
if opts.quiet == "True":
log = "> log.txt"
else:
log = ""
cmd = " {0} build_ext {1}".format(buildingScript_fileName, log)
if opts.pyVer == '':
cmd = 'python' + cmd
else:
cmd = 'python' + opts.pyVer + cmd
if not isWindows:
print('> pyEncrypt')
print(cmd)
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT)
code = p.wait()
if code:
print("\nPy2Sec Encrypt Encounter Error")
sys.exit(1)
def genProject(opts, will_compile_files):
makeDirs('result')
for file in getFiles_inDir('build', True, 1, ['.so', '.pyd']):
src_path = os.path.join('build', file)
mid_path = os.path.sep.join(file.split(os.path.sep)[1:-1])
file_name_parts = os.path.basename(src_path).split('.')
file_name = '.'.join([file_name_parts[0]] + file_name_parts[-1:])
dest_path = os.path.join('result', mid_path, file_name)
makeDirs(os.path.dirname(dest_path))
shutil.copy(src_path, dest_path)
# 非编译文件靠谱至生成库路径
not_compile_files = get_not_compile_files(opts, will_compile_files)
for not_compile_file in not_compile_files:
dest_path = os.path.join('result', not_compile_file)
filepath, filename = os.path.split(dest_path)
makeDirs(filepath)
shutil.copyfile(not_compile_file, dest_path)
if opts.release:
clearTmpFiles()
print("\nPy2Sec Encrypt Finished")
def get_not_compile_files(opts, will_compile_files):
"""获取非编译文件"""
files = getFiles_inDir(dir_path=opts.rootName,
includeSubfolder=True,
path_type=1,
ext_names='*')
files = [os.path.join(opts.rootName, file) for file in files if not file.endswith('.pyc')]
not_compile_files = list(set(files) - set(will_compile_files))
return not_compile_files
if __name__ == "__main__":
opts = getCommandOptions(OptionsOfBuild())
will_compile_files = getEncryptFileList(opts)
clearBuildFolders()
if not isWindows:
genSetup(opts, will_compile_files)
pyEncrypt(opts)
else: # Windows OS
for file in will_compile_files:
genSetup(opts, [file])
pyEncrypt(opts)
genProject(opts, will_compile_files)