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