3 """ Manipulate suite tags """
4 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 James Troup <james@nocrew.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 #######################################################################################
22 # 8to6Guy: "Wow, Bob, You look rough!"
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!"
31 # CoWorker#n+3: "Bob's been overdosing quite a bit lately..."
32 # CoWorker#n+4: "Third time this week."
34 # -- http://www.angryflower.com/8to6.gif
36 #######################################################################################
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.
42 #######################################################################################
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
54 #######################################################################################
58 ################################################################################
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.
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"""
73 #######################################################################################
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})
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})
88 utils.warn("Couldn't find '%s_%s_%s'." % (package, version, architecture))
92 utils.warn("Found more than one match for '%s_%s_%s'." % (package, version, architecture))
97 #######################################################################################
99 def britney_changelog(packages, suite, session):
103 Cnf = utils.get_conf()
106 q = session.execute("SELECT changelog FROM suite WHERE id = :suiteid", \
107 {'suiteid': suite.suite_id})
108 brit_file = q.fetchone()[0]
113 brit_file = os.path.join(Cnf['Dir::Root'], brit_file)
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})
122 for p in q.fetchall():
124 for p in packages.keys():
130 for p in current.keys():
132 if apt_pkg.VersionCompare(current[p], old[p]) > 0:
133 new[p] = [current[p], old[p]]
135 new[p] = [current[p], 0]
137 query = "SELECT source, changelog FROM changelogs WHERE"
139 query += " source = '%s' AND version > '%s' AND version <= '%s'" \
140 % (p, new[p][1], new[p][0])
141 query += " AND architecture LIKE '%source%' AND distribution in \
142 ('unstable', 'experimental', 'testing-proposed-updates') OR"
143 query += " False ORDER BY source, version DESC"
144 q = session.execute(query)
147 brit = utils.open_file(brit_file, 'w')
150 if pu and pu != u[0]:
152 brit.write("%s\n" % u[1])
154 if q.rowcount: brit.write("\n\n\n")
156 for p in list(set(old.keys()).difference(current.keys())):
157 brit.write("REMOVED: %s %s\n" % (p, old[p]))
162 #######################################################################################
164 def version_checks(package, architecture, target_suite, new_version, session, force = False):
165 if architecture == "source":
166 suite_version_list = get_suite_version_by_source(package, session)
168 suite_version_list = get_suite_version_by_package(package, architecture, session)
170 must_be_newer_than = [ vc.reference.suite_name for vc in get_version_checks(target_suite, "MustBeNewerThan") ]
171 must_be_older_than = [ vc.reference.suite_name for vc in get_version_checks(target_suite, "MustBeOlderThan") ]
173 # Must be newer than an existing version in target_suite
174 if target_suite not in must_be_newer_than:
175 must_be_newer_than.append(target_suite)
179 for suite, version in suite_version_list:
180 cmp = apt_pkg.VersionCompare(new_version, version)
181 if suite in must_be_newer_than and cmp < 1:
182 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))
184 if suite in must_be_older_than and cmp > 1:
185 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))
190 utils.warn("Continuing anyway (forced)...")
192 utils.fubar("Aborting. Version checks violated and not forced.")
194 #######################################################################################
196 def cmp_package_version(a, b):
198 comparison function for tuples of the form (package-name, version ...)
200 cmp_package = cmp(a[0], b[0])
203 return apt_pkg.VersionCompare(a[1], b[1])
205 #######################################################################################
207 def set_suite(file, suite, session, britney=False, force=False):
208 suite_id = suite.suite_id
209 lines = file.readlines()
211 # Our session is already in a transaction
213 # Build up a dictionary of what is currently in the suite
215 q = session.execute("""SELECT b.package, b.version, a.arch_string, ba.id
216 FROM binaries b, bin_associations ba, architecture a
217 WHERE ba.suite = :suiteid
218 AND ba.bin = b.id AND b.architecture = a.id""", {'suiteid': suite_id})
223 q = session.execute("""SELECT s.source, s.version, 'source', sa.id
224 FROM source s, src_associations sa
225 WHERE sa.suite = :suiteid
226 AND sa.source = s.id""", {'suiteid': suite_id})
231 # Build up a dictionary of what should be in the suite
234 split_line = line.strip().split()
235 if len(split_line) != 3:
236 utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
238 desired.add(tuple(split_line))
240 # Check to see which packages need added and add them
241 for key in sorted(desired, cmp=cmp_package_version):
242 if key not in current:
243 (package, version, architecture) = key
244 version_checks(package, architecture, suite.suite_name, version, session, force)
245 pkid = get_id (package, version, architecture, session)
248 if architecture == "source":
249 session.execute("""INSERT INTO src_associations (suite, source)
250 VALUES (:suiteid, :pkid)""", {'suiteid': suite_id, 'pkid': pkid})
252 session.execute("""INSERT INTO bin_associations (suite, bin)
253 VALUES (:suiteid, :pkid)""", {'suiteid': suite_id, 'pkid': pkid})
254 Logger.log(["added", " ".join(key), pkid])
256 # Check to see which packages need removed and remove them
257 for key, pkid in current.iteritems():
258 if key not in desired:
259 (package, version, architecture) = key
260 if architecture == "source":
261 session.execute("""DELETE FROM src_associations WHERE id = :pkid""", {'pkid': pkid})
263 session.execute("""DELETE FROM bin_associations WHERE id = :pkid""", {'pkid': pkid})
264 Logger.log(["removed", " ".join(key), pkid])
269 britney_changelog(current, suite, session)
271 #######################################################################################
273 def process_file(file, suite, action, session, britney=False, force=False):
275 set_suite(file, suite, session, britney, force)
278 suite_id = suite.suite_id
282 # Our session is already in a transaction
284 split_line = line.strip().split()
285 if len(split_line) != 3:
286 utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
288 request.append(split_line)
290 request.sort(cmp=cmp_package_version)
292 for package, version, architecture in request:
293 pkid = get_id(package, version, architecture, session)
297 # Do version checks when adding packages
299 version_checks(package, architecture, suite.suite_name, version, session, force)
301 if architecture == "source":
302 # Find the existing association ID, if any
303 q = session.execute("""SELECT id FROM src_associations
304 WHERE suite = :suiteid and source = :pkid""",
305 {'suiteid': suite_id, 'pkid': pkid})
308 association_id = None
310 association_id = ql[0][0]
315 utils.warn("'%s_%s_%s' already exists in suite %s." % (package, version, architecture, suite))
318 session.execute("""INSERT INTO src_associations (suite, source)
319 VALUES (:suiteid, :pkid)""",
320 {'suiteid': suite_id, 'pkid': pkid})
321 elif action == "remove":
322 if association_id == None:
323 utils.warn("'%s_%s_%s' doesn't exist in suite %s." % (package, version, architecture, suite))
326 session.execute("""DELETE FROM src_associations WHERE id = :pkid""", {'pkid': association_id})
328 # Find the existing associations ID, if any
329 q = session.execute("""SELECT id FROM bin_associations
330 WHERE suite = :suiteid and bin = :pkid""",
331 {'suiteid': suite_id, 'pkid': pkid})
334 association_id = None
336 association_id = ql[0][0]
341 utils.warn("'%s_%s_%s' already exists in suite %s." % (package, version, architecture, suite))
344 session.execute("""INSERT INTO bin_associations (suite, bin)
345 VALUES (:suiteid, :pkid)""",
346 {'suiteid': suite_id, 'pkid': pkid})
347 elif action == "remove":
348 if association_id == None:
349 utils.warn("'%s_%s_%s' doesn't exist in suite %s." % (package, version, architecture, suite))
352 session.execute("""DELETE FROM bin_associations WHERE id = :pkid""", {'pkid': association_id})
356 #######################################################################################
358 def get_list(suite, session):
359 suite_id = suite.suite_id
361 q = session.execute("""SELECT b.package, b.version, a.arch_string
362 FROM binaries b, bin_associations ba, architecture a
363 WHERE ba.suite = :suiteid
364 AND ba.bin = b.id AND b.architecture = a.id""", {'suiteid': suite_id})
365 for i in q.fetchall():
369 q = session.execute("""SELECT s.source, s.version
370 FROM source s, src_associations sa
371 WHERE sa.suite = :suiteid
372 AND sa.source = s.id""", {'suiteid': suite_id})
373 for i in q.fetchall():
374 print " ".join(i) + " source"
376 #######################################################################################
383 Arguments = [('a',"add","Control-Suite::Options::Add", "HasArg"),
384 ('b',"britney","Control-Suite::Options::Britney"),
385 ('f','force','Control-Suite::Options::Force'),
386 ('h',"help","Control-Suite::Options::Help"),
387 ('l',"list","Control-Suite::Options::List","HasArg"),
388 ('r',"remove", "Control-Suite::Options::Remove", "HasArg"),
389 ('s',"set", "Control-Suite::Options::Set", "HasArg")]
391 for i in ["add", "britney", "help", "list", "remove", "set", "version" ]:
392 if not cnf.has_key("Control-Suite::Options::%s" % (i)):
393 cnf["Control-Suite::Options::%s" % (i)] = ""
396 file_list = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv);
397 except SystemError as e:
400 Options = cnf.SubTree("Control-Suite::Options")
405 session = DBConn().session()
407 force = Options.has_key("Force") and Options["Force"]
411 for i in ("add", "list", "remove", "set"):
412 if cnf["Control-Suite::Options::%s" % (i)] != "":
413 suite_name = cnf["Control-Suite::Options::%s" % (i)]
414 suite = get_suite(suite_name, session=session)
416 utils.fubar("Unknown suite '%s'." % (suite_name))
419 utils.fubar("Can only perform one action at a time.")
422 # Safety/Sanity check
423 if action == "set" and (not suite.allowcsset):
425 utils.warn("Would not normally allow setting suite %s (allowsetcs is FALSE), but --force used" % (suite_name))
427 utils.fubar("Will not reset suite %s due to its database configuration (allowsetcs is FALSE)" % (suite_name))
431 utils.fubar("No action specified.")
434 if action == "set" and cnf["Control-Suite::Options::Britney"]:
438 get_list(suite, session)
440 Logger = daklog.Logger("control-suite")
443 process_file(utils.open_file(f), suite, action, session, britney, force)
445 process_file(sys.stdin, suite, action, session, britney, force)
448 #######################################################################################
450 if __name__ == '__main__':