Skip to content

Commit 0994dfe

Browse files
feat: 直接 iscc pack.iss
More: fix: 修复一些类型警告 feat: 完成后尝试清理工作区
1 parent 7d6c24f commit 0994dfe

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

AutoPack/AutoPack.py

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def generate_iss_file():
175175
def compress_folder_to_7z(source_folder, target_folder, archive_name):
176176
try:
177177
# 压缩文件夹为7z格式
178-
with py7zr.SevenZipFile(os.path.join(target_folder, archive_name + '.7z'), 'w') as archive:
178+
with py7zr.SevenZipFile(str(os.path.join(target_folder, archive_name + '.7z')), 'w') as archive:
179179
archive.writeall(source_folder, arcname=os.path.basename(source_folder))
180180

181181
print(f"{Fore.GREEN}{Fore.RESET} 压缩7z发行版 {Fore.BLUE}{archive_name}{Fore.RESET} 成功")
@@ -187,10 +187,10 @@ def compress_folder_to_7z(source_folder, target_folder, archive_name):
187187
def compress_folder_to_zip(source_folder, target_folder, archive_name):
188188
try:
189189
# 压缩文件夹为zip格式
190-
with zipfile.ZipFile(os.path.join(target_folder, archive_name + '.zip'), 'w', zipfile.ZIP_DEFLATED) as archive:
190+
with zipfile.ZipFile(str(os.path.join(target_folder, archive_name + '.zip')), 'w', zipfile.ZIP_DEFLATED) as archive:
191191
for root, dirs, files in os.walk(source_folder):
192192
for file in files:
193-
archive.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), source_folder))
193+
archive.write(str(os.path.join(root, file)), os.path.relpath(str(os.path.join(root, file)), source_folder))
194194

195195
print(f"{Fore.GREEN}{Fore.RESET} 压缩zip发行版 {Fore.BLUE}{archive_name}{Fore.RESET} 成功")
196196
return True
@@ -204,8 +204,9 @@ def compress_releases():
204204
compress_folder_to_zip(py_releases_dir, releases_dir, "Chinese_git_py")
205205
compress_folder_to_7z(pack_releases_dir, releases_dir, "Chinese_git")
206206
compress_folder_to_7z(py_releases_dir, releases_dir, "Chinese_git_py")
207+
subprocess.run(["iscc", os.path.join(releases_dir, "pack.iss")], check=True)
207208
return True
208-
except:
209+
except Exception:
209210
return False
210211
# -----------
211212

@@ -218,10 +219,54 @@ def jobs_clean_up(step):
218219
shutil.rmtree(py_releases_dir)
219220
shutil.rmtree(repo_dir)
220221
print(f"{Fore.GREEN}{Fore.RESET} 成功清理工作文件")
222+
return True
221223
except Exception as e:
222224
print(f"{Fore.RED}{Fore.RESET} 清理失败,因为:\n{Fore.RED}{e}{Fore.RESET}")
225+
return False
223226
else:
224227
print(f"{Fore.YELLOW}{Fore.RESET} 跳过清理,因为目录 {Fore.BLUE}{repo_dir}{Fore.RESET} 不存在")
228+
return True
229+
elif step == "finish":
230+
if os.path.exists(repo_dir):
231+
# Chinese_git 仓库
232+
try:
233+
# 移除非空目录 repo_dir
234+
shutil.rmtree(repo_dir)
235+
print(f"{Fore.GREEN}{Fore.RESET} 成功清理 Chinese_git 仓库文件")
236+
# 权限
237+
except PermissionError:
238+
print(f"{Fore.RED}{Fore.RESET} 清理 Chinese_git 仓库文件失败: {Fore.YELLOW}权限不足{Fore.RESET}")
239+
return False
240+
except Exception as e:
241+
print(f"{Fore.RED}{Fore.RESET} 清理 Chinese_git 仓库文件失败: {Fore.RED}{e}{Fore.RESET}")
242+
return False
243+
if os.path.exists(os.path.join(script_dir, "yazicbs.github.io")):
244+
# (方法二) 公告文件所在仓库
245+
try:
246+
shutil.rmtree(os.path.join(script_dir, "yazicbs.github.io"))
247+
print(f"{Fore.GREEN}{Fore.RESET} 成功清理 yazicbs.github.io 仓库文件")
248+
except PermissionError:
249+
print(f"{Fore.RED}{Fore.RESET} 清理 yazicbs.github.io 仓库文件失败: {Fore.YELLOW}权限不足{Fore.RESET}")
250+
return False
251+
except Exception as e:
252+
print(f"{Fore.RED}{Fore.RESET} 清理 yazicbs.github.io 仓库文件失败: {Fore.RED}{e}{Fore.RESET}")
253+
return False
254+
if os.path.exists(os.path.join(releases_dir, "pack.iss")):
255+
# 移除单文件
256+
try:
257+
os.remove(os.path.join(releases_dir, "pack.iss"))
258+
print(f"{Fore.GREEN}{Fore.RESET} 成功清理 pack.iss 文件")
259+
except PermissionError:
260+
print(f"{Fore.RED}{Fore.RESET} 清理 pack.iss 文件失败: {Fore.YELLOW}权限不足{Fore.RESET}")
261+
return False
262+
except Exception as e:
263+
print(f"{Fore.RED}{Fore.RESET} 清理 pack.iss 文件失败: {Fore.RED}{e}{Fore.RESET}")
264+
return False
265+
print(f"{Fore.GREEN}{Fore.RESET} 成功清理工作区!")
266+
return True
267+
else:
268+
print(f"{Fore.RED}{Fore.RESET} 未知步骤 {Fore.BLUE}{step}{Fore.RESET}")
269+
return False
225270

226271
# --- main ---
227272
def main():
@@ -231,7 +276,8 @@ def main():
231276
if copy_file():
232277
if generate_iss_file():
233278
if compress_releases():
234-
return 0
279+
if jobs_clean_up("finish"):
280+
return 0
235281
else:
236282
jobs_clean_up("clone")
237283
else:

0 commit comments

Comments
 (0)