This repository has been archived by the owner on Nov 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aptconfig.py
141 lines (132 loc) · 5.12 KB
/
aptconfig.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
# Copyright 2011 James Ascroft-Leigh
"""\
%prog [options] [JSON_CONFIG]
"""
from jwalutil import trim
from process import call
import json
import optparse
import subprocess
import sys
DEFAULT_CONFIG = {
"mirror": "http://archive.ubuntu.com/ubuntu",
"distribution": None,
"components": [
"main",
"universe",
"restricted",
# "multiverse",
],
"prefixes": [
"deb",
"deb-src",
],
"extensions": [
"",
"-security",
"-updates",
# "-proposed",
# "-backports",
],
}
def fix_newlines(data):
if "\r" not in data:
data = data.replace("\n", "\r\n")
return data
def pretty_print_json(json_data):
return fix_newlines(
json.dumps(json.loads(json_data),
indent=2, separators=(", ", ": ")))
def render_to_sources_list(config):
mirror = config["mirror"]
assert " " not in mirror, repr(mirror)
distribution = config["distribution"]
assert " " not in distribution, repr(distribution)
results = []
for prefix in sorted(config["prefixes"]):
assert " " not in prefix, repr(prefix)
for component in sorted(config["components"]):
assert " " not in component, repr(component)
for extension in sorted(config["extensions"]):
assert " " not in extension, repr(extension)
results.append("%(prefix)s %(mirror)s "
"%(distribution)s%(extension)s "
"%(component)s" % locals())
message = """\
## This file is automatically generated using the following configuration:\r\n\
"""
header = "".join("# %s\r\n" % (l,) for l in
pretty_print_json(json.dumps(config)).split("\r\n"))
body = "".join("%s\r\n" % (l,) for l in results)
assert len(results) == len(set(results)), results
return message + header + body
BLANK_SOURCES = """\
## File intentionally blank; see /etc/apt/sources.list.d/*.list\r\n\
"""
def main(argv):
parser = optparse.OptionParser(__doc__)
parser.add_option("--install", dest="do_install",
default=False, action="store_true")
parser.add_option("--skip-proxy-install", dest="do_proxy_install",
default=True, action="store_false")
parser.add_option("--skip-blank-sources-list-install", dest="do_blank",
default=True, action="store_false")
parser.add_option("--proxy", dest="proxy",
default="http://ubuntu.devel.cmedltd.com:3142/")
parser.add_option("--no-proxy", dest="proxy", action="store_const",
const=None)
parser.add_option("--proxy-basename", dest="proxy_basename",
default="02-generated-proxy")
parser.add_option("--basename", dest="basename", default="generated")
options, args = parser.parse_args(argv)
custom_json = json.dumps({})
if len(args) > 0:
custom_json = args.pop(0)
if len(args) > 0:
parser.error("Unexpected: %r" % (args,))
default_json = json.dumps(DEFAULT_CONFIG)
defaults = json.loads(default_json)
custom = json.loads(custom_json)
for key, value in defaults.items():
custom.setdefault(key, value)
if custom["distribution"] is None:
custom["distribution"] = trim(
call(["lsb_release", "--short", "--codename"]), suffix="\r\n")
output = render_to_sources_list(custom)
if options.do_install:
assert "\0" not in options.basename and "/" not in options.basename,\
repr(options.basename)
file_data = {
"/etc/apt/sources.list.d/%s.list" % (options.basename,): output}
if options.do_blank:
file_data["/etc/apt/sources.list"] = BLANK_SOURCES
if options.do_proxy_install:
if options.proxy is None:
proxy_data = "# No proxy\r\n"
else:
assert '"' not in options.proxy, repr(options.proxy)
proxy_data = ('Acquire::HTTP { Proxy::"%s"; }\r\n'
% (options.proxy,))
assert ("\0" not in options.proxy_basename
and "/" not in options.proxy_basename), \
repr(options.proxy_basename)
file_data["/etc/apt/apt.conf.d/%s" % (options.proxy_basename,)] = \
proxy_data
child = subprocess.Popen(["sudo", "python", "-c", """\
assert __name__ == "__main__"
import sys
assert len(sys.argv) == 1
import json
file_data = json.loads(sys.stdin.read())
for file_path, data in sorted(file_data.items()):
fh = open(file_path, "wb")
try:
fh.write(data)
finally:
fh.close()
"""], stdin=subprocess.PIPE)
child.communicate(json.dumps(file_data))
assert child.returncode == 0, child.returncode
sys.stdout.write(output)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))