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 daklib.config import Config
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:
60 a list show a list of architectures
61 a rm ARCH remove an architecture (will only work if
62 no longer linked to any suites)
63 a add ARCH DESCRIPTION [SUITELIST]
64 add architecture ARCH with DESCRIPTION.
65 If SUITELIST is given, add to each of the
66 suites at the same time
69 s list show a list of suites
70 s show SUITE show config details for a suite
72 suite-architecture / s-a:
73 s-a list-suite ARCH show the suites an ARCH is in
74 s-a list-arch SUITE show the architectures in a SUITE
75 s-a add SUITE ARCH add ARCH to suite
76 s-a rm SUITE ARCH remove ARCH from suite (will only work if
77 no packages remain for the arch in the suite)
81 ################################################################################
83 def __architecture_list(d, args):
84 q = d.session().query(Architecture).order_by('arch_string')
86 # HACK: We should get rid of source from the arch table
87 if j.arch_string == 'source': continue
91 def __architecture_add(d, args):
92 die_arglen(args, 3, "E: adding an architecture requires a name and a description")
93 print "Adding architecture %s" % args[2]
94 suites = [str(x) for x in args[4:]]
100 a.arch_string = str(args[2]).lower()
101 a.description = str(args[3])
104 su = get_suite(sn ,s)
106 archsu = SuiteArchitecture()
107 archsu.arch_id = a.arch_id
108 archsu.suite_id = su.suite_id
111 warn("W: Cannot find suite %s" % su)
113 except IntegrityError, e:
114 die("E: Integrity error adding architecture %s (it probably already exists)" % args[2])
115 except SQLAlchemyError, e:
116 die("E: Error adding architecture %s (%s)" % (args[2], e))
117 print "Architecture %s added" % (args[2])
119 def __architecture_rm(d, args):
120 die_arglen(args, 3, "E: removing an architecture requires at least a name")
121 print "Removing architecture %s" % args[2]
125 a = get_architecture(args[2].lower(), s)
127 die("E: Cannot find architecture %s" % args[2])
130 except IntegrityError, e:
131 die("E: Integrity error removing architecture %s (suite-arch entries probably still exist)" % args[2])
132 except SQLAlchemyError, e:
133 die("E: Error removing architecture %s (%s)" % (args[2], e))
134 print "Architecture %s removed" % args[2]
136 def architecture(command):
137 args = [str(x) for x in command]
138 Cnf = utils.get_conf()
141 die_arglen(args, 2, "E: architecture needs at least a command")
143 mode = args[1].lower()
145 __architecture_list(d, args)
147 __architecture_add(d, args)
149 __architecture_rm(d, args)
151 die("E: architecture command unknown")
153 dispatch['architecture'] = architecture
154 dispatch['a'] = architecture
156 ################################################################################
158 def __suite_list(d, args):
160 for j in s.query(Suite).order_by('suite_name').all():
163 def __suite_show(d, args):
165 die("E: showing an suite entry requires a suite")
168 su = get_suite(args[2].lower())
170 die("E: can't find suite entry for %s" % (args[2].lower()))
175 args = [str(x) for x in command]
176 Cnf = utils.get_conf()
179 die_arglen(args, 2, "E: suite needs at least a command")
181 mode = args[1].lower()
184 __suite_list(d, args)
186 __suite_show(d, args)
188 die("E: suite command unknown")
190 dispatch['suite'] = suite
191 dispatch['s'] = suite
193 ################################################################################
195 def __suite_architecture_list(d, args):
197 for j in s.query(Suite).order_by('suite_name').all():
198 print j.suite_name + ' ' + \
199 ','.join([a.architecture.arch_string for a in j.suitearchitectures])
201 def __suite_architecture_listarch(d, args):
202 die_arglen(args, 3, "E: suite-architecture list-arch requires a suite")
203 a = get_suite_architectures(args[2].lower())
205 # HACK: We should get rid of source from the arch table
206 if j.arch_string != 'source':
210 def __suite_architecture_listsuite(d, args):
211 die_arglen(args, 3, "E: suite-architecture list-suite requires an arch")
212 for j in get_architecture_suites(args[2].lower()):
216 def __suite_architecture_add(d, args):
218 die("E: adding a suite-architecture entry requires a suite and arch")
222 suite = get_suite(args[2].lower(), s)
223 if suite is None: die("E: Can't find suite %s" % args[2].lower())
225 arch = get_architecture(args[3].lower(), s)
226 if arch is None: die("E: Can't find architecture %s" % args[3].lower())
230 sa = SuiteArchitecture()
231 sa.arch_id = arch.arch_id
232 sa.suite_id = suite.suite_id
235 except IntegrityError, e:
236 die("E: Can't add suite-architecture entry (%s, %s) - probably already exists" % (args[2].lower(), args[3].lower()))
237 except SQLAlchemyError, e:
238 die("E: Can't add suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
240 print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
243 def __suite_architecture_rm(d, args):
245 die("E: removing an suite-architecture entry requires a suite and arch")
250 sa = get_suite_architecture(args[2].lower(), args[3].lower(), s)
252 die("E: can't find suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower()))
255 except IntegrityError, e:
256 die("E: Can't remove suite-architecture entry (%s, %s) - it's probably referenced" % (args[2].lower(), args[3].lower()))
257 except SQLAlchemyError, e:
258 die("E: Can't remove suite-architecture entry (%s, %s) - %s" % (args[2].lower(), args[3].lower(), e))
260 print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
263 def suite_architecture(command):
264 args = [str(x) for x in command]
265 Cnf = utils.get_conf()
268 die_arglen(args, 2, "E: suite-architecture needs at least a command")
270 mode = args[1].lower()
273 __suite_architecture_list(d, args)
274 elif mode == 'list-arch':
275 __suite_architecture_listarch(d, args)
276 elif mode == 'list-suite':
277 __suite_architecture_listsuite(d, args)
279 __suite_architecture_add(d, args)
281 __suite_architecture_rm(d, args)
283 die("E: suite-architecture command unknown")
285 dispatch['suite-architecture'] = suite_architecture
286 dispatch['s-a'] = suite_architecture
288 ################################################################################
291 """Perform administrative work on the dak database"""
293 Cnf = utils.get_conf()
294 arguments = [('h', "help", "Admin::Options::Help"),
295 ('n', "dry-run", "Admin::Options::Dry-Run")]
296 for i in [ "help", "dry-run" ]:
297 if not Cnf.has_key("Admin::Options::%s" % (i)):
298 Cnf["Admin::Options::%s" % (i)] = ""
300 arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
302 options = Cnf.SubTree("Admin::Options")
303 if options["Help"] or len(arguments) < 1:
305 if options["Dry-Run"]:
308 subcommand = str(arguments[0])
310 if subcommand in dispatch.keys():
311 dispatch[subcommand](arguments)
313 die("E: Unknown command")
315 ################################################################################
317 if __name__ == '__main__':