download_deps.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # download_deps.py
  2. """
  3. 依赖下载管理器
  4. 接入 deps_registry 注册中心,根据指定或当前平台自动拉取所需的二进制依赖。
  5. """
  6. import os
  7. import sys
  8. import platform
  9. import requests
  10. import time
  11. from deps_registry import registry, Dependency
  12. if sys.platform == "win32":
  13. try:
  14. sys.stdout.reconfigure(encoding="utf-8")
  15. sys.stderr.reconfigure(encoding="utf-8")
  16. except Exception:
  17. pass
  18. try:
  19. from tqdm import tqdm
  20. except ImportError:
  21. tqdm = None
  22. SAVE_DIR = os.path.join(os.path.dirname(__file__), "downloads")
  23. os.makedirs(SAVE_DIR, exist_ok=True)
  24. def get_current_platform() -> str:
  25. """获取当前宿主机操作系统平台"""
  26. sys_name = platform.system().lower()
  27. if "windows" in sys_name:
  28. return "windows"
  29. elif "darwin" in sys_name:
  30. return "darwin"
  31. return "linux"
  32. def get_effective_proxies() -> dict | None:
  33. """
  34. 自动检测并获取最适用的网络代理:
  35. 1. 检查命令行环境变量 (HTTP_PROXY / HTTPS_PROXY / ALL_PROXY)
  36. 2. 检查 Windows 注册表系统代理 (Internet Settings -> ProxyEnable / ProxyServer)
  37. """
  38. # 1. 优先读取环境变量
  39. for key in ("https_proxy", "http_proxy", "all_proxy", "HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY"):
  40. val = os.environ.get(key)
  41. if val:
  42. if not val.startswith(("http://", "https://", "socks5://")):
  43. val = f"http://{val}"
  44. return {"http": val, "https": val}
  45. # 2. Windows 平台:主动读取系统全局代理设置 (WinINet)
  46. if platform.system().lower() == "windows":
  47. try:
  48. import winreg
  49. with winreg.OpenKey(
  50. winreg.HKEY_CURRENT_USER,
  51. r"Software\Microsoft\Windows\CurrentVersion\Internet Settings"
  52. ) as key:
  53. enabled, _ = winreg.QueryValueEx(key, "ProxyEnable")
  54. if enabled:
  55. server, _ = winreg.QueryValueEx(key, "ProxyServer")
  56. if server:
  57. if "=" in server:
  58. proxies = {}
  59. for part in server.split(";"):
  60. if "=" in part:
  61. p_proto, p_addr = part.split("=", 1)
  62. if not p_addr.startswith(("http://", "https://", "socks5://")):
  63. p_addr = f"http://{p_addr}"
  64. proxies[p_proto.strip()] = p_addr.strip()
  65. return proxies
  66. else:
  67. if not server.startswith(("http://", "https://", "socks5://")):
  68. server = f"http://{server}"
  69. return {"http": server, "https": server}
  70. except Exception:
  71. pass
  72. return None
  73. def download_dependency(dep: Dependency, proxies: dict | None = None):
  74. """下载单个依赖项"""
  75. filename = dep.get_filename()
  76. save_path = os.path.join(SAVE_DIR, filename)
  77. # 若文件已存在且有效,直接跳过,避免重复下载
  78. if os.path.exists(save_path) and os.path.getsize(save_path) > 0:
  79. size_mb = round(os.path.getsize(save_path) / 1024 / 1024, 2)
  80. print(f"[已存在跳过] {dep.name} (版本: {dep.version}) -> {filename} ({size_mb} MB)", flush=True)
  81. return
  82. print(f"\n开始下载 [{dep.name}] (版本: {dep.version}, 平台: {dep.platform})", flush=True)
  83. print(f"目标文件: {filename}", flush=True)
  84. print(f"下载链接: {dep.url}", flush=True)
  85. if proxies:
  86. proxy_display = proxies.get("https") or proxies.get("http")
  87. print(f"代理通道: {proxy_display}", flush=True)
  88. temp_path = save_path + ".tmp"
  89. try:
  90. r = requests.get(dep.url, stream=True, timeout=60, proxies=proxies)
  91. r.raise_for_status()
  92. total_size = int(r.headers.get("content-length", 0))
  93. chunk_size = 64 * 1024 # 64 KB 提高传输吞吐量
  94. if tqdm is not None:
  95. with tqdm(
  96. total=total_size,
  97. unit="B",
  98. unit_scale=True,
  99. unit_divisor=1024,
  100. desc=filename,
  101. ncols=90,
  102. leave=True
  103. ) as pbar:
  104. with open(temp_path, "wb") as f:
  105. for chunk in r.iter_content(chunk_size=chunk_size):
  106. if chunk:
  107. f.write(chunk)
  108. pbar.update(len(chunk))
  109. else:
  110. downloaded = 0
  111. start_time = time.time()
  112. with open(temp_path, "wb") as f:
  113. for chunk in r.iter_content(chunk_size=chunk_size):
  114. if chunk:
  115. f.write(chunk)
  116. downloaded += len(chunk)
  117. elapsed = time.time() - start_time
  118. speed = (downloaded / 1024 / 1024) / max(elapsed, 0.001)
  119. if total_size > 0:
  120. percent = (downloaded / total_size) * 100
  121. bar_len = 28
  122. filled = int(bar_len * downloaded / total_size)
  123. bar = "█" * filled + "░" * (bar_len - filled)
  124. print(
  125. f"\r[{bar}] {percent:5.1f}% ({downloaded/1048576:.1f}/{total_size/1048576:.1f} MB) {speed:.2f} MB/s",
  126. end="",
  127. flush=True,
  128. )
  129. else:
  130. print(f"\r已下载: {downloaded/1048576:.1f} MB ({speed:.2f} MB/s)", end="", flush=True)
  131. print()
  132. if os.path.exists(save_path):
  133. os.remove(save_path)
  134. os.rename(temp_path, save_path)
  135. final_size = os.path.getsize(save_path)
  136. print(f"✓ 下载完成: {filename} ({round(final_size / 1024 / 1024, 2)} MB)", flush=True)
  137. except Exception as e:
  138. if os.path.exists(temp_path):
  139. try:
  140. os.remove(temp_path)
  141. except Exception:
  142. pass
  143. raise e
  144. def main():
  145. # 代理判定:支持 --no-proxy 或 --proxy <addr>,默认自动读取系统代理
  146. proxies = get_effective_proxies()
  147. args = sys.argv[1:]
  148. if "--no-proxy" in args:
  149. args.remove("--no-proxy")
  150. proxies = None
  151. print("[代理状态] 用户已禁用代理,使用直连模式", flush=True)
  152. elif any(a.startswith("--proxy=") for a in args):
  153. p_arg = [a for a in args if a.startswith("--proxy=")][0]
  154. args.remove(p_arg)
  155. p_val = p_arg.split("=", 1)[1]
  156. if not p_val.startswith(("http://", "https://", "socks5://")):
  157. p_val = f"http://{p_val}"
  158. proxies = {"http": p_val, "https": p_val}
  159. if proxies:
  160. p_addr = proxies.get("https") or proxies.get("http")
  161. print(f"[代理状态] 已自动启用系统/环境代理: {p_addr}", flush=True)
  162. else:
  163. print("[代理状态] 未检测到代理设置,使用直连模式", flush=True)
  164. # 目标平台判定:支持传参覆盖(如 python download_deps.py linux / windows / all),默认自动检测宿主机
  165. if args:
  166. arg = args[0].lower().replace("--", "")
  167. if arg in ("docker", "linux"):
  168. target = "linux"
  169. elif arg in ("win", "windows"):
  170. target = "windows"
  171. elif arg in ("mac", "darwin"):
  172. target = "darwin"
  173. else:
  174. target = arg
  175. else:
  176. target = get_current_platform()
  177. print(f"[目标平台] [{target}] (可通过命令行指定: python download_deps.py linux / windows / darwin / all)\n", flush=True)
  178. deps = registry.get_by_platform(target)
  179. if not deps:
  180. available_platforms = list({d.platform for d in registry.get_all()})
  181. print(f"平台 [{target}] 未注册任何依赖。当前可用平台: {available_platforms}", flush=True)
  182. return
  183. print(f"已匹配到 {len(deps)} 个依赖项:", flush=True)
  184. for dep in deps:
  185. try:
  186. download_dependency(dep, proxies=proxies)
  187. except Exception as e:
  188. print(f"下载失败 [{dep.name} - {dep.version}]: {e}", flush=True)
  189. if __name__ == "__main__":
  190. main()