forked from osbuild/osbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org.osbuild.dnf.config
executable file
·82 lines (58 loc) · 1.98 KB
/
org.osbuild.dnf.config
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
#!/usr/bin/python3
import os
import sys
import iniparse
import osbuild.api
def configure_variable(tree, name, value):
"""
Creates a persistent DNF configuration for the given variable name and
value.
From DNF.CONF(5):
Filenames may contain only alphanumeric characters and underscores and be
in lowercase.
"""
vars_directory = "/etc/dnf/vars"
with open(f"{tree}{vars_directory}/{name}", "w", encoding="utf8") as f:
f.write(value + "\n")
def make_value(value):
if isinstance(value, list):
val = ", ".join(value)
return val
val = str(value)
return val
def make_section(cfg, name, settings):
if not cfg.has_section(name):
cfg.add_section(name)
for key, value in settings.items():
val = make_value(value)
cfg.set(name, key, val)
def make_dnf_config(tree, config_options):
"""
Merges the given config object into /etc/dnf/dnf.conf, overwriting existing
values.
"""
dnf_config_path = f"{tree}/etc/dnf/dnf.conf"
dnf_config = iniparse.SafeConfigParser()
try:
with open(dnf_config_path, "r", encoding="utf8") as f:
dnf_config.readfp(f)
except FileNotFoundError:
print(f"Warning: DNF configuration file '{dnf_config_path}' does not exist, will create it.")
os.makedirs(f"{tree}/etc/dnf", exist_ok=True)
for section, items in config_options.items():
make_section(dnf_config, section, items)
with open(dnf_config_path, "w", encoding="utf8") as f:
os.fchmod(f.fileno(), 0o644)
dnf_config.write(f)
def main(tree, options):
variables = options.get("variables", [])
for variable in variables:
configure_variable(tree, variable["name"], variable["value"])
config_options = options.get("config")
if config_options:
make_dnf_config(tree, config_options)
return 0
if __name__ == '__main__':
args = osbuild.api.arguments()
r = main(args["tree"], args["options"])
sys.exit(r)