download_deps.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # download_deps.py
  2. """
  3. 依赖下载管理器
  4. 接入 deps_registry 注册中心,根据指定或当前平台自动拉取所需的二进制依赖。
  5. """
  6. import os
  7. import sys
  8. import platform
  9. import requests
  10. from deps_registry import registry, Dependency
  11. SAVE_DIR = os.path.join(os.path.dirname(__file__), "downloads")
  12. os.makedirs(SAVE_DIR, exist_ok=True)
  13. def get_current_platform() -> str:
  14. """获取当前宿主机操作系统平台"""
  15. sys_name = platform.system().lower()
  16. if "windows" in sys_name:
  17. return "windows"
  18. elif "darwin" in sys_name:
  19. return "darwin"
  20. return "linux"
  21. def download_dependency(dep: Dependency):
  22. """下载单个依赖项"""
  23. filename = dep.get_filename()
  24. save_path = os.path.join(SAVE_DIR, filename)
  25. # 若文件已存在且有效,直接跳过,避免重复下载
  26. if os.path.exists(save_path) and os.path.getsize(save_path) > 0:
  27. size_mb = round(os.path.getsize(save_path) / 1024 / 1024, 2)
  28. print(f"[已存在跳过] {dep.name} (版本: {dep.version}) -> {filename} ({size_mb} MB)", flush=True)
  29. return
  30. print(f"\n开始下载 [{dep.name}] (版本: {dep.version}, 平台: {dep.platform})", flush=True)
  31. print(f"目标文件: {filename}", flush=True)
  32. print(f"下载链接: {dep.url}", flush=True)
  33. temp_path = save_path + ".tmp"
  34. try:
  35. r = requests.get(dep.url, stream=True, timeout=60)
  36. r.raise_for_status()
  37. total = 0
  38. with open(temp_path, "wb") as f:
  39. for chunk in r.iter_content(chunk_size=16384):
  40. if chunk:
  41. f.write(chunk)
  42. total += len(chunk)
  43. if os.path.exists(save_path):
  44. os.remove(save_path)
  45. os.rename(temp_path, save_path)
  46. print(f"下载完成: {save_path} ({round(total / 1024 / 1024, 2)} MB)", flush=True)
  47. except Exception as e:
  48. if os.path.exists(temp_path):
  49. try:
  50. os.remove(temp_path)
  51. except Exception:
  52. pass
  53. raise e
  54. def main():
  55. # 目标平台判定:支持传参覆盖(如 python download_deps.py linux / windows / all),默认自动检测宿主机
  56. if len(sys.argv) > 1:
  57. arg = sys.argv[1].lower().replace("--", "")
  58. if arg in ("docker", "linux"):
  59. target = "linux"
  60. elif arg in ("win", "windows"):
  61. target = "windows"
  62. elif arg in ("mac", "darwin"):
  63. target = "darwin"
  64. else:
  65. target = arg
  66. else:
  67. target = get_current_platform()
  68. print(f"目标运行平台: [{target}] (可通过命令行指定: python download_deps.py linux / windows / darwin / all)", flush=True)
  69. deps = registry.get_by_platform(target)
  70. if not deps:
  71. available_platforms = list({d.platform for d in registry.get_all()})
  72. print(f"平台 [{target}] 未注册任何依赖。当前可用平台: {available_platforms}", flush=True)
  73. return
  74. print(f"已匹配到 {len(deps)} 个依赖项:\n", flush=True)
  75. for dep in deps:
  76. try:
  77. download_dependency(dep)
  78. except Exception as e:
  79. print(f"下载失败 [{dep.name} - {dep.version}]: {e}", flush=True)
  80. if __name__ == "__main__":
  81. main()