]> git.decadent.org.uk Git - dak.git/blob - dak/init_dirs.py
Debug suites might also miss the source package
[dak.git] / dak / init_dirs.py
1 #!/usr/bin/env python
2
3 """Initial setup of an archive."""
4 # Copyright (C) 2002, 2004, 2006  James Troup <james@nocrew.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21
22 import os, sys
23 import apt_pkg
24 from daklib import utils
25 from daklib.dbconn import *
26
27 ################################################################################
28
29 Cnf = None
30
31 ################################################################################
32
33 def usage(exit_code=0):
34     """Print a usage message and exit with 'exit_code'."""
35
36     print """Usage: dak init-dirs
37 Creates directories for an archive based on dak.conf configuration file.
38
39   -h, --help                show this help and exit."""
40     sys.exit(exit_code)
41
42 ################################################################################
43
44 def do_dir(target, config_name):
45     """If 'target' exists, make sure it is a directory.  If it doesn't, create
46 it."""
47
48     if os.path.exists(target):
49         if not os.path.isdir(target):
50             utils.fubar("%s (%s) is not a directory."
51                                % (target, config_name))
52     else:
53         print "Creating %s ..." % (target)
54         os.makedirs(target)
55
56 def process_file(config, config_name):
57     """Create directories for a config entry that's a filename."""
58
59     if config.has_key(config_name):
60         target = os.path.dirname(config[config_name])
61         do_dir(target, config_name)
62
63 def process_tree(config, tree):
64     """Create directories for a config tree."""
65
66     for entry in config.subtree(tree).list():
67         entry = entry.lower()
68         config_name = "%s::%s" % (tree, entry)
69         target = config[config_name]
70         do_dir(target, config_name)
71
72 def process_morguesubdir(subdir):
73     """Create directories for morgue sub directories."""
74
75     config_name = "%s::MorgueSubDir" % (subdir)
76     if Cnf.has_key(config_name):
77         target = os.path.join(Cnf["Dir::Morgue"], Cnf[config_name])
78         do_dir(target, config_name)
79
80 def process_keyring(fullpath, secret=False):
81     """Create empty keyring if necessary."""
82
83     if os.path.exists(fullpath):
84         return
85
86     keydir = os.path.dirname(fullpath)
87
88     if not os.path.isdir(keydir):
89         print "Creating %s ..." % (keydir)
90         os.makedirs(keydir)
91         if secret:
92             # Make sure secret keyring directories are 0700
93             os.chmod(keydir, 0o700)
94
95     # Touch the file
96     print "Creating %s ..." % (fullpath)
97     file(fullpath, 'w')
98     if secret:
99         os.chmod(fullpath, 0o600)
100     else:
101         os.chmod(fullpath, 0o644)
102
103 ######################################################################
104
105 def create_directories():
106     """Create directories referenced in dak.conf."""
107
108     session = DBConn().session()
109
110     # Process directories from dak.conf
111     process_tree(Cnf, "Dir")
112
113     # Hardcode creation of the unchecked directory
114     if Cnf.has_key("Dir::Base"):
115         do_dir(os.path.join(Cnf["Dir::Base"], "queue", "unchecked"), 'unchecked directory')
116
117     # Process queue directories
118     for queue in session.query(PolicyQueue):
119         do_dir(queue.path, '%s queue' % queue.queue_name)
120         # If we're doing the NEW queue, make sure it has a COMMENTS directory
121         if queue.queue_name == 'new':
122             do_dir(os.path.join(queue.path, "COMMENTS"), '%s queue comments' % queue.queue_name)
123
124     for config_name in [ "Rm::LogFile",
125                          "Import-Archive::ExportDir" ]:
126         process_file(Cnf, config_name)
127
128     for subdir in [ "Clean-Queues", "Clean-Suites" ]:
129         process_morguesubdir(subdir)
130
131     suite_suffix = "%s" % (Cnf.find("Dinstall::SuiteSuffix"))
132
133     # Process secret keyrings
134     if Cnf.has_key('Dinstall::SigningKeyring'):
135         process_keyring(Cnf['Dinstall::SigningKeyring'], secret=True)
136
137     if Cnf.has_key('Dinstall::SigningPubKeyring'):
138         process_keyring(Cnf['Dinstall::SigningPubKeyring'], secret=True)
139
140     # Process public keyrings
141     for keyring in session.query(Keyring).filter_by(active=True):
142         process_keyring(keyring.keyring_name)
143
144     # Process dists directories
145     # TODO: Store location of each suite in database
146     for suite in session.query(Suite):
147         suite_dir = os.path.join(suite.archive.path, 'dists', suite.suite_name, suite_suffix)
148
149         # TODO: Store valid suite/component mappings in database
150         for component in session.query(Component):
151             component_name = component.component_name
152
153             sc_dir = os.path.join(suite_dir, component_name)
154
155             do_dir(sc_dir, "%s/%s" % (suite.suite_name, component_name))
156
157             for arch in suite.architectures:
158                 if arch.arch_string == 'source':
159                     arch_string = 'source'
160                 else:
161                     arch_string = 'binary-%s' % arch.arch_string
162
163                 suite_arch_dir = os.path.join(sc_dir, arch_string)
164
165                 do_dir(suite_arch_dir, "%s/%s/%s" % (suite.suite_name, component_name, arch_string))
166
167 ################################################################################
168
169 def main ():
170     """Initial setup of an archive."""
171
172     global Cnf
173
174     Cnf = utils.get_conf()
175     arguments = [('h', "help", "Init-Dirs::Options::Help")]
176     for i in [ "help" ]:
177         if not Cnf.has_key("Init-Dirs::Options::%s" % (i)):
178             Cnf["Init-Dirs::Options::%s" % (i)] = ""
179
180     d = DBConn()
181
182     arguments = apt_pkg.parse_commandline(Cnf, arguments, sys.argv)
183
184     options = Cnf.subtree("Init-Dirs::Options")
185     if options["Help"]:
186         usage()
187     elif arguments:
188         utils.warn("dak init-dirs takes no arguments.")
189         usage(exit_code=1)
190
191     create_directories()
192
193 ################################################################################
194
195 if __name__ == '__main__':
196     main()