|
7 | 7 | @desc: |
8 | 8 | """ |
9 | 9 | import os |
| 10 | +import sys |
| 11 | +import shutil |
10 | 12 | from py_compile import compile |
11 | 13 |
|
12 | 14 |
|
| 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 | + |
13 | 27 | def compile_pyc(path_str: str): |
14 | | - """ |
15 | | - 将py编译为pyc文件 |
16 | | - @param path_str: 需要编译的目录 |
17 | | - @return: None |
18 | | - """ |
19 | 28 | for parent, dir_name, filename in os.walk(path_str): |
20 | 29 | for cfile in filename: |
21 | 30 | fullname = os.path.join(parent, cfile) |
22 | 31 | if cfile[-3:] == '.py': |
23 | 32 | try: |
24 | | - if compile(fullname, fullname.replace('py', 'pyc')): |
| 33 | + if compile(fullname): |
25 | 34 | if cfile != 'settings.py' and cfile != 'wsgi.py': |
26 | 35 | 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) |
28 | 37 | else: |
29 | 38 | print("Can't compile file:%s,The original file has been retained" % fullname) |
30 | 39 | except Exception as e: |
31 | 40 | print("Can't compile file:%s, reason:%s" % (fullname, e)) |
32 | 41 |
|
33 | 42 |
|
| 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 | + |
34 | 80 | 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