1
2
3 """Initial setup of an archive."""
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import os
23 import sys
24
25 import apt_pkg
26
27 from daklib import utils
28 from daklib.dbconn import Component, DBConn, Keyring, PolicyQueue, Suite
29
30
31
32 Cnf = None
33
34
35
36
38 """Print a usage message and exit with 'exit_code'."""
39
40 print(
41 """Usage: dak init-dirs
42 Creates directories for an archive based on dak.conf configuration file.
43
44 -h, --help show this help and exit."""
45 )
46 sys.exit(exit_code)
47
48
49
50
51
52 -def do_dir(target, config_name):
53 """If 'target' exists, make sure it is a directory. If it doesn't, create
54 it."""
55
56 if os.path.exists(target):
57 if not os.path.isdir(target):
58 utils.fubar("%s (%s) is not a directory." % (target, config_name))
59 else:
60 print("Creating {} ...".format(target))
61 os.makedirs(target)
62
63
65 """Create directories for a config entry that's a filename."""
66
67 if config_name in config:
68 target = os.path.dirname(config[config_name])
69 do_dir(target, config_name)
70
71
73 """Create directories for a config tree."""
74
75 for entry in config.subtree(tree).list():
76 entry = entry.lower()
77 config_name = "%s::%s" % (tree, entry)
78 target = config[config_name]
79 do_dir(target, config_name)
80
81
83 """Create directories for morgue sub directories."""
84
85 config_name = "%s::MorgueSubDir" % (subdir)
86 if config_name in Cnf:
87 target = os.path.join(Cnf["Dir::Morgue"], Cnf[config_name])
88 do_dir(target, config_name)
89
90
92 """Create empty keyring if necessary."""
93
94 if os.path.exists(fullpath):
95 return
96
97 keydir = os.path.dirname(fullpath)
98
99 if not os.path.isdir(keydir):
100 print("Creating {} ...".format(keydir))
101 os.makedirs(keydir)
102 if secret:
103
104 os.chmod(keydir, 0o700)
105
106
107 print("Creating {} ...".format(fullpath))
108 with open(fullpath, "w") as fh:
109 if secret:
110 os.fchmod(fh.fileno(), 0o600)
111 else:
112 os.fchmod(fh.fileno(), 0o644)
113
114
115
116
117
119 """Create directories referenced in dak.conf."""
120
121 session = DBConn().session()
122
123
124 process_tree(Cnf, "Dir")
125
126
127 if "Dir::Base" in Cnf:
128 do_dir(
129 os.path.join(Cnf["Dir::Base"], "queue", "unchecked"), "unchecked directory"
130 )
131
132
133 for queue in session.query(PolicyQueue):
134 do_dir(queue.path, "%s queue" % queue.queue_name)
135
136 if queue.queue_name == "new":
137 do_dir(
138 os.path.join(queue.path, "COMMENTS"),
139 "%s queue comments" % queue.queue_name,
140 )
141
142 for config_name in ["Rm::LogFile", "Import-Archive::ExportDir"]:
143 process_file(Cnf, config_name)
144
145 for subdir in ["Clean-Queues", "Clean-Suites"]:
146 process_morguesubdir(subdir)
147
148 suite_suffix = "%s" % (Cnf.find("Dinstall::SuiteSuffix"))
149
150
151 if "Dinstall::SigningKeyring" in Cnf:
152 process_keyring(Cnf["Dinstall::SigningKeyring"], secret=True)
153
154 if "Dinstall::SigningPubKeyring" in Cnf:
155 process_keyring(Cnf["Dinstall::SigningPubKeyring"], secret=True)
156
157
158 for keyring in session.query(Keyring).filter_by(active=True):
159 process_keyring(keyring.keyring_name)
160
161
162
163 for suite in session.query(Suite):
164 suite_dir = os.path.join(
165 suite.archive.path, "dists", suite.suite_name, suite_suffix
166 )
167
168
169 for component in session.query(Component):
170 component_name = component.component_name
171
172 sc_dir = os.path.join(suite_dir, component_name)
173
174 do_dir(sc_dir, "%s/%s" % (suite.suite_name, component_name))
175
176 for arch in suite.architectures:
177 if arch.arch_string == "source":
178 arch_string = "source"
179 else:
180 arch_string = "binary-%s" % arch.arch_string
181
182 suite_arch_dir = os.path.join(sc_dir, arch_string)
183
184 do_dir(
185 suite_arch_dir,
186 "%s/%s/%s" % (suite.suite_name, component_name, arch_string),
187 )
188
189
190
191
192
194 """Initial setup of an archive."""
195
196 global Cnf
197
198 Cnf = utils.get_conf()
199 arguments = [("h", "help", "Init-Dirs::Options::Help")]
200 for i in ["help"]:
201 key = "Init-Dirs::Options::%s" % i
202 if key not in Cnf:
203 Cnf[key] = ""
204
205 arguments = apt_pkg.parse_commandline(Cnf, arguments, sys.argv)
206
207 options = Cnf.subtree("Init-Dirs::Options")
208 if options["Help"]:
209 usage()
210 elif arguments:
211 utils.warn("dak init-dirs takes no arguments.")
212 usage(exit_code=1)
213
214 create_directories()
215
216
217
218
219
220 if __name__ == "__main__":
221 main()
222