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
|
#!/usr/bin/env python3
from aptsources.sourceslist import SourcesList, Deb822SourceEntry
import apt_pkg
import glob
import os.path
import sys
import os
def main(rootfs):
sources = SourcesList(False, deb822=True)
dbglvl = os.environ.get("MMDEBSTRAP_VERBOSITY", "-1")
try:
dbglvl = int(dbglvl)
except ValueError:
dbglvl = -1
if dbglvl >= 2:
print("before:", file=sys.stderr)
for s in sources.list:
print(s.str(), end="", file=sys.stderr)
os.makedirs(rootfs + "/etc/apt/sources.list.d", exist_ok=True)
# remove all the things we do not want and change components and uri
for s in list(sources):
# since we put everything into a single output file, only one-line
# format is supported for now
assert not isinstance(s, Deb822SourceEntry), s
s.file = rootfs + s.file
# if it's a local file:// repo, don't mangle it
if s.uri.startswith("file://"):
continue
# remove debug packages
if s.uri.endswith("/debian-debug"):
sources.remove(s)
continue
# only allow main
s.comps = [c for c in s.comps if c in ["main"]]
# remove sources with empty components list
if not s.comps:
sources.remove(s)
continue
# remove deb-src entries
if s.type == "deb-src":
sources.remove(s)
continue
# translate uri to our proxy on 127.0.0.1
s.uri = "http://127.0.0.1/debian"
# remove duplicates
# we cannot use set() because order must be preserved
seen = set()
for s in list(sources):
if s.str() in seen:
sources.remove(s)
continue
seen.add(s.str())
if dbglvl >= 2:
print("after:", file=sys.stderr)
for s in sources.list:
print(s.str(), end="", file=sys.stderr)
# we want to append to existing files, so backup the files which would
# otherwise be overwritten by sources.save()
files_to_backup = {}
for s in sources.list:
if os.path.isfile(s.file):
files_to_backup[s.file] = b""
for fname in files_to_backup:
with open(fname, "rb") as f:
files_to_backup[fname] = f.read()
sources.save()
# now prepend the original content
for fname in files_to_backup:
with open(fname, "rb") as f:
updated_content = f.read()
with open(fname, "wb") as f:
f.write(files_to_backup[fname] + b"\n" + updated_content)
# append apt preferences verbatim
for pref in glob.glob("/etc/apt/preferences.d/*"):
if not os.path.isfile(pref):
continue
os.makedirs(os.path.dirname(rootfs + pref), exist_ok=True)
with open(pref) as fin, open(rootfs + pref, "a") as fout:
fout.write("\n")
for line in fin:
fout.write(line)
if __name__ == "__main__":
main(sys.argv[1])
|