| 1 | """Prefetch only the Chromium archives selected by the upcoming APT transaction.""" |
| 2 | |
| 3 | import os |
| 4 | import subprocess |
| 5 | import sys |
| 6 | from pathlib import Path |
| 7 | |
| 8 | |
| 9 | try: |
| 10 | import apt |
| 11 | import apt_pkg |
| 12 | from apt.progress.base import AcquireProgress |
| 13 | except ImportError: |
| 14 | print("Using configured APT sources (python3-apt unavailable).") |
| 15 | sys.exit(0) |
| 16 | |
| 17 | |
| 18 | SECURITY_LIST = Path("/etc/apt/mirrors/debian-security.list") |
| 19 | |
| 20 | |
| 21 | def stock_security_uri(uri): |
| 22 | |
| 23 | |
| 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 | |
| 31 | def 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 | |
| 38 | |
| 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 | |
| 44 | class 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 | |
| 61 | def 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 | |
| 73 | |
| 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 | |
| 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 | |
| 86 | def prefetch(install_options): |
| 87 | |
| 88 | |
| 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 | |
| 113 | if __name__ == "__main__": |
| 114 | try: |
| 115 | prefetch(sys.argv[1:]) |
| 116 | except (OSError, apt_pkg.Error, subprocess.CalledProcessError) as error: |
| 117 | |
| 118 | |
| 119 | print(f"Using configured APT sources ({type(error).__name__}).", flush=True) |