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