]> git.decadent.org.uk Git - dak.git/blob - dak/admin.py
Merge commit 'ftpmaster/master' into sqlalchemy
[dak.git] / dak / admin.py
1 #!/usr/bin/env python
2
3 """Configure dak parameters in the database"""
4 # Copyright (C) 2009  Mark Hymers <mhy@debian.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 sys
23
24 import apt_pkg
25
26 from daklib import utils
27 from daklib.dbconn import *
28 from daklib.config import Config
29
30 ################################################################################
31
32 dispatch = {}
33 dryrun = False
34
35 ################################################################################
36 def warn(msg):
37     print >> sys.stderr, msg
38
39 def die(msg, exit_code=1):
40     print >> sys.stderr, msg
41     sys.exit(exit_code)
42
43 def die_arglen(args, args_needed, msg):
44     if len(args) < args_needed:
45         die(msg)
46
47 def usage(exit_code=0):
48     """Perform administrative work on the dak database."""
49
50     print """Usage: dak admin COMMAND
51 Perform administrative work on the dak database.
52
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).
56
57   Commands can use a long or abbreviated form:
58
59   architecture / a:
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
67
68   suite / s:
69      s list                 show a list of suites
70      s show SUITE           show config details for a suite
71
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)
78 """
79     sys.exit(exit_code)
80
81 ################################################################################
82
83 def __architecture_list(d, args):
84     q = d.session().query(Architecture).order_by('arch_string')
85     for j in q.all():
86         # HACK: We should get rid of source from the arch table
87         if j.arch_string == 'source': continue
88         print j.arch_string
89     sys.exit(0)
90
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:]]
95     print suites
96     if not dryrun:
97         try:
98             s = d.session()
99             a = Architecture()
100             a.arch_string = str(args[2]).lower()
101             a.description = str(args[3])
102             s.add(a)
103             for sn in suites:
104                 su = get_suite(sn ,s)
105                 if su is not None:
106                     archsu = SuiteArchitecture()
107                     archsu.arch_id = a.arch_id
108                     archsu.suite_id = su.suite_id
109                     s.add(archsu)
110                 else:
111                     warn("W: Cannot find suite %s" % su)
112             s.commit()
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])
118
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]
122     if not dryrun:
123         try:
124             s = d.session()
125             a = get_architecture(args[2].lower(), s)
126             if a is None:
127                 die("E: Cannot find architecture %s" % args[2])
128             s.delete(a)
129             s.commit()
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]
135
136 def architecture(command):
137     args = [str(x) for x in command]
138     Cnf = utils.get_conf()
139     d = DBConn()
140
141     die_arglen(args, 2, "E: architecture needs at least a command")
142
143     mode = args[1].lower()
144     if mode == 'list':
145         __architecture_list(d, args)
146     elif mode == 'add':
147         __architecture_add(d, args)
148     elif mode == 'rm':
149         __architecture_rm(d, args)
150     else:
151         die("E: architecture command unknown")
152
153 dispatch['architecture'] = architecture
154 dispatch['a'] = architecture
155
156 ################################################################################
157
158 def __suite_list(d, args):
159     s = d.session()
160     for j in s.query(Suite).order_by('suite_name').all():
161         print j.suite_name
162
163 def __suite_show(d, args):
164     if len(args) < 2:
165         die("E: showing an suite entry requires a suite")
166
167     s = d.session()
168     su = get_suite(args[2].lower())
169     if su is None:
170         die("E: can't find suite entry for %s" % (args[2].lower()))
171
172     print su.details()
173
174 def suite(command):
175     args = [str(x) for x in command]
176     Cnf = utils.get_conf()
177     d = DBConn()
178
179     die_arglen(args, 2, "E: suite needs at least a command")
180
181     mode = args[1].lower()
182
183     if mode == 'list':
184         __suite_list(d, args)
185     elif mode == 'show':
186         __suite_show(d, args)
187     else:
188         die("E: suite command unknown")
189
190 dispatch['suite'] = suite
191 dispatch['s'] = suite
192
193 ################################################################################
194
195 def __suite_architecture_list(d, args):
196     s = d.session()
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])
200
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())
204     for j in a:
205         # HACK: We should get rid of source from the arch table
206         if j.arch_string != 'source':
207             print j.arch_string
208
209
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()):
213         print j.suite_name
214
215
216 def __suite_architecture_add(d, args):
217     if len(args) < 3:
218         die("E: adding a suite-architecture entry requires a suite and arch")
219
220     s = d.session()
221
222     suite = get_suite(args[2].lower(), s)
223     if suite is None: die("E: Can't find suite %s" % args[2].lower())
224
225     arch = get_architecture(args[3].lower(), s)
226     if arch is None: die("E: Can't find architecture %s" % args[3].lower())
227
228     if not dryrun:
229         try:
230             sa = SuiteArchitecture()
231             sa.arch_id = arch.arch_id
232             sa.suite_id = suite.suite_id
233             s.add(sa)
234             s.commit()
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))
239
240     print "Added suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
241
242
243 def __suite_architecture_rm(d, args):
244     if len(args) < 3:
245         die("E: removing an suite-architecture entry requires a suite and arch")
246
247     s = d.session()
248     if not dryrun:
249         try:
250             sa = get_suite_architecture(args[2].lower(), args[3].lower(), s)
251             if sa is None:
252                 die("E: can't find suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower()))
253             s.delete(sa)
254             s.commit()
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))
259
260     print "Removed suite-architecture entry for %s, %s" % (args[2].lower(), args[3].lower())
261
262
263 def suite_architecture(command):
264     args = [str(x) for x in command]
265     Cnf = utils.get_conf()
266     d = DBConn()
267
268     die_arglen(args, 2, "E: suite-architecture needs at least a command")
269
270     mode = args[1].lower()
271
272     if mode == 'list':
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)
278     elif mode == 'add':
279         __suite_architecture_add(d, args)
280     elif mode == 'rm':
281         __suite_architecture_rm(d, args)
282     else:
283         die("E: suite-architecture command unknown")
284
285 dispatch['suite-architecture'] = suite_architecture
286 dispatch['s-a'] = suite_architecture
287
288 ################################################################################
289
290 def main():
291     """Perform administrative work on the dak database"""
292     global dryrun
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)] = ""
299
300     arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv)
301
302     options = Cnf.SubTree("Admin::Options")
303     if options["Help"] or len(arguments) < 1:
304         usage()
305     if options["Dry-Run"]:
306         dryrun = True
307
308     subcommand = str(arguments[0])
309
310     if subcommand in dispatch.keys():
311         dispatch[subcommand](arguments)
312     else:
313         die("E: Unknown command")
314
315 ################################################################################
316
317 if __name__ == '__main__':
318     main()