|
@@ -7,8 +7,21 @@ import os
|
|
|
import sys
|
|
import sys
|
|
|
import platform
|
|
import platform
|
|
|
import requests
|
|
import requests
|
|
|
|
|
+import time
|
|
|
from deps_registry import registry, Dependency
|
|
from deps_registry import registry, Dependency
|
|
|
|
|
|
|
|
|
|
+if sys.platform == "win32":
|
|
|
|
|
+ try:
|
|
|
|
|
+ sys.stdout.reconfigure(encoding="utf-8")
|
|
|
|
|
+ sys.stderr.reconfigure(encoding="utf-8")
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+try:
|
|
|
|
|
+ from tqdm import tqdm
|
|
|
|
|
+except ImportError:
|
|
|
|
|
+ tqdm = None
|
|
|
|
|
+
|
|
|
SAVE_DIR = os.path.join(os.path.dirname(__file__), "downloads")
|
|
SAVE_DIR = os.path.join(os.path.dirname(__file__), "downloads")
|
|
|
os.makedirs(SAVE_DIR, exist_ok=True)
|
|
os.makedirs(SAVE_DIR, exist_ok=True)
|
|
|
|
|
|
|
@@ -21,7 +34,51 @@ def get_current_platform() -> str:
|
|
|
return "darwin"
|
|
return "darwin"
|
|
|
return "linux"
|
|
return "linux"
|
|
|
|
|
|
|
|
-def download_dependency(dep: Dependency):
|
|
|
|
|
|
|
+def get_effective_proxies() -> dict | None:
|
|
|
|
|
+ """
|
|
|
|
|
+ 自动检测并获取最适用的网络代理:
|
|
|
|
|
+ 1. 检查命令行环境变量 (HTTP_PROXY / HTTPS_PROXY / ALL_PROXY)
|
|
|
|
|
+ 2. 检查 Windows 注册表系统代理 (Internet Settings -> ProxyEnable / ProxyServer)
|
|
|
|
|
+ """
|
|
|
|
|
+ # 1. 优先读取环境变量
|
|
|
|
|
+ for key in ("https_proxy", "http_proxy", "all_proxy", "HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY"):
|
|
|
|
|
+ val = os.environ.get(key)
|
|
|
|
|
+ if val:
|
|
|
|
|
+ if not val.startswith(("http://", "https://", "socks5://")):
|
|
|
|
|
+ val = f"http://{val}"
|
|
|
|
|
+ return {"http": val, "https": val}
|
|
|
|
|
+
|
|
|
|
|
+ # 2. Windows 平台:主动读取系统全局代理设置 (WinINet)
|
|
|
|
|
+ if platform.system().lower() == "windows":
|
|
|
|
|
+ try:
|
|
|
|
|
+ import winreg
|
|
|
|
|
+ with winreg.OpenKey(
|
|
|
|
|
+ winreg.HKEY_CURRENT_USER,
|
|
|
|
|
+ r"Software\Microsoft\Windows\CurrentVersion\Internet Settings"
|
|
|
|
|
+ ) as key:
|
|
|
|
|
+ enabled, _ = winreg.QueryValueEx(key, "ProxyEnable")
|
|
|
|
|
+ if enabled:
|
|
|
|
|
+ server, _ = winreg.QueryValueEx(key, "ProxyServer")
|
|
|
|
|
+ if server:
|
|
|
|
|
+ if "=" in server:
|
|
|
|
|
+ proxies = {}
|
|
|
|
|
+ for part in server.split(";"):
|
|
|
|
|
+ if "=" in part:
|
|
|
|
|
+ p_proto, p_addr = part.split("=", 1)
|
|
|
|
|
+ if not p_addr.startswith(("http://", "https://", "socks5://")):
|
|
|
|
|
+ p_addr = f"http://{p_addr}"
|
|
|
|
|
+ proxies[p_proto.strip()] = p_addr.strip()
|
|
|
|
|
+ return proxies
|
|
|
|
|
+ else:
|
|
|
|
|
+ if not server.startswith(("http://", "https://", "socks5://")):
|
|
|
|
|
+ server = f"http://{server}"
|
|
|
|
|
+ return {"http": server, "https": server}
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+def download_dependency(dep: Dependency, proxies: dict | None = None):
|
|
|
"""下载单个依赖项"""
|
|
"""下载单个依赖项"""
|
|
|
filename = dep.get_filename()
|
|
filename = dep.get_filename()
|
|
|
save_path = os.path.join(SAVE_DIR, filename)
|
|
save_path = os.path.join(SAVE_DIR, filename)
|
|
@@ -35,21 +92,61 @@ def download_dependency(dep: Dependency):
|
|
|
print(f"\n开始下载 [{dep.name}] (版本: {dep.version}, 平台: {dep.platform})", flush=True)
|
|
print(f"\n开始下载 [{dep.name}] (版本: {dep.version}, 平台: {dep.platform})", flush=True)
|
|
|
print(f"目标文件: {filename}", flush=True)
|
|
print(f"目标文件: {filename}", flush=True)
|
|
|
print(f"下载链接: {dep.url}", flush=True)
|
|
print(f"下载链接: {dep.url}", flush=True)
|
|
|
|
|
+ if proxies:
|
|
|
|
|
+ proxy_display = proxies.get("https") or proxies.get("http")
|
|
|
|
|
+ print(f"代理通道: {proxy_display}", flush=True)
|
|
|
|
|
|
|
|
temp_path = save_path + ".tmp"
|
|
temp_path = save_path + ".tmp"
|
|
|
try:
|
|
try:
|
|
|
- r = requests.get(dep.url, stream=True, timeout=60)
|
|
|
|
|
|
|
+ r = requests.get(dep.url, stream=True, timeout=60, proxies=proxies)
|
|
|
r.raise_for_status()
|
|
r.raise_for_status()
|
|
|
- total = 0
|
|
|
|
|
- with open(temp_path, "wb") as f:
|
|
|
|
|
- for chunk in r.iter_content(chunk_size=16384):
|
|
|
|
|
- if chunk:
|
|
|
|
|
- f.write(chunk)
|
|
|
|
|
- total += len(chunk)
|
|
|
|
|
|
|
+ total_size = int(r.headers.get("content-length", 0))
|
|
|
|
|
+ chunk_size = 64 * 1024 # 64 KB 提高传输吞吐量
|
|
|
|
|
+
|
|
|
|
|
+ if tqdm is not None:
|
|
|
|
|
+ with tqdm(
|
|
|
|
|
+ total=total_size,
|
|
|
|
|
+ unit="B",
|
|
|
|
|
+ unit_scale=True,
|
|
|
|
|
+ unit_divisor=1024,
|
|
|
|
|
+ desc=filename,
|
|
|
|
|
+ ncols=90,
|
|
|
|
|
+ leave=True
|
|
|
|
|
+ ) as pbar:
|
|
|
|
|
+ with open(temp_path, "wb") as f:
|
|
|
|
|
+ for chunk in r.iter_content(chunk_size=chunk_size):
|
|
|
|
|
+ if chunk:
|
|
|
|
|
+ f.write(chunk)
|
|
|
|
|
+ pbar.update(len(chunk))
|
|
|
|
|
+ else:
|
|
|
|
|
+ downloaded = 0
|
|
|
|
|
+ start_time = time.time()
|
|
|
|
|
+ with open(temp_path, "wb") as f:
|
|
|
|
|
+ for chunk in r.iter_content(chunk_size=chunk_size):
|
|
|
|
|
+ if chunk:
|
|
|
|
|
+ f.write(chunk)
|
|
|
|
|
+ downloaded += len(chunk)
|
|
|
|
|
+ elapsed = time.time() - start_time
|
|
|
|
|
+ speed = (downloaded / 1024 / 1024) / max(elapsed, 0.001)
|
|
|
|
|
+ if total_size > 0:
|
|
|
|
|
+ percent = (downloaded / total_size) * 100
|
|
|
|
|
+ bar_len = 28
|
|
|
|
|
+ filled = int(bar_len * downloaded / total_size)
|
|
|
|
|
+ bar = "█" * filled + "░" * (bar_len - filled)
|
|
|
|
|
+ print(
|
|
|
|
|
+ f"\r[{bar}] {percent:5.1f}% ({downloaded/1048576:.1f}/{total_size/1048576:.1f} MB) {speed:.2f} MB/s",
|
|
|
|
|
+ end="",
|
|
|
|
|
+ flush=True,
|
|
|
|
|
+ )
|
|
|
|
|
+ else:
|
|
|
|
|
+ print(f"\r已下载: {downloaded/1048576:.1f} MB ({speed:.2f} MB/s)", end="", flush=True)
|
|
|
|
|
+ print()
|
|
|
|
|
+
|
|
|
if os.path.exists(save_path):
|
|
if os.path.exists(save_path):
|
|
|
os.remove(save_path)
|
|
os.remove(save_path)
|
|
|
os.rename(temp_path, save_path)
|
|
os.rename(temp_path, save_path)
|
|
|
- print(f"下载完成: {save_path} ({round(total / 1024 / 1024, 2)} MB)", flush=True)
|
|
|
|
|
|
|
+ final_size = os.path.getsize(save_path)
|
|
|
|
|
+ print(f"✓ 下载完成: {filename} ({round(final_size / 1024 / 1024, 2)} MB)", flush=True)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
if os.path.exists(temp_path):
|
|
if os.path.exists(temp_path):
|
|
|
try:
|
|
try:
|
|
@@ -59,9 +156,31 @@ def download_dependency(dep: Dependency):
|
|
|
raise e
|
|
raise e
|
|
|
|
|
|
|
|
def main():
|
|
def main():
|
|
|
|
|
+ # 代理判定:支持 --no-proxy 或 --proxy <addr>,默认自动读取系统代理
|
|
|
|
|
+ proxies = get_effective_proxies()
|
|
|
|
|
+ args = sys.argv[1:]
|
|
|
|
|
+
|
|
|
|
|
+ if "--no-proxy" in args:
|
|
|
|
|
+ args.remove("--no-proxy")
|
|
|
|
|
+ proxies = None
|
|
|
|
|
+ print("[代理状态] 用户已禁用代理,使用直连模式", flush=True)
|
|
|
|
|
+ elif any(a.startswith("--proxy=") for a in args):
|
|
|
|
|
+ p_arg = [a for a in args if a.startswith("--proxy=")][0]
|
|
|
|
|
+ args.remove(p_arg)
|
|
|
|
|
+ p_val = p_arg.split("=", 1)[1]
|
|
|
|
|
+ if not p_val.startswith(("http://", "https://", "socks5://")):
|
|
|
|
|
+ p_val = f"http://{p_val}"
|
|
|
|
|
+ proxies = {"http": p_val, "https": p_val}
|
|
|
|
|
+
|
|
|
|
|
+ if proxies:
|
|
|
|
|
+ p_addr = proxies.get("https") or proxies.get("http")
|
|
|
|
|
+ print(f"[代理状态] 已自动启用系统/环境代理: {p_addr}", flush=True)
|
|
|
|
|
+ else:
|
|
|
|
|
+ print("[代理状态] 未检测到代理设置,使用直连模式", flush=True)
|
|
|
|
|
+
|
|
|
# 目标平台判定:支持传参覆盖(如 python download_deps.py linux / windows / all),默认自动检测宿主机
|
|
# 目标平台判定:支持传参覆盖(如 python download_deps.py linux / windows / all),默认自动检测宿主机
|
|
|
- if len(sys.argv) > 1:
|
|
|
|
|
- arg = sys.argv[1].lower().replace("--", "")
|
|
|
|
|
|
|
+ if args:
|
|
|
|
|
+ arg = args[0].lower().replace("--", "")
|
|
|
if arg in ("docker", "linux"):
|
|
if arg in ("docker", "linux"):
|
|
|
target = "linux"
|
|
target = "linux"
|
|
|
elif arg in ("win", "windows"):
|
|
elif arg in ("win", "windows"):
|
|
@@ -73,7 +192,7 @@ def main():
|
|
|
else:
|
|
else:
|
|
|
target = get_current_platform()
|
|
target = get_current_platform()
|
|
|
|
|
|
|
|
- print(f"目标运行平台: [{target}] (可通过命令行指定: python download_deps.py linux / windows / darwin / all)", flush=True)
|
|
|
|
|
|
|
+ print(f"[目标平台] [{target}] (可通过命令行指定: python download_deps.py linux / windows / darwin / all)\n", flush=True)
|
|
|
|
|
|
|
|
deps = registry.get_by_platform(target)
|
|
deps = registry.get_by_platform(target)
|
|
|
if not deps:
|
|
if not deps:
|
|
@@ -81,10 +200,10 @@ def main():
|
|
|
print(f"平台 [{target}] 未注册任何依赖。当前可用平台: {available_platforms}", flush=True)
|
|
print(f"平台 [{target}] 未注册任何依赖。当前可用平台: {available_platforms}", flush=True)
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
- print(f"已匹配到 {len(deps)} 个依赖项:\n", flush=True)
|
|
|
|
|
|
|
+ print(f"已匹配到 {len(deps)} 个依赖项:", flush=True)
|
|
|
for dep in deps:
|
|
for dep in deps:
|
|
|
try:
|
|
try:
|
|
|
- download_dependency(dep)
|
|
|
|
|
|
|
+ download_dependency(dep, proxies=proxies)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
print(f"下载失败 [{dep.name} - {dep.version}]: {e}", flush=True)
|
|
print(f"下载失败 [{dep.name} - {dep.version}]: {e}", flush=True)
|
|
|
|
|
|