]> git.decadent.org.uk Git - dak.git/blob - dak/control_suite.py
stop using deprecated python-apt functions
[dak.git] / dak / control_suite.py
1 #!/usr/bin/env python
2
3 """ Manipulate suite tags """
4 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006  James Troup <james@nocrew.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 # 8to6Guy: "Wow, Bob, You look rough!"
23 # BTAF: "Mbblpmn..."
24 # BTAF <.oO>: "You moron! This is what you get for staying up all night drinking vodka and salad dressing!"
25 # BTAF <.oO>: "This coffee I.V. drip is barely even keeping me awake! I need something with more kick! But what?"
26 # BTAF: "OMIGOD! I OVERDOSED ON HEROIN"
27 # CoWorker#n: "Give him air!!"
28 # CoWorker#n+1: "We need a syringe full of adrenaline!"
29 # CoWorker#n+2: "Stab him in the heart!"
30 # BTAF: "*YES!*"
31 # CoWorker#n+3: "Bob's been overdosing quite a bit lately..."
32 # CoWorker#n+4: "Third time this week."
33
34 # -- http://www.angryflower.com/8to6.gif
35
36 #######################################################################################
37
38 # Adds or removes packages from a suite.  Takes the list of files
39 # either from stdin or as a command line argument.  Special action
40 # "set", will reset the suite (!) and add all packages from scratch.
41
42 #######################################################################################
43
44 import sys
45 import apt_pkg
46 import os
47
48 from daklib.config import Config
49 from daklib.dbconn import *
50 from daklib import daklog
51 from daklib import utils
52 from daklib.queue import get_suite_version_by_package, get_suite_version_by_source
53
54 #######################################################################################
55
56 Logger = None
57
58 ################################################################################
59
60 def usage (exit_code=0):
61     print """Usage: dak control-suite [OPTIONS] [FILE]
62 Display or alter the contents of a suite using FILE(s), or stdin.
63
64   -a, --add=SUITE            add to SUITE
65   -h, --help                 show this help and exit
66   -l, --list=SUITE           list the contents of SUITE
67   -r, --remove=SUITE         remove from SUITE
68   -s, --set=SUITE            set SUITE
69   -b, --britney              generate changelog entry for britney runs"""
70
71     sys.exit(exit_code)
72
73 #######################################################################################
74
75 def get_id(package, version, architecture, session):
76     if architecture == "source":
77         q = session.execute("SELECT id FROM source WHERE source = :package AND version = :version",
78                             {'package': package, 'version': version})
79     else:
80         q = session.execute("""SELECT b.id FROM binaries b, architecture a
81                                 WHERE b.package = :package AND b.version = :version
82                                   AND (a.arch_string = :arch OR a.arch_string = 'all')
83                                   AND b.architecture = a.id""",
84                                {'package': package, 'version': version, 'arch': architecture})
85
86     ql = q.fetchall()
87     if len(ql) < 1:
88         utils.warn("Couldn't find '%s_%s_%s'." % (package, version, architecture))
89         return None
90
91     if len(ql) > 1:
92         utils.warn("Found more than one match for '%s_%s_%s'." % (package, version, architecture))
93         return None
94
95     return ql[0][0]
96
97 #######################################################################################
98
99 def britney_changelog(packages, suite, session):
100
101     old = {}
102     current = {}
103     Cnf = utils.get_conf()
104
105     try:
106         q = session.execute("SELECT changelog FROM suite WHERE id = :suiteid", \
107                             {'suiteid': suite.suite_id})
108         brit_file = q.fetchone()[0]
109     except:
110         brit_file = None
111
112     if brit_file:
113         brit_file = os.path.join(Cnf['Dir::Root'], brit_file)
114     else:
115         return
116
117     q = session.execute("""SELECT s.source, s.version, sa.id
118                              FROM source s, src_associations sa
119                             WHERE sa.suite = :suiteid
120                               AND sa.source = s.id""", {'suiteid': suite.suite_id})
121
122     for p in q.fetchall():
123         current[p[0]] = p[1]
124     for p in packages.keys():
125         if p[2] == "source":
126             old[p[0]] = p[1]
127
128     new = {}
129     for p in current.keys():
130         if p in old.keys():
131             if apt_pkg.version_compare(current[p], old[p]) > 0:
132                 new[p] = [current[p], old[p]]
133         else:
134             new[p] = [current[p], 0]
135
136     query =  "SELECT source, changelog FROM changelogs WHERE"
137     for p in new.keys():
138         query += " source = '%s' AND version > '%s' AND version <= '%s'" \
139                  % (p, new[p][1], new[p][0])
140         query += " AND architecture LIKE '%source%' AND distribution in \
141                   ('unstable', 'experimental', 'testing-proposed-updates') OR"
142     query += " False ORDER BY source, version DESC"
143     q = session.execute(query)
144
145     pu = None
146     brit = utils.open_file(brit_file, 'w')
147
148     for u in q:
149         if pu and pu != u[0]:
150             brit.write("\n")
151         brit.write("%s\n" % u[1])
152         pu = u[0]
153     if q.rowcount: brit.write("\n\n\n")
154
155     for p in list(set(old.keys()).difference(current.keys())):
156         brit.write("REMOVED: %s %s\n" % (p, old[p]))
157
158     brit.flush()
159     brit.close()
160
161 #######################################################################################
162
163 def version_checks(package, architecture, target_suite, new_version, session, force = False):
164     if architecture == "source":
165         suite_version_list = get_suite_version_by_source(package, session)
166     else:
167         suite_version_list = get_suite_version_by_package(package, architecture, session)
168
169     must_be_newer_than = [ vc.reference.suite_name for vc in get_version_checks(target_suite, "MustBeNewerThan") ]
170     must_be_older_than = [ vc.reference.suite_name for vc in get_version_checks(target_suite, "MustBeOlderThan") ]
171
172     # Must be newer than an existing version in target_suite
173     if target_suite not in must_be_newer_than:
174         must_be_newer_than.append(target_suite)
175
176     violations = False
177
178     for suite, version in suite_version_list:
179         cmp = apt_pkg.version_compare(new_version, version)
180         if suite in must_be_newer_than and cmp < 1:
181             utils.warn("%s (%s): version check violated: %s targeted at %s is *not* newer than %s in %s" % (package, architecture, new_version, target_suite, version, suite))
182             violations = True
183         if suite in must_be_older_than and cmp > 1:
184             utils.warn("%s (%s): version check violated: %s targeted at %s is *not* older than %s in %s" % (package, architecture, new_version, target_suite, version, suite))
185             violations = True
186
187     if violations:
188         if force:
189             utils.warn("Continuing anyway (forced)...")
190         else:
191             utils.fubar("Aborting. Version checks violated and not forced.")
192
193 #######################################################################################
194
195 def cmp_package_version(a, b):
196     """
197     comparison function for tuples of the form (package-name, version ...)
198     """
199     cmp_package = cmp(a[0], b[0])
200     if cmp_package != 0:
201         return cmp_package
202     return apt_pkg.version_compare(a[1], b[1])
203
204 #######################################################################################
205
206 def set_suite(file, suite, session, britney=False, force=False):
207     suite_id = suite.suite_id
208     lines = file.readlines()
209
210     # Our session is already in a transaction
211
212     # Build up a dictionary of what is currently in the suite
213     current = {}
214     q = session.execute("""SELECT b.package, b.version, a.arch_string, ba.id
215                              FROM binaries b, bin_associations ba, architecture a
216                             WHERE ba.suite = :suiteid
217                               AND ba.bin = b.id AND b.architecture = a.id""", {'suiteid': suite_id})
218     for i in q:
219         key = i[:3]
220         current[key] = i[3]
221
222     q = session.execute("""SELECT s.source, s.version, 'source', sa.id
223                              FROM source s, src_associations sa
224                             WHERE sa.suite = :suiteid
225                               AND sa.source = s.id""", {'suiteid': suite_id})
226     for i in q:
227         key = i[:3]
228         current[key] = i[3]
229
230     # Build up a dictionary of what should be in the suite
231     desired = set()
232     for line in lines:
233         split_line = line.strip().split()
234         if len(split_line) != 3:
235             utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
236             continue
237         desired.add(tuple(split_line))
238
239     # Check to see which packages need added and add them
240     for key in sorted(desired, cmp=cmp_package_version):
241         if key not in current:
242             (package, version, architecture) = key
243             version_checks(package, architecture, suite.suite_name, version, session, force)
244             pkid = get_id (package, version, architecture, session)
245             if not pkid:
246                 continue
247             if architecture == "source":
248                 session.execute("""INSERT INTO src_associations (suite, source)
249                                         VALUES (:suiteid, :pkid)""", {'suiteid': suite_id, 'pkid': pkid})
250             else:
251                 session.execute("""INSERT INTO bin_associations (suite, bin)
252                                         VALUES (:suiteid, :pkid)""", {'suiteid': suite_id, 'pkid': pkid})
253             Logger.log(["added", " ".join(key), pkid])
254
255     # Check to see which packages need removed and remove them
256     for key, pkid in current.iteritems():
257         if key not in desired:
258             (package, version, architecture) = key
259             if architecture == "source":
260                 session.execute("""DELETE FROM src_associations WHERE id = :pkid""", {'pkid': pkid})
261             else:
262                 session.execute("""DELETE FROM bin_associations WHERE id = :pkid""", {'pkid': pkid})
263             Logger.log(["removed", " ".join(key), pkid])
264
265     session.commit()
266
267     if britney:
268         britney_changelog(current, suite, session)
269
270 #######################################################################################
271
272 def process_file(file, suite, action, session, britney=False, force=False):
273     if action == "set":
274         set_suite(file, suite, session, britney, force)
275         return
276
277     suite_id = suite.suite_id
278
279     request = []
280
281     # Our session is already in a transaction
282     for line in file:
283         split_line = line.strip().split()
284         if len(split_line) != 3:
285             utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
286             continue
287         request.append(split_line)
288
289     request.sort(cmp=cmp_package_version)
290
291     for package, version, architecture in request:
292         pkid = get_id(package, version, architecture, session)
293         if not pkid:
294             continue
295
296         # Do version checks when adding packages
297         if action == "add":
298             version_checks(package, architecture, suite.suite_name, version, session, force)
299
300         if architecture == "source":
301             # Find the existing association ID, if any
302             q = session.execute("""SELECT id FROM src_associations
303                                     WHERE suite = :suiteid and source = :pkid""",
304                                     {'suiteid': suite_id, 'pkid': pkid})
305             ql = q.fetchall()
306             if len(ql) < 1:
307                 association_id = None
308             else:
309                 association_id = ql[0][0]
310
311             # Take action
312             if action == "add":
313                 if association_id:
314                     utils.warn("'%s_%s_%s' already exists in suite %s." % (package, version, architecture, suite))
315                     continue
316                 else:
317                     session.execute("""INSERT INTO src_associations (suite, source)
318                                             VALUES (:suiteid, :pkid)""",
319                                        {'suiteid': suite_id, 'pkid': pkid})
320                     Logger.log(["added", package, version, architecture, suite.suite_name, pkid])
321
322             elif action == "remove":
323                 if association_id == None:
324                     utils.warn("'%s_%s_%s' doesn't exist in suite %s." % (package, version, architecture, suite))
325                     continue
326                 else:
327                     session.execute("""DELETE FROM src_associations WHERE id = :pkid""", {'pkid': association_id})
328                     Logger.log(["removed", package, version, architecture, suite.suite_name, pkid])
329         else:
330             # Find the existing associations ID, if any
331             q = session.execute("""SELECT id FROM bin_associations
332                                     WHERE suite = :suiteid and bin = :pkid""",
333                                     {'suiteid': suite_id, 'pkid': pkid})
334             ql = q.fetchall()
335             if len(ql) < 1:
336                 association_id = None
337             else:
338                 association_id = ql[0][0]
339
340             # Take action
341             if action == "add":
342                 if association_id:
343                     utils.warn("'%s_%s_%s' already exists in suite %s." % (package, version, architecture, suite))
344                     continue
345                 else:
346                     session.execute("""INSERT INTO bin_associations (suite, bin)
347                                             VALUES (:suiteid, :pkid)""",
348                                        {'suiteid': suite_id, 'pkid': pkid})
349                     Logger.log(["added", package, version, architecture, suite.suite_name, pkid])
350             elif action == "remove":
351                 if association_id == None:
352                     utils.warn("'%s_%s_%s' doesn't exist in suite %s." % (package, version, architecture, suite))
353                     continue
354                 else:
355                     session.execute("""DELETE FROM bin_associations WHERE id = :pkid""", {'pkid': association_id})
356                     Logger.log(["removed", package, version, architecture, suite.suite_name, pkid])
357
358     session.commit()
359
360 #######################################################################################
361
362 def get_list(suite, session):
363     suite_id = suite.suite_id
364     # List binaries
365     q = session.execute("""SELECT b.package, b.version, a.arch_string
366                              FROM binaries b, bin_associations ba, architecture a
367                             WHERE ba.suite = :suiteid
368                               AND ba.bin = b.id AND b.architecture = a.id""", {'suiteid': suite_id})
369     for i in q.fetchall():
370         print " ".join(i)
371
372     # List source
373     q = session.execute("""SELECT s.source, s.version
374                              FROM source s, src_associations sa
375                             WHERE sa.suite = :suiteid
376                               AND sa.source = s.id""", {'suiteid': suite_id})
377     for i in q.fetchall():
378         print " ".join(i) + " source"
379
380 #######################################################################################
381
382 def main ():
383     global Logger
384
385     cnf = Config()
386
387     Arguments = [('a',"add","Control-Suite::Options::Add", "HasArg"),
388                  ('b',"britney","Control-Suite::Options::Britney"),
389                  ('f','force','Control-Suite::Options::Force'),
390                  ('h',"help","Control-Suite::Options::Help"),
391                  ('l',"list","Control-Suite::Options::List","HasArg"),
392                  ('r',"remove", "Control-Suite::Options::Remove", "HasArg"),
393                  ('s',"set", "Control-Suite::Options::Set", "HasArg")]
394
395     for i in ["add", "britney", "help", "list", "remove", "set", "version" ]:
396         if not cnf.has_key("Control-Suite::Options::%s" % (i)):
397             cnf["Control-Suite::Options::%s" % (i)] = ""
398
399     try:
400         file_list = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv);
401     except SystemError as e:
402         print "%s\n" % e
403         usage(1)
404     Options = cnf.subtree("Control-Suite::Options")
405
406     if Options["Help"]:
407         usage()
408
409     session = DBConn().session()
410
411     force = Options.has_key("Force") and Options["Force"]
412
413     action = None
414
415     for i in ("add", "list", "remove", "set"):
416         if cnf["Control-Suite::Options::%s" % (i)] != "":
417             suite_name = cnf["Control-Suite::Options::%s" % (i)]
418             suite = get_suite(suite_name, session=session)
419             if suite is None:
420                 utils.fubar("Unknown suite '%s'." % (suite_name))
421             else:
422                 if action:
423                     utils.fubar("Can only perform one action at a time.")
424                 action = i
425
426                 # Safety/Sanity check
427                 if action == "set" and (not suite.allowcsset):
428                     if force:
429                         utils.warn("Would not normally allow setting suite %s (allowsetcs is FALSE), but --force used" % (suite_name))
430                     else:
431                         utils.fubar("Will not reset suite %s due to its database configuration (allowsetcs is FALSE)" % (suite_name))
432
433     # Need an action...
434     if action == None:
435         utils.fubar("No action specified.")
436
437     britney = False
438     if action == "set" and cnf["Control-Suite::Options::Britney"]:
439         britney = True
440
441     if action == "list":
442         get_list(suite, session)
443     else:
444         Logger = daklog.Logger("control-suite")
445         if file_list:
446             for f in file_list:
447                 process_file(utils.open_file(f), suite, action, session, britney, force)
448         else:
449             process_file(sys.stdin, suite, action, session, britney, force)
450         Logger.close()
451
452 #######################################################################################
453
454 if __name__ == '__main__':
455     main()