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
| import os
ignore_files = ['build', 'dist', 'data', 'package', 'venv', '__pycache__', '.git', 'setup.py', 'setup_main.py', '__init__.py']
ignore_names = ['__init__.py']
ignore_move = ['venv', '__pycache__', 'server.log', 'setup.py', 'setup_main.py']
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ignore_files = [os.path.join(BASE_DIR, x) for x in ignore_files]
package = True
package_name = "dist"
package_path = os.path.join(BASE_DIR, package_name)
if not os.path.exists(package_path): os.mkdir(package_path) translate_pys = []
def translate_dir(path): pathes = os.listdir(path) for p in pathes: if p in ignore_names: continue if p.startswith('__') or p.startswith('.') or p.startswith('build'): continue f_path = os.path.join(path, p) if f_path in ignore_files: continue if os.path.isdir(f_path): translate_dir(f_path) else: if not f_path.endswith('.py') and not f_path.endswith('.pyx'): continue if f_path.endswith('__init__.py') or f_path.endswith('__init__.pyx'): continue with open(f_path, 'r') as f: content = f.read() if not content.startswith('# cython: language_level=3'): content = '# cython: language_level=3\n' + content with open(f_path, 'w') as f1: f1.write(content) os.system('python setup.py ' + f_path + ' build_ext --inplace') translate_pys.append(f_path) f_name = '.'.join(f_path.split('.')[:-1]) py_file = '.'.join([f_name, 'py']) c_file = '.'.join([f_name, 'c']) if os.path.exists(c_file): os.remove(c_file)
def remove_dir(path, rm_path=True): if not os.path.exists(path): return pathes = os.listdir(path) for p in pathes: f_path = os.path.join(path, p) if os.path.isdir(f_path): remove_dir(f_path, False) os.rmdir(f_path) else: os.remove(f_path) if rm_path: os.rmdir(path)
def mv_to_packages(path=BASE_DIR): pathes = os.listdir(path) for p in pathes: if p.startswith('.'): continue if p in ignore_move: continue f_path = os.path.join(path, p) if f_path == package_path: continue p_f_path = f_path.replace(BASE_DIR, package_path) if os.path.isdir(f_path): if not os.path.exists(p_f_path): os.mkdir(p_f_path) mv_to_packages(f_path) else: if not f_path.endswith('.py') or f_path not in translate_pys: with open(f_path, 'rb') as f: content = f.read() with open(p_f_path, 'wb') as f: f.write(content) if f_path.endswith('.pyd') or f_path.endswith('.so'): os.remove(f_path)
def batch_rename(src_path): filenames = os.listdir(src_path) same_name = [] count = 0 for filename in filenames: old_name = os.path.join(src_path, filename) if old_name == package_path: continue if os.path.isdir(old_name): batch_rename(old_name) if filename[-4:] == ".pyd" or filename[-3:] == ".so": old_pyd = filename.split(".") new_pyd = str(old_pyd[0]) + "." + str(old_pyd[2]) else: continue change_name = new_pyd count += 1 new_name = os.path.join(src_path, change_name) if change_name in filenames: same_name.append(change_name) continue os.rename(old_name, new_name)
def run(): translate_dir(BASE_DIR) remove_dir(os.path.join(BASE_DIR, 'build')) if package: mv_to_packages() batch_rename(os.path.join(BASE_DIR, package_name))
if __name__ == '__main__': run()
|