|
7 | 7 | from dotenv import load_dotenv |
8 | 8 | import winreg |
9 | 9 | import locale |
| 10 | +import threading |
| 11 | +import time |
| 12 | +from concurrent.futures import ThreadPoolExecutor |
10 | 13 |
|
11 | 14 |
|
12 | 15 | # Inputs: MooaRootDir engineBranchName projectBranchName [--Clean --BuildEngine --ZipEngine --ZipProject --Release --Reupload] |
13 | 16 |
|
14 | 17 | # ================= Defines ================= |
15 | 18 | repo_name = "JasonMa0012/MooaToon" |
16 | 19 |
|
17 | | -mooatoon_root_path = r"E:\MooaToon" |
| 20 | +mooatoon_root_path = r"E:\Mooa" |
18 | 21 | if len(sys.argv) > 1: |
19 | 22 | mooatoon_root_path = sys.argv[1] |
20 | 23 |
|
|
28 | 31 |
|
29 | 32 | # Default Input |
30 | 33 | argv = [ |
| 34 | + # '--Release' |
31 | 35 | '--Reupload' |
32 | 36 | ] |
33 | 37 | if len(sys.argv) > 4: |
@@ -87,6 +91,68 @@ def async_run(args): |
87 | 91 | return process.poll() |
88 | 92 |
|
89 | 93 |
|
| 94 | +def upload_single_asset(repo_name, tag_name, file_path, lock): |
| 95 | + """上传单个文件的线程函数,失败时无限重试""" |
| 96 | + file_name = os.path.basename(file_path) |
| 97 | + retry_count = 0 |
| 98 | + |
| 99 | + with lock: |
| 100 | + print(f"开始上传: {file_name}") |
| 101 | + |
| 102 | + while True: |
| 103 | + try: |
| 104 | + ghr.gh_asset_upload(repo_name, tag_name, [file_path]) |
| 105 | + |
| 106 | + with lock: |
| 107 | + if retry_count > 0: |
| 108 | + print(f"上传成功: {file_name} (重试 {retry_count} 次后成功)") |
| 109 | + else: |
| 110 | + print(f"上传成功: {file_name}") |
| 111 | + return True |
| 112 | + |
| 113 | + except Exception as e: |
| 114 | + retry_count += 1 |
| 115 | + with lock: |
| 116 | + print(f"上传失败: {file_name}, 错误: {str(e)}") |
| 117 | + print(f"第 {retry_count} 次重试中... (3秒后重试)") |
| 118 | + |
| 119 | + # 等待3秒后重试 |
| 120 | + time.sleep(3) |
| 121 | + |
| 122 | + |
| 123 | +def multithread_upload(repo_name, tag_name, file_paths): |
| 124 | + """多线程上传文件,每个文件都会重试直到成功""" |
| 125 | + if not file_paths: |
| 126 | + print("没有文件需要上传") |
| 127 | + return True |
| 128 | + |
| 129 | + # 计算并发线程数:文件总数的1/3,但至少1个 |
| 130 | + max_workers = max(1, len(file_paths) // 3) |
| 131 | + |
| 132 | + print(f"开始多线程上传 {len(file_paths)} 个文件...") |
| 133 | + print(f"同时上传文件数量: {max_workers} (文件总数的1/3)") |
| 134 | + print("注意: 每个文件都会重试直到上传成功") |
| 135 | + |
| 136 | + lock = threading.Lock() |
| 137 | + |
| 138 | + # 使用线程池控制并发数量 |
| 139 | + with ThreadPoolExecutor(max_workers=max_workers) as executor: |
| 140 | + # 提交所有上传任务 |
| 141 | + futures = [] |
| 142 | + for file_path in file_paths: |
| 143 | + future = executor.submit(upload_single_asset, repo_name, tag_name, file_path, lock) |
| 144 | + futures.append(future) |
| 145 | + |
| 146 | + # 等待所有任务完成 |
| 147 | + for future in futures: |
| 148 | + future.result() # 这会等待任务完成,如果有异常会抛出 |
| 149 | + |
| 150 | + with lock: |
| 151 | + print(f"所有文件上传完成: {len(file_paths)} 个文件全部上传成功") |
| 152 | + |
| 153 | + return True # 现在总是返回True,因为会无限重试直到成功 |
| 154 | + |
| 155 | + |
90 | 156 | # ================= Main ================== |
91 | 157 | load_dotenv(dotenv_path=get_onedrive_env_path()) |
92 | 158 |
|
@@ -145,21 +211,29 @@ def async_run(args): |
145 | 211 | publish=False, |
146 | 212 | body=comment, |
147 | 213 | name=release_name, |
148 | | - asset_pattern=file_paths, |
149 | 214 | ) |
150 | | - ghr.gh_release_publish(repo_name, release_name) |
| 215 | + # 使用多线程上传 |
| 216 | + upload_success = multithread_upload(repo_name, release_name, file_paths) |
| 217 | + if upload_success: |
| 218 | + ghr.gh_release_publish(repo_name, release_name) |
| 219 | + else: |
| 220 | + print("部分文件上传失败,请检查后重试") |
151 | 221 |
|
152 | 222 | if '--Reupload' in argv: |
153 | 223 | print("======Reupload======") |
154 | 224 | if last_draft_info is not None: |
155 | 225 | # 仅上传失败的文件 |
156 | 226 | for asset in last_draft_info['assets']: |
157 | | - for file_path in file_paths: |
| 227 | + for file_path in file_paths[:]: # 使用副本避免在迭代时修改列表 |
158 | 228 | if file_path.endswith(asset['name']): |
159 | 229 | file_paths.remove(file_path) |
160 | 230 |
|
161 | | - ghr.gh_asset_upload(repo_name, last_draft_info['tag_name'], file_paths) |
162 | | - ghr.gh_release_publish(repo_name, last_draft_info['tag_name']) |
| 231 | + # 使用多线程上传 |
| 232 | + upload_success = multithread_upload(repo_name, last_draft_info['tag_name'], file_paths) |
| 233 | + if upload_success: |
| 234 | + ghr.gh_release_publish(repo_name, last_draft_info['tag_name']) |
| 235 | + else: |
| 236 | + print("部分文件上传失败,请检查后重试") |
163 | 237 | else: |
164 | 238 | print("\nThere is no draft!\n") |
165 | 239 |
|
|
0 commit comments