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 *
28 from sqlalchemy.orm.exc import NoResultFound
30 ################################################################################
35 ################################################################################
37 print >> sys.stderr, msg
39 def die(msg, exit_code=1):
40 print >> sys.stderr, msg
43 def die_arglen(args, args_needed, msg):
44 if len(args) < args_needed:
47 def usage(exit_code=0):
48 """Perform administrative work on the dak database."""
50 print """Usage: dak admin COMMAND
51 Perform administrative work on the dak database.
53 -h, --help show this help and exit.
54 -n, --dry-run don't do anything, just show what would have been done
55 (only applies to add or rm operations).
57 Commands can use a long or abbreviated form:
61 c db-shell show db config in a usable form for psql
62 c NAME show option NAME as set in configuration table
65 a list show a list of architectures
66 a rm ARCH remove an architecture (will only work if
67 no longer linked to any suites)
68 a add ARCH DESCRIPTION [SUITELIST]
69 add architecture ARCH with DESCRIPTION.
70 If SUITELIST is given, add to each of the
71 suites at the same time
74 s list show a list of suites
75 s show SUITE show config details for a suite
76 s add SUITE VERSION [ label=LABEL ] [ description=DESCRIPTION ]
77 [ origin=ORIGIN ] [ codename=CODENAME ]
78 add suite SUITE, version VERSION. label,
79 description, origin and codename are optional.
81 suite-architecture / s-a:
82 s-a list show the architectures for all suites
83 s-a list-suite ARCH show the suites an ARCH is in
84 s-a list-arch SUITE show the architectures in a SUITE
85 s-a add SUITE ARCH add ARCH to suite
86 s-a rm SUITE ARCH remove ARCH from suite (will only work if
87 no packages remain for the arch in the suite)
90 v-c list show version checks for all suites
91 v-c list-suite SUITE show version checks for suite SUITE
92 v-c add SUITE CHECK REFERENCE add a version check for suite SUITE
93 v-c rm SUITE CHECK REFERENCE rmove a version check
95 CHECK is one of Enhances, MustBeNewerThan, MustBeOlderThan
96 REFERENCE is another suite name
100 ################################################################################
102 def __architecture_list(d, args):
103 q = d.session().query(Architecture).order_by('arch_string')
105 # HACK: We should get rid of source from the arch table
106 if j.arch_string == 'source': continue
110 def __architecture_add(d, args):
111 die_arglen(args, 4, "E: adding an architecture requires a name and a description")
112 print "Adding architecture %s" % args[2]
113 suites = [str(x) for x in args[4:]]
115 print "Adding to suites %s" % ", ".join(suites)
120 a.arch_string = str(args[2]).lower()
121 a.description = str(args[3])
124 su = get_suite(sn, s)
128 warn("W: Cannot find suite %s" % su)
130 except IntegrityError, e:
131 die("E: Integrity error adding architecture %s (it probably already exists)" % args[2])
132 except SQLAlchemyError, e:
133 die("E: Error adding architecture %s (%s)" % (args[2], e))
134 print "Architecture %s added" % (args[2])
136 def __architecture_rm(d, args):
137 die_arglen(args, 3, "E: removing an architecture requires at least a name")
138 print "Removing architecture %s" % args[2]
142 a = get_architecture(args[2].lower(), s)
144 die("E: Cannot find architecture %s" % args[2])
147 except IntegrityError, e:
148 die("E: Integrity error removing architecture %s (suite-arch entries probably still exist)" % args[2])
149 except SQLAlchemyError, e:
150 die("E: Error removing architecture %s (%s)" % (args[2], e))
151 print "Architecture %s removed" % args[2]
153 def architecture(command):
154 args = [str(x) for x in command]
155 Cnf = utils.get_conf()
158 die_arglen(args, 2, "E: architecture needs at least a command")
160 mode = args[1].lower()
162 __architecture_list(d, args)
164 __architecture_add(d, args)
166 __architecture_rm(d, args)
168 die("E: architecture command unknown")
170 dispatch['architecture'] = architecture
171 dispatch['a'] = architecture
173 ################################################################################
175 def __suite_list(d, args):
177 for j in s.query(Suite).order_by('suite_name').all():
180 def __suite_show(d, args):
182 die("E: showing an suite entry requires a suite")
185 su = get_suite(args[2].lower())
187 die("E: can't find suite entry for %s" % (args[2].lower()))
191 def __suite_add(d, args):
192 die_arglen(args, 4, "E: adding a suite requires at least a name and a version")
193 suite_name = args[2].lower()
197 def get_field(field):
199 if varval.startswith(field + '='):
200 return varval.split('=')[1]
203 print "Adding suite %s" % suite_name
208 suite.suite_name = suite_name
209 suite.version = version
210 suite.label = get_field('label')
211 suite.description = get_field('description')
212 suite.origin = get_field('origin')
213 suite.codename = get_field('codename')
216 except IntegrityError, e:
217 die("E: Integrity error adding suite %s (it probably already exists)" % suite_name)
218 except SQLAlchemyError, e:
219 die("E: Error adding suite %s (%s)" % (suite_name, e))
220 print "Suite %s added" % (suite_name)
223 args = [str(x) for x in command]
224 Cnf = utils.get_conf()
227 die_arglen(args, 2, "E: suite needs at least a command")
229 mode = args[1].lower()
232 __suite_list(d, args)
234 __suite_show(d, args)
238 die("E: suite command unknown")
240 dispatch['suite'] = suite
241 dispatch['s'] = suite
243 ################################################################################
245 def __suite_architecture_list(d, args):
247 for j in s.query(Suite).order_by('suite_name'):
248 architectures = j.get_architectures(skipsrc = True, skipall = True)
249 print j.suite_name + ': ' + \
250 ', '.join([a.arch_string for a in architectures])
252 def __suite_architecture_listarch(d, args):
253 die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
254 suite = get_suite(args[2].lower(), d.session())
256 die('E: suite %s is invalid' % args[2].lower())
257 a = suite.get_architectures(skipsrc = True, skipall = True)
262 def __suite_architecture_listsuite(d, args):
263 die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
264 architecture = get_architecture(args[2].lower(), d.session())
265 if architecture is None:
266 die("E: architecture %s is invalid" % args[2].lower())
267 for j in architecture.suites:
271 def __suite_architecture_add(d, args):
273 die("E: adding a suite-architecture entry requires a suite and arch")
277 suite = get_suite(args[2].lower(), s)
278 if suite is None: die("E: Can't find suite %s" % args[2].lower())
280 arch = get_architecture(args[3].lower(), s)
281 if arch is None: die("E: Can't find architecture %s" % args[3].lower())
285 suite.architectures.append(arch)
287 except IntegrityError, e:
288 die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), args[3].lower()))
289 except SQLAlchemyError, e:
290 die("E: Can't add suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
292 print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
295 def __suite_architecture_rm(d, args):
297 die("E: removing an suite-architecture entry requires a suite and arch")
302 suite_name = args[2].lower()
303 suite = get_suite(suite_name, s)
305 die('E: no such suite %s' % suite_name)
306 arch_string = args[3].lower()
307 architecture = get_architecture(arch_string, s)
308 if architecture not in suite.architectures:
309 die("E: architecture %s not found in suite %s" % (arch_string, suite_name))
310 suite.architectures.remove(architecture)
312 except IntegrityError, e:
313 die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
314 except SQLAlchemyError, e:
315 die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
317 print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
320 def suite_architecture(command):
321 args = [str(x) for x in command]
322 Cnf = utils.get_conf()
325 die_arglen(args, 2, "E: suite-architecture needs at least a command")
327 mode = args[1].lower()
330 __suite_architecture_list(d, args)
331 elif mode == 'list-arch':
332 __suite_architecture_listarch(d, args)
333 elif mode == 'list-suite':
334 __suite_architecture_listsuite(d, args)
336 __suite_architecture_add(d, args)
338 __suite_architecture_rm(d, args)
340 die("E: suite-architecture command unknown")
342 dispatch['suite-architecture'] = suite_architecture
343 dispatch['s-a'] = suite_architecture
345 ################################################################################
347 def __version_check_list(d):
348 session = d.session()
349 for s in session.query(Suite).order_by('suite_name'):
350 __version_check_list_suite(d, s.suite_name)
352 def __version_check_list_suite(d, suite_name):
353 vcs = get_version_checks(suite_name)
355 print "%s %s %s" % (suite_name, vc.check, vc.reference.suite_name)
357 def __version_check_add(d, suite_name, check, reference_name):
358 suite = get_suite(suite_name)
360 die("E: Could not find suite %s." % (suite_name))
361 reference = get_suite(reference_name)
363 die("E: Could not find reference suite %s." % (reference_name))
365 session = d.session()
369 vc.reference = reference
373 def __version_check_rm(d, suite_name, check, reference_name):
374 suite = get_suite(suite_name)
376 die("E: Could not find suite %s." % (suite_name))
377 reference = get_suite(reference_name)
379 die("E: Could not find reference suite %s." % (reference_name))
381 session = d.session()
383 vc = session.query(VersionCheck).filter_by(suite=suite, check=check, reference=reference).one()
386 except NoResultFound:
387 print "W: version-check not found."
389 def version_check(command):
390 args = [str(x) for x in command]
391 Cnf = utils.get_conf()
394 die_arglen(args, 2, "E: version-check needs at least a command")
395 mode = args[1].lower()
398 __version_check_list(d)
399 elif mode == 'list-suite':
401 die("E: version-check list-suite needs a single parameter")
402 __version_check_list_suite(d, args[2])
405 die("E: version-check add needs three parameters")
406 __version_check_add(d, args[2], args[3], args[4])
409 die("E: version-check rm needs three parameters")
410 __version_check_rm(d, args[2], args[3], args[4])
412 die("E: version-check command unknown")
414 dispatch['version-check'] = version_check
415 dispatch['v-c'] = version_check
417 ################################################################################
419 def show_config(command):
420 args = [str(x) for x in command]
421 cnf = utils.get_conf()
423 die_arglen(args, 2, "E: config needs at least a command")
425 mode = args[1].lower()
429 if cnf.has_key("DB::Service"):
431 connstr = "postgresql://service=%s" % cnf["DB::Service"]
432 elif cnf.has_key("DB::Host"):
434 connstr = "postgres://%s" % cnf["DB::Host"]
435 if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
436 connstr += ":%s" % cnf["DB::Port"]
437 connstr += "/%s" % cnf["DB::Name"]
440 connstr = "postgres:///%s" % cnf["DB::Name"]
441 if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
442 connstr += "?port=%s" % cnf["DB::Port"]
444 elif mode == 'db-shell':
446 if cnf.has_key("DB::Service"):
447 e.append('PGSERVICE')
448 print "PGSERVICE=%s" % cnf["DB::Service"]
449 if cnf.has_key("DB::Name"):
450 e.append('PGDATABASE')
451 print "PGDATABASE=%s" % cnf["DB::Name"]
452 if cnf.has_key("DB::Host"):
453 print "PGHOST=%s" % cnf["DB::Host"]
455 if cnf.has_key("DB::Port") and cnf["DB::Port"] != "-1":
456 print "PGPORT=%s" % cnf["DB::Port"]
458 print "export " + " ".join(e)
460 session = DBConn().session()
462 o = session.query(DBConfig).filter_by(name = mode).one()
464 except NoResultFound:
465 print "W: option '%s' not set" % mode
467 dispatch['config'] = show_config
468 dispatch['c'] = show_config
470 ################################################################################
473 """Perform administrative work on the dak database"""
475 Cnf = utils.get_conf()
476 arguments = [('h', "help", "Admin::Options::Help"),
477 ('n', "dry-run", "Admin::Options::Dry-Run")]
478 for i in [ "help", "dry-run" ]:
479 if not Cnf.has_key("Admin::Options::%s" % (i)):
480 Cnf["Admin::Options::%s" % (i)] = ""
482 arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
484 options = Cnf.SubTree("Admin::Options")
485 if options["Help"] or len(arguments) < 1:
487 if options["Dry-Run"]:
490 subcommand = str(arguments[0])
492 if subcommand in dispatch.keys():
493 dispatch[subcommand](arguments)
495 die("E: Unknown command")
497 ################################################################################
499 if __name__ == '__main__':