3 """Configure dak parameters in the database"""
4 # Copyright (C) 2009 Mark Hymers <mhy@debian.org>
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.
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.
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
20 ################################################################################
26 from daklib import utils
27 from daklib.dbconn import *
29 ################################################################################
34 ################################################################################
36 print >> sys.stderr, msg
38 def die(msg, exit_code=1):
39 print >> sys.stderr, msg
42 def die_arglen(args, args_needed, msg):
43 if len(args) < args_needed:
46 def usage(exit_code=0):
47 """Perform administrative work on the dak database."""
49 print """Usage: dak admin COMMAND
50 Perform administrative work on the dak database.
52 -h, --help show this help and exit.
53 -n, --dry-run don't do anything, just show what would have been done
54 (only applies to add or rm operations).
56 Commands can use a long or abbreviated form:
59 a list show a list of architectures
60 a rm ARCH remove an architecture (will only work if
61 no longer linked to any suites)
62 a add ARCH DESCRIPTION [SUITELIST]
63 add architecture ARCH with DESCRIPTION.
64 If SUITELIST is given, add to each of the
65 suites at the same time
68 s list show a list of suites
69 s show SUITE show config details for a suite
70 s add SUITE VERSION [ label=LABEL ] [ description=DESCRIPTION ]
71 [ origin=ORIGIN ] [ codename=CODENAME ]
72 add suite SUITE, version VERSION. label,
73 description, origin and codename are optional.
75 suite-architecture / s-a:
76 s-a list-suite ARCH show the suites an ARCH is in
77 s-a list-arch SUITE show the architectures in a SUITE
78 s-a add SUITE ARCH add ARCH to suite
79 s-a rm SUITE ARCH remove ARCH from suite (will only work if
80 no packages remain for the arch in the suite)
84 ################################################################################
86 def __architecture_list(d, args):
87 q = d.session().query(Architecture).order_by('arch_string')
89 # HACK: We should get rid of source from the arch table
90 if j.arch_string == 'source': continue
94 def __architecture_add(d, args):
95 die_arglen(args, 3, "E: adding an architecture requires a name and a description")
96 print "Adding architecture %s" % args[2]
97 suites = [str(x) for x in args[4:]]
103 a.arch_string = str(args[2]).lower()
104 a.description = str(args[3])
107 su = get_suite(sn ,s)
109 archsu = SuiteArchitecture()
110 archsu.arch_id = a.arch_id
111 archsu.suite_id = su.suite_id
114 warn("W: Cannot find suite %s" % su)
116 except IntegrityError, e:
117 die("E: Integrity error adding architecture %s (it probably already exists)" % args[2])
118 except SQLAlchemyError, e:
119 die("E: Error adding architecture %s (%s)" % (args[2], e))
120 print "Architecture %s added" % (args[2])
122 def __architecture_rm(d, args):
123 die_arglen(args, 3, "E: removing an architecture requires at least a name")
124 print "Removing architecture %s" % args[2]
128 a = get_architecture(args[2].lower(), s)
130 die("E: Cannot find architecture %s" % args[2])
133 except IntegrityError, e:
134 die("E: Integrity error removing architecture %s (suite-arch entries probably still exist)" % args[2])
135 except SQLAlchemyError, e:
136 die("E: Error removing architecture %s (%s)" % (args[2], e))
137 print "Architecture %s removed" % args[2]
139 def architecture(command):
140 args = [str(x) for x in command]
141 Cnf = utils.get_conf()
144 die_arglen(args, 2, "E: architecture needs at least a command")
146 mode = args[1].lower()
148 __architecture_list(d, args)
150 __architecture_add(d, args)
152 __architecture_rm(d, args)
154 die("E: architecture command unknown")
156 dispatch['architecture'] = architecture
157 dispatch['a'] = architecture
159 ################################################################################
161 def __suite_list(d, args):
163 for j in s.query(Suite).order_by('suite_name').all():
166 def __suite_show(d, args):
168 die("E: showing an suite entry requires a suite")
171 su = get_suite(args[2].lower())
173 die("E: can't find suite entry for %s" % (args[2].lower()))
177 def __suite_add(d, args):
178 die_arglen(args, 4, "E: adding a suite requires at least a name and a version")
179 suite_name = args[2].lower()
183 def get_field(field):
185 if varval.startswith(field + '='):
186 return varval.split('=')[1]
189 print "Adding suite %s" % suite_name
194 suite.suite_name = suite_name
195 suite.version = version
196 suite.label = get_field('label')
197 suite.description = get_field('description')
198 suite.origin = get_field('origin')
199 suite.codename = get_field('codename')
202 except IntegrityError, e:
203 die("E: Integrity error adding suite %s (it probably already exists)" % suite_name)
204 except SQLAlchemyError, e:
205 die("E: Error adding suite %s (%s)" % (suite_name, e))
206 print "Suite %s added" % (suite_name)
209 args = [str(x) for x in command]
210 Cnf = utils.get_conf()
213 die_arglen(args, 2, "E: suite needs at least a command")
215 mode = args[1].lower()
218 __suite_list(d, args)
220 __suite_show(d, args)
224 die("E: suite command unknown")
226 dispatch['suite'] = suite
227 dispatch['s'] = suite
229 ################################################################################
231 def __suite_architecture_list(d, args):
233 for j in s.query(Suite).order_by('suite_name').all():
234 print j.suite_name + ' ' + \
235 ','.join([a.architecture.arch_string for a in j.suitearchitectures])
237 def __suite_architecture_listarch(d, args):
238 die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
239 a = get_suite_architectures(args[2].lower())
241 # HACK: We should get rid of source from the arch table
242 if j.arch_string != 'source':
246 def __suite_architecture_listsuite(d, args):
247 die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
248 for j in get_architecture_suites(args[2].lower()):
252 def __suite_architecture_add(d, args):
254 die("E: adding a suite-architecture entry requires a suite and arch")
258 suite = get_suite(args[2].lower(), s)
259 if suite is None: die("E: Can't find suite %s" % args[2].lower())
261 arch = get_architecture(args[3].lower(), s)
262 if arch is None: die("E: Can't find architecture %s" % args[3].lower())
266 sa = SuiteArchitecture()
267 sa.arch_id = arch.arch_id
268 sa.suite_id = suite.suite_id
271 except IntegrityError, e:
272 die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), args[3].lower()))
273 except SQLAlchemyError, e:
274 die("E: Can't add suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
276 print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
279 def __suite_architecture_rm(d, args):
281 die("E: removing an suite-architecture entry requires a suite and arch")
286 sa = get_suite_architecture(args[2].lower(), args[3].lower(), s)
288 die("E: can't find suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower()))
291 except IntegrityError, e:
292 die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
293 except SQLAlchemyError, e:
294 die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
296 print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
299 def suite_architecture(command):
300 args = [str(x) for x in command]
301 Cnf = utils.get_conf()
304 die_arglen(args, 2, "E: suite-architecture needs at least a command")
306 mode = args[1].lower()
309 __suite_architecture_list(d, args)
310 elif mode == 'list-arch':
311 __suite_architecture_listarch(d, args)
312 elif mode == 'list-suite':
313 __suite_architecture_listsuite(d, args)
315 __suite_architecture_add(d, args)
317 __suite_architecture_rm(d, args)
319 die("E: suite-architecture command unknown")
321 dispatch['suite-architecture'] = suite_architecture
322 dispatch['s-a'] = suite_architecture
324 ################################################################################
327 """Perform administrative work on the dak database"""
329 Cnf = utils.get_conf()
330 arguments = [('h', "help", "Admin::Options::Help"),
331 ('n', "dry-run", "Admin::Options::Dry-Run")]
332 for i in [ "help", "dry-run" ]:
333 if not Cnf.has_key("Admin::Options::%s" % (i)):
334 Cnf["Admin::Options::%s" % (i)] = ""
336 arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
338 options = Cnf.SubTree("Admin::Options")
339 if options["Help"] or len(arguments) < 1:
341 if options["Dry-Run"]:
344 subcommand = str(arguments[0])
346 if subcommand in dispatch.keys():
347 dispatch[subcommand](arguments)
349 die("E: Unknown command")
351 ################################################################################
353 if __name__ == '__main__':