|
1 | 1 | import os |
2 | 2 | import subprocess |
3 | 3 | from concurrent.futures import ThreadPoolExecutor |
4 | | -from status import status |
5 | 4 |
|
| 5 | +# mirrors config (written only if missing) |
6 | 6 | mirrors = """:main: |
7 | 7 | install_script = https://raw.githubusercontent.com/redroselinux/car-binary-storage/main/ |
8 | 8 | packagelist = https://raw.githubusercontent.com/redroselinux/car/main/existing-packages.txt |
|
16 | 16 | :end: |
17 | 17 | """ |
18 | 18 |
|
19 | | -# Write mirrors only if they do not exist |
20 | | -for path in ["/etc/mirrors.car", "/home/user/.config/mirrors.car"]: |
| 19 | +# write mirrors only if they do not exist |
| 20 | +for path in ["/etc/mirrors.car", os.path.expanduser("~/.config/mirrors.car")]: |
21 | 21 | if not os.path.exists(path): |
22 | 22 | os.makedirs(os.path.dirname(path), exist_ok=True) |
23 | 23 | with open(path, "w") as f: |
24 | 24 | f.write(mirrors) |
25 | 25 |
|
26 | | -install_script_places = [] |
27 | | -packagelist_places = [] |
28 | | -versions_places = [] |
| 26 | +install_script_places = [] # list of (repo, url) |
| 27 | +packagelist_places = [] # list of (repo, url) |
| 28 | +versions_places = [] # list of (repo, url) |
29 | 29 | repos = [] |
30 | 30 |
|
31 | 31 | current_repo = None |
32 | 32 | current_data = {} |
33 | 33 |
|
| 34 | +# parse mirrors config |
34 | 35 | for line in mirrors.strip().splitlines(): |
35 | 36 | line = line.strip() |
| 37 | + |
36 | 38 | if line.startswith(":") and line.endswith(":") and line != ":end:": |
37 | 39 | current_repo = line.strip(":") |
38 | 40 | repos.append(current_repo) |
39 | 41 | current_data = {} |
| 42 | + |
40 | 43 | elif line == ":end:": |
41 | 44 | if "install_script" in current_data: |
42 | | - install_script_places.append(current_data["install_script"]) |
| 45 | + install_script_places.append((current_repo, current_data["install_script"])) |
43 | 46 | if "packagelist" in current_data: |
44 | | - packagelist_places.append(current_data["packagelist"]) |
| 47 | + packagelist_places.append((current_repo, current_data["packagelist"])) |
45 | 48 | if "versions" in current_data: |
46 | | - versions_places.append(current_data["versions"]) |
| 49 | + versions_places.append((current_repo, current_data["versions"])) |
47 | 50 | current_repo = None |
48 | 51 | current_data = {} |
| 52 | + |
49 | 53 | elif current_repo and "=" in line: |
50 | 54 | key, value = line.split("=", 1) |
51 | 55 | current_data[key.strip()] = value.strip() |
52 | 56 |
|
53 | | -def fetch_one(url): |
| 57 | + |
| 58 | +def fetch_one(repo, url): |
54 | 59 | print("fetch " + url) |
55 | 60 | try: |
56 | 61 | result = subprocess.run( |
57 | 62 | ["curl", "-fsSL", url], |
58 | 63 | capture_output=True, |
59 | 64 | text=True, |
60 | | - check=True |
| 65 | + check=True, |
61 | 66 | ) |
62 | 67 | packages = [line.strip() for line in result.stdout.splitlines() if line.strip()] |
63 | | - repo_name = url.strip().split("/")[-2] if "/" in url else "unknown" |
64 | | - return [f"{repo_name}/{pkg}" for pkg in packages] |
| 68 | + return [f"{repo}/{pkg}" for pkg in packages] |
65 | 69 | except subprocess.CalledProcessError: |
66 | 70 | return [] |
67 | 71 |
|
| 72 | + |
68 | 73 | def fetch_all_packages(packagelist_places, max_threads=8): |
69 | 74 | all_packages = [] |
70 | 75 | with ThreadPoolExecutor(max_workers=max_threads) as executor: |
71 | | - results = executor.map(fetch_one, packagelist_places) |
| 76 | + results = executor.map( |
| 77 | + lambda x: fetch_one(x[0], x[1]), |
| 78 | + packagelist_places, |
| 79 | + ) |
72 | 80 | for r in results: |
73 | 81 | all_packages.extend(r) |
74 | 82 | return all_packages |
0 commit comments