-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmerge-apt-repo.py
executable file
·159 lines (135 loc) · 5 KB
/
merge-apt-repo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
import argparse
import gzip
import io
import json
import logging
import lzma
import os
import re
import requests
import sys
from concurrent.futures import ThreadPoolExecutor
from threading import Lock
package_version = {arch: {} for arch in ["all", "amd64", "i386", "arm64"]}
package_info = {arch: {} for arch in ["all", "amd64", "i386", "arm64"]}
lock = {arch: Lock() for arch in ["all", "amd64", "i386", "arm64"]}
USER_AGENT = "Debian APT-HTTP/1.3 (2.6.1)" # from Debian 12
"""
repo info json format:
"repo_name": {
"repo": repo url, end with "/"
"xxx_path": {
"arch": repo Packages file path of "arch", start with no "/"
}
}
"""
def read_repo_list(repo_list_file: str) -> dict:
try:
with open(repo_list_file, "r") as f:
return json.load(f)
except Exception as e:
logging.error(f"Error reading repo list: {e}")
return {}
def get_remote_packages(repo_url: str, file_path: str) -> bytes:
"""
get the packages file content from remote repo
"""
file_url = repo_url + file_path
try:
response = requests.get(
file_url, timeout=10, headers={"User-Agent": USER_AGENT}
)
if response.status_code != 200:
logging.error(
f"GetError: {file_url} returned status {response.status_code}"
)
return b""
content = b""
if file_url.endswith(".gz"): # Packages.gz
with gzip.GzipFile(fileobj=io.BytesIO(response.content)) as f:
content = f.read()
elif file_url.endswith(".xz"): # Packages.xz
with lzma.LZMAFile(io.BytesIO(response.content)) as f:
content = f.read()
else: # Packages
content = response.content
# complete the two newlines if the ending is less than two newlines
# 结尾不足两个换行符的话,补全两个换行符
if not content.endswith(b"\n\n"):
content += b"\n"
return content.replace(b"Filename: ", f"Filename: {repo_url}".encode())
except Exception as e:
logging.error(f"Error fetching packages: {e}")
return b""
def get_latest(deb_packages: bytes):
"""
split the information of each packet, deduplication and store the latest in infoList
将每个包的信息分割开,去重并将最新的存放到 infoList 中
"""
deb_packages = re.sub(rb"^Package: ", b"{{start}}Package: ", deb_packages, flags=re.MULTILINE)
info_list = deb_packages.split(b"{{start}}")[1:]
find_name = re.compile(rb"Package: (.+)")
find_arch = re.compile(rb"Architecture: (.+)")
find_version = re.compile(rb"Version: (.+)")
for v in info_list:
try:
name = find_name.search(v).group(1).decode()
arch = find_arch.search(v).group(1).decode()
tmp_version = find_version.search(v).group(1).decode()
with lock[arch]:
if (
name not in package_version[arch]
or os.system(
f"dpkg --compare-versions {tmp_version} gt {package_version[arch][name]}"
)
== 0
):
package_version[arch][name] = tmp_version
package_info[arch][name] = v
except Exception as e:
logging.error(f"Error processing package {name}: {e}")
return
def process_repo(r: dict):
"""
获取仓库中不同架构子仓库的内容,最后调用 get_latest 去重并保存。
"""
try:
for arch, path in r["path"].items():
get_latest(get_remote_packages(r["repo"], path))
except Exception as e:
logging.error(f"Error processing repo {r.get('name', 'unknown')}: {e}")
def parse_arguments():
parser = argparse.ArgumentParser(
description="A script to merge the latest versions Packages files"
)
parser.add_argument(
"-r",
"--repo",
type=str,
default="data/repo_list.json",
help="Path to the repository list file. Default is 'data/repo_list.json'.",
)
parser.add_argument("--local", type=str, help="Process Packages in local repo")
return parser.parse_args()
if __name__ == "__main__":
args = parse_arguments()
# 处理本地 repo
if args.local:
with open(args.local) as f:
get_latest(f.read().encode())
# 读取 repo_list 配置
repo_list = read_repo_list(args.repo)
if not repo_list:
sys.exit()
# 多线程,同时限制最大线程数
with ThreadPoolExecutor(max_workers=10) as executor:
executor.map(process_repo, repo_list.values())
# 分别输出到不同文件
for arch in ["amd64", "arm64"]:
os.makedirs(f"deb/dists/wcbing/main/binary-{arch}/", exist_ok=True)
with open(f"deb/dists/wcbing/main/binary-{arch}/Packages", "+wb") as f:
for i in package_info[arch].values():
f.write(i)
for i in package_info["all"].values():
f.write(i)