跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

chromium-download.py

119 行 · 5.0 KB历史下载
1"""Prefetch only the Chromium archives selected by the upcoming APT transaction."""
2
3import os
4import subprocess
5import sys
6from pathlib import Path
7
8# Older or minimal container images can still use their normal apt-get install.
9try:
10 import apt
11 import apt_pkg
12 from apt.progress.base import AcquireProgress
13except ImportError:
14 print("Using configured APT sources (python3-apt unavailable).")
15 sys.exit(0)
16
17
18SECURITY_LIST = Path("/etc/apt/mirrors/debian-security.list")
19
20
21def stock_security_uri(uri):
22 # The Debian cloud image delegates this URI to a separate security list.
23 # A customized list or other repository keeps its own download routes.
24 if uri.startswith("mirror+file:" + str(SECURITY_LIST) + "/"):
25 if not SECURITY_LIST.is_file():
26 return False
27 return SECURITY_LIST.read_text().startswith("# Cage managed Debian mirrors\n")
28 return False
29
30
31def security_mirrors():
32 if not SECURITY_LIST.is_file():
33 return []
34 lines = SECURITY_LIST.read_text().splitlines()
35 if not lines or lines[0] != "# Cage managed Debian mirrors":
36 return []
37 # Reuse only mirrors selected by the bounded reachability probe. Explicit
38 # official preference and custom sources never cause hidden mirror requests.
39 allowed = ("https://mirrors.nju.edu.cn/debian-security/", "https://mirrors.ustc.edu.cn/debian-security/")
40 return [parts[0].rstrip("/") + "/" for line in lines[1:]
41 if (parts := line.split()) and parts[0].rstrip("/") + "/" in allowed]
42
43
44class Progress(AcquireProgress):
45 def __init__(self, package, size):
46 super().__init__()
47 self.package = package
48 self.size = size
49 self.previous = -1
50
51 def report(self, received):
52 if os.environ.get("CAGE_SETUP_PROGRESS") == "1" and received != self.previous:
53 print(f"CAGE_TOOL_BYTES {self.package}.deb {received} {self.size}", flush=True)
54 self.previous = received
55
56 def pulse(self, owner):
57 self.report(min(int(self.current_bytes), self.size))
58 return True
59
60
61def fetch(package, version, target):
62 hashes = apt_pkg.HashStringList()
63 hashes.append(apt_pkg.HashString("SHA256", version.sha256))
64 partial = target.parent / "partial" / (target.name + ".cage")
65 for mirror in security_mirrors():
66 print(f"Downloading {package} {version.version} from {mirror}", flush=True)
67 progress = Progress(package, version.size)
68 acquire = apt_pkg.Acquire(progress)
69 item = apt_pkg.AcquireFile(acquire, mirror + version.filename, hashes, version.size, destfile=str(partial))
70 acquire.run()
71 if item.complete and item.status == item.STAT_DONE and hashes.verify_file(str(partial)):
72 # APT has verified the official digest. Publish on this volume
73 # under its own archive lock; never expose an unverified .deb.
74 partial.chmod(0o644)
75 partial.replace(target)
76 progress.report(version.size)
77 return
78 if item.status == item.STAT_AUTH_ERROR or item.complete:
79 partial.unlink(missing_ok=True)
80 # APT quarantines responses with an incorrect digest under .FAILED.
81 Path(str(partial) + ".FAILED").unlink(missing_ok=True)
82 print(f"Mirror could not supply the authenticated {package} archive.", flush=True)
83 print(f"Using the official Debian security source for {package}.", flush=True)
84
85
86def prefetch(install_options):
87 # --print-uris resolves the same install/options without installing. Its
88 # destination names include APT's architecture and any escaped epoch.
89 plan = subprocess.check_output(
90 ["apt-get", "--print-uris", "install", "-y", *install_options], text=True
91 )
92 cache = apt.Cache()
93 archives = Path(apt_pkg.config.find_dir("Dir::Cache::archives"))
94 apt_pkg.config.set("Acquire::Retries", "0")
95 apt_pkg.config.set("Acquire::https::Timeout", "15")
96 with apt_pkg.FileLock(str(archives / "lock")):
97 for line in plan.splitlines():
98 if not line.startswith("'"):
99 continue
100 uri, filename, *_ = line.split()
101 package = filename.partition("_")[0]
102 if package not in ("chromium", "chromium-common") or Path(filename).name != filename:
103 continue
104 candidate = cache[package].candidate
105 if not candidate or candidate.uri != uri.strip("'") or not stock_security_uri(candidate.uri):
106 continue
107 origin = candidate.origins[0]
108 if not (origin.trusted and origin.origin == "Debian" and origin.label == "Debian-Security" and candidate.sha256):
109 continue
110 fetch(package, candidate, archives / filename)
111
112
113if __name__ == "__main__":
114 try:
115 prefetch(sys.argv[1:])
116 except (OSError, apt_pkg.Error, subprocess.CalledProcessError) as error:
117 # apt-get owns dependency resolution, the installation, and its error
118 # messages, including in the presence of concurrent package operations.
119 print(f"Using configured APT sources ({type(error).__name__}).", flush=True)