@@ -95,21 +95,23 @@ def _fitter_path(self, path : str) -> str:
95
95
""" 让一个路径的分隔符统一为当前系统下的 """
96
96
return path .replace ('/' , self ._sp ).replace ('\\ ' , self ._sp ).rstrip (self ._sp )
97
97
98
- def _get_all_file_list (self , path = '.' , file_type = []) -> list :
99
- """ 获取某个目录下的某文件路径列表 """
98
+ def _get_all_file_list (self , path = '.' , file_type = [], file_name = [] ) -> list :
99
+ """ 获取某个目录下的某类文件或某文件名路径列表 """
100
100
all_file = list ()
101
101
try :
102
102
for file in os .listdir (path ):
103
103
file_path = os .path .join (path , file )
104
104
if os .path .isfile (file_path ):
105
- if file_type :
105
+ if file_type or file_name :
106
106
_ , filename = os .path .split (file_path )
107
107
if filename .split ('.' )[- 1 ] in file_type and file .find ('AutoCython' ) == - 1 :
108
108
all_file .append (file_path )
109
+ if filename in file_name :
110
+ all_file .append (file_path )
109
111
else :
110
112
all_file .append (file_path )
111
113
elif os .path .isdir (file_path ):
112
- all_file .extend (self ._get_all_file_list (file_path , file_type ))
114
+ all_file .extend (self ._get_all_file_list (file_path , file_type , file_name ))
113
115
except FileNotFoundError :
114
116
pass
115
117
finally :
@@ -209,8 +211,20 @@ def compile_th(delete):
209
211
try :
210
212
if not delete :
211
213
delete = self .delete
214
+
215
+ # 重命名所有__init__.py文件,这个文件存在生成的pyd会跑到二级目录
216
+ self .re_init_file_dict = dict ()
217
+ for init_file_path in self ._get_all_file_list (file_name = ['__init__.py' ]):
218
+ # 重命名为___init__.py
219
+ path , name = os .path .split (init_file_path )
220
+ new_name = '_' + name
221
+ new_file_path = os .path .join (path , new_name )
222
+ self .re_init_file_dict [init_file_path ] = new_file_path
223
+ os .rename (init_file_path , new_file_path )
224
+
212
225
# 获取所有的py文件
213
226
py_file_path_iter = map (self ._fitter_path , self ._get_all_file_list (self .compile_path , ['py' ]))
227
+ self .exclude_file_list .extend (['__init__.py' ,'___init__.py' ])
214
228
# 剔除不需要编译的文件
215
229
py_file_path_iter = filter (lambda file_path : file_path not in self .exclude_file_list , py_file_path_iter )
216
230
# 剔除全部只标注了文件名的文件
@@ -242,23 +256,29 @@ def compile_th(delete):
242
256
print ("\033 [0;37;41m任务信息记录失败!\033 [0m" )
243
257
traceback .print_exc ()
244
258
print (err )
245
- print ('Info : \033 [0;37;41merr\033 [0m path : ' , file_path )
259
+ else :
260
+ print ('Info : \033 [0;37;41merr\033 [0m path : ' , file_path )
246
261
else :
247
262
# 编译正确
248
263
try :
249
264
path , file_name = os .path .split (file_path )
250
265
pyd_name = list (filter (lambda name : name .split ('.' )[- 1 ] == 'pyd' and name .split ('.' )[0 ] == file_name [:- 3 ], os .listdir (path )))[0 ]
251
- self .compile_result [file_name [:- 3 ] + '_' + task_index_dict [file_path ]] = Popen_out (cython_popen , file_path , os .path .join (path , pyd_name ))
266
+ pyd_path = os .path .join (path , pyd_name )
267
+ self .compile_result [file_name [:- 3 ] + '_' + task_index_dict [file_path ]] = Popen_out (cython_popen , file_path , pyd_path )
252
268
except Exception as err :
253
269
print ("\033 [0;37;41m任务信息记录失败!\033 [0m" )
254
270
print (file_path )
255
271
traceback .print_exc ()
256
272
print (err )
257
- print ( 'Info : \033 [0;37;44mok \033 [0m path : ' , file_path , ' -> ' , file_path [ 0 : - 2 ] + 'pyd' )
258
- print ("complete!" )
273
+ else :
274
+ print ('Info : \033 [0;37;44mok \033 [0m path : ' , file_path , ' -> ' , pyd_path )
259
275
# 全部结束后清理build文件夹
260
276
for rm_path in self ._get_all_path_list (self .compile_path , file_type = ['build' ]):
261
277
shutil .rmtree (rm_path )
278
+ # 逆向重命名
279
+ for old_file , new_file in self .re_init_file_dict .items ():
280
+ os .rename (new_file , old_file )
281
+ print ("complete!" )
262
282
finally :
263
283
self .compile_lock .release ()
264
284
@@ -280,7 +300,7 @@ def __init__(self):
280
300
self .file_path = ''
281
301
self .a_file_flag = False
282
302
283
- self .version = 'AutoCython V1.0.1 '
303
+ self .version = 'AutoCython V1.1.0 '
284
304
# 像这样写格式好看一点
285
305
self .help_info = (
286
306
"Usage: AutoCython [options] ...\n " +
0 commit comments