Skip to content

Commit 027c507

Browse files
committed
build: compile-pyc
1 parent 8d02e9c commit 027c507

File tree

1 file changed

+59
-8
lines changed

1 file changed

+59
-8
lines changed

installer/compile.py

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,80 @@
77
@desc:
88
"""
99
import os
10+
import sys
11+
import shutil
1012
from py_compile import compile
1113

1214

15+
def clean(path_str: str):
16+
for parent, dir_name, filename in os.walk(path_str):
17+
for dir_str in dir_name:
18+
if dir == '__pycache__':
19+
fullname = os.path.join(parent, dir_str)
20+
try:
21+
shutil.rmtree(fullname)
22+
print("Success clean Folder:%s" % fullname)
23+
except Exception as e:
24+
print("Can't clean Folder:%s, reason:%s" % (fullname, e))
25+
26+
1327
def compile_pyc(path_str: str):
14-
"""
15-
将py编译为pyc文件
16-
@param path_str: 需要编译的目录
17-
@return: None
18-
"""
1928
for parent, dir_name, filename in os.walk(path_str):
2029
for cfile in filename:
2130
fullname = os.path.join(parent, cfile)
2231
if cfile[-3:] == '.py':
2332
try:
24-
if compile(fullname, fullname.replace('py', 'pyc')):
33+
if compile(fullname):
2534
if cfile != 'settings.py' and cfile != 'wsgi.py':
2635
os.remove(fullname) # 删除原文件,保留settings.py和wsgi.py
27-
print("Success compile and remove file:%s" % fullname)
36+
print("Success compile and remove file:%s" % fullname)
2837
else:
2938
print("Can't compile file:%s,The original file has been retained" % fullname)
3039
except Exception as e:
3140
print("Can't compile file:%s, reason:%s" % (fullname, e))
3241

3342

43+
def move(path_str: str):
44+
for parent, dir_name, filename in os.walk(path_str):
45+
for c_file in filename:
46+
fullname = os.path.join(parent, c_file)
47+
if c_file[-4:] == '.pyc':
48+
try:
49+
if parent.endswith('__pycache__'):
50+
parent_path = os.path.dirname(parent)
51+
shutil.move(fullname, parent_path)
52+
print('update the dir of file successfully')
53+
except Exception as e:
54+
print("Can't move file:%s, reason:%s" % (fullname, e))
55+
56+
57+
def replace_name(path_str: str):
58+
for parent, dir_name, filename in os.walk(path_str):
59+
for c_file in filename:
60+
fullname = os.path.join(parent, c_file)
61+
if c_file[-4:] == '.pyc':
62+
try:
63+
cfile_name = ''
64+
cfile_list = c_file.split('.')
65+
version = sys.version_info
66+
replace_name_str = 'cpython-' + str(version[0]) + str(version[1])
67+
for i in range(len(cfile_list)):
68+
if cfile_list[i] == replace_name_str:
69+
continue
70+
cfile_name += cfile_list[i]
71+
if i == len(cfile_list) - 1:
72+
continue
73+
cfile_name += '.'
74+
shutil.move(fullname, os.path.join(parent, cfile_name))
75+
print('update the name of the file successfully')
76+
except Exception as e:
77+
print("Can't remove file:%s, reason:%s" % (fullname, e))
78+
79+
3480
if __name__ == '__main__':
35-
compile_pyc("../apps")
81+
path = "/opt/maxkb/app/apps"
82+
clean(path)
83+
compile_pyc(path)
84+
move(path)
85+
replace_name(path)
86+
clean(path)

0 commit comments

Comments
 (0)