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
71 suite-architecture / s-a:
72 s-a list-suite ARCH show the suites an ARCH is in
73 s-a list-arch SUITE show the architectures in a SUITE
74 s-a add SUITE ARCH add ARCH to suite
75 s-a rm SUITE ARCH remove ARCH from suite (will only work if
76 no packages remain for the arch in the suite)
80 ################################################################################
82 def __architecture_list(d, args):
83 q = d.session().query(Architecture).order_by('arch_string')
85 # HACK: We should get rid of source from the arch table
86 if j.arch_string == 'source': continue
90 def __architecture_add(d, args):
91 die_arglen(args, 3, "E: adding an architecture requires a name and a description")
92 print "Adding architecture %s" % args[2]
93 suites = [str(x) for x in args[4:]]
99 a.arch_string = str(args[2]).lower()
100 a.description = str(args[3])
103 su = get_suite(sn ,s)
105 archsu = SuiteArchitecture()
106 archsu.arch_id = a.arch_id
107 archsu.suite_id = su.suite_id
110 warn("W: Cannot find suite %s" % su)
112 except IntegrityError, e:
113 die("E: Integrity error adding architecture %s (it probably already exists)" % args[2])
114 except SQLAlchemyError, e:
115 die("E: Error adding architecture %s (%s)" % (args[2], e))
116 print "Architecture %s added" % (args[2])
118 def __architecture_rm(d, args):
119 die_arglen(args, 3, "E: removing an architecture requires at least a name")
120 print "Removing architecture %s" % args[2]
124 a = get_architecture(args[2].lower(), s)
126 die("E: Cannot find architecture %s" % args[2])
129 except IntegrityError, e:
130 die("E: Integrity error removing architecture %s (suite-arch entries probably still exist)" % args[2])
131 except SQLAlchemyError, e:
132 die("E: Error removing architecture %s (%s)" % (args[2], e))
133 print "Architecture %s removed" % args[2]
135 def architecture(command):
136 args = [str(x) for x in command]
137 Cnf = utils.get_conf()
140 die_arglen(args, 2, "E: architecture needs at least a command")
142 mode = args[1].lower()
144 __architecture_list(d, args)
146 __architecture_add(d, args)
148 __architecture_rm(d, args)
150 die("E: architecture command unknown")
152 dispatch['architecture'] = architecture
153 dispatch['a'] = architecture
155 ################################################################################
157 def __suite_list(d, args):
159 for j in s.query(Suite).order_by('suite_name').all():
162 def __suite_show(d, args):
164 die("E: showing an suite entry requires a suite")
167 su = get_suite(args[2].lower())
169 die("E: can't find suite entry for %s" % (args[2].lower()))
174 args = [str(x) for x in command]
175 Cnf = utils.get_conf()
178 die_arglen(args, 2, "E: suite needs at least a command")
180 mode = args[1].lower()
183 __suite_list(d, args)
185 __suite_show(d, args)
187 die("E: suite command unknown")
189 dispatch['suite'] = suite
190 dispatch['s'] = suite
192 ################################################################################
194 def __suite_architecture_list(d, args):
196 for j in s.query(Suite).order_by('suite_name').all():
197 print j.suite_name + ' ' + \
198 ','.join([a.architecture.arch_string for a in j.suitearchitectures])
200 def __suite_architecture_listarch(d, args):
201 die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
202 a = get_suite_architectures(args[2].lower())
204 # HACK: We should get rid of source from the arch table
205 if j.arch_string != 'source':
209 def __suite_architecture_listsuite(d, args):
210 die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
211 for j in get_architecture_suites(args[2].lower()):
215 def __suite_architecture_add(d, args):
217 die("E: adding a suite-architecture entry requires a suite and arch")
221 suite = get_suite(args[2].lower(), s)
222 if suite is None: die("E: Can't find suite %s" % args[2].lower())
224 arch = get_architecture(args[3].lower(), s)
225 if arch is None: die("E: Can't find architecture %s" % args[3].lower())
229 sa = SuiteArchitecture()
230 sa.arch_id = arch.arch_id
231 sa.suite_id = suite.suite_id
234 except IntegrityError, e:
235 die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), args[3].lower()))
236 except SQLAlchemyError, e:
237 die("E: Can't add suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
239 print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
242 def __suite_architecture_rm(d, args):
244 die("E: removing an suite-architecture entry requires a suite and arch")
249 sa = get_suite_architecture(args[2].lower(), args[3].lower(), s)
251 die("E: can't find suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower()))
254 except IntegrityError, e:
255 die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
256 except SQLAlchemyError, e:
257 die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
259 print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
262 def suite_architecture(command):
263 args = [str(x) for x in command]
264 Cnf = utils.get_conf()
267 die_arglen(args, 2, "E: suite-architecture needs at least a command")
269 mode = args[1].lower()
272 __suite_architecture_list(d, args)
273 elif mode == 'list-arch':
274 __suite_architecture_listarch(d, args)
275 elif mode == 'list-suite':
276 __suite_architecture_listsuite(d, args)
278 __suite_architecture_add(d, args)
280 __suite_architecture_rm(d, args)
282 die("E: suite-architecture command unknown")
284 dispatch['suite-architecture'] = suite_architecture
285 dispatch['s-a'] = suite_architecture
287 ################################################################################
290 """Perform administrative work on the dak database"""
292 Cnf = utils.get_conf()
293 arguments = [('h', "help", "Admin::Options::Help"),
294 ('n', "dry-run", "Admin::Options::Dry-Run")]
295 for i in [ "help", "dry-run" ]:
296 if not Cnf.has_key("Admin::Options::%s" % (i)):
297 Cnf["Admin::Options::%s" % (i)] = ""
299 arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
301 options = Cnf.SubTree("Admin::Options")
302 if options["Help"] or len(arguments) < 1:
304 if options["Dry-Run"]:
307 subcommand = str(arguments[0])
309 if subcommand in dispatch.keys():
310 dispatch[subcommand](arguments)
312 die("E: Unknown command")
314 ################################################################################
316 if __name__ == '__main__':