Skip to content

Commit a72c1de

Browse files
committed
修复如果存在__init__.py文件不能正常编译的bug
1 parent 3539811 commit a72c1de

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

AutoCython.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,23 @@ def _fitter_path(self, path : str) -> str:
9595
""" 让一个路径的分隔符统一为当前系统下的 """
9696
return path.replace('/', self._sp).replace('\\', self._sp).rstrip(self._sp)
9797

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+
""" 获取某个目录下的某类文件或某文件名路径列表 """
100100
all_file = list()
101101
try:
102102
for file in os.listdir(path):
103103
file_path = os.path.join(path, file)
104104
if os.path.isfile(file_path):
105-
if file_type:
105+
if file_type or file_name:
106106
_, filename = os.path.split(file_path)
107107
if filename.split('.')[-1] in file_type and file.find('AutoCython') == -1:
108108
all_file.append(file_path)
109+
if filename in file_name:
110+
all_file.append(file_path)
109111
else:
110112
all_file.append(file_path)
111113
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))
113115
except FileNotFoundError:
114116
pass
115117
finally:
@@ -209,8 +211,20 @@ def compile_th(delete):
209211
try:
210212
if not delete:
211213
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+
212225
# 获取所有的py文件
213226
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'])
214228
# 剔除不需要编译的文件
215229
py_file_path_iter = filter(lambda file_path : file_path not in self.exclude_file_list, py_file_path_iter)
216230
# 剔除全部只标注了文件名的文件
@@ -242,23 +256,29 @@ def compile_th(delete):
242256
print("\033[0;37;41m任务信息记录失败!\033[0m")
243257
traceback.print_exc()
244258
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)
246261
else:
247262
# 编译正确
248263
try:
249264
path, file_name = os.path.split(file_path)
250265
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)
252268
except Exception as err:
253269
print("\033[0;37;41m任务信息记录失败!\033[0m")
254270
print(file_path)
255271
traceback.print_exc()
256272
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)
259275
# 全部结束后清理build文件夹
260276
for rm_path in self._get_all_path_list(self.compile_path, file_type=['build']):
261277
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!")
262282
finally:
263283
self.compile_lock.release()
264284

@@ -280,7 +300,7 @@ def __init__(self):
280300
self.file_path = ''
281301
self.a_file_flag = False
282302

283-
self.version = 'AutoCython V1.0.1'
303+
self.version = 'AutoCython V1.1.0'
284304
# 像这样写格式好看一点
285305
self.help_info =(
286306
"Usage: AutoCython [options] ...\n"+

0 commit comments

Comments
 (0)