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 #######################################################################################
47 from daklib.config import Config
48 from daklib.dbconn import *
49 from daklib import daklog
50 from daklib import utils
52 #######################################################################################
56 ################################################################################
58 def usage (exit_code=0):
59 print """Usage: dak control-suite [OPTIONS] [FILE]
60 Display or alter the contents of a suite using FILE(s), or stdin.
62 -a, --add=SUITE add to SUITE
63 -h, --help show this help and exit
64 -l, --list=SUITE list the contents of SUITE
65 -r, --remove=SUITE remove from SUITE
66 -s, --set=SUITE set SUITE
67 -b, --britney generate changelog entry for britney runs"""
71 #######################################################################################
73 def get_id(package, version, architecture, session):
74 if architecture == "source":
75 q = session.execute("SELECT id FROM source WHERE source = :package AND version = :version",
76 {'package': package, 'version': version})
78 q = session.execute("""SELECT b.id FROM binaries b, architecture a
79 WHERE b.package = :package AND b.version = :version
80 AND (a.arch_string = :arch OR a.arch_string = 'all')
81 AND b.architecture = a.id""",
82 {'package': package, 'version': version, 'arch': architecture})
86 utils.warn("Couldn't find '%s_%s_%s'." % (package, version, architecture))
90 utils.warn("Found more than one match for '%s_%s_%s'." % (package, version, architecture))
95 #######################################################################################
97 def britney_changelog(packages, suite, session):
103 q = session.execute("""SELECT britney_changelog FROM suite
104 WHERE id = :suiteid""", {'suiteid': suite.suite_id})
105 brit_file = q.fetchone()[0]
109 q = session.execute("""SELECT s.source, s.version, sa.id
110 FROM source s, src_associations sa
111 WHERE sa.suite = :suiteid
112 AND sa.source = s.id""", {'suiteid': suite.suite_id})
114 for p in q.fetchall():
116 for p in packages.keys():
122 for p in current.keys():
124 if apt_pkg.VersionCompare(current[p], old[p]) > 0:
125 new[p] = [current[p], old[p]]
127 new[p] = [current[p], 0]
129 query = "SELECT source, changelog FROM changelogs WHERE"
131 query += " source = '%s' AND version > '%s' AND version <= '%s'" \
132 % (p, new[p][1], new[p][0])
133 query += " AND architecture LIKE '%source%' AND distribution in \
134 ('unstable', 'experimental', 'testing-proposed-updates') OR"
135 query += " False ORDER BY source, version DESC"
136 q = session.execute(query)
139 brit = utils.open_file(brit_file, 'w')
142 if pu and pu != u[0]:
144 brit.write("%s\n" % u[1])
146 if q.rowcount: brit.write("\n\n\n")
148 for p in list(set(old.keys()).difference(current.keys())):
149 brit.write("REMOVED: %s %s\n" % (p, old[p]))
154 #######################################################################################
156 def set_suite(file, suite, session, britney=False):
157 suite_id = suite.suite_id
158 lines = file.readlines()
160 # Our session is already in a transaction
162 # Build up a dictionary of what is currently in the suite
164 q = session.execute("""SELECT b.package, b.version, a.arch_string, ba.id
165 FROM binaries b, bin_associations ba, architecture a
166 WHERE ba.suite = :suiteid
167 AND ba.bin = b.id AND b.architecture = a.id""", {'suiteid': suite_id})
168 for i in q.fetchall():
169 key = " ".join(i[:3])
172 q = session.execute("""SELECT s.source, s.version, sa.id
173 FROM source s, src_associations sa
174 WHERE sa.suite = :suiteid
175 AND sa.source = s.id""", {'suiteid': suite_id})
176 for i in q.fetchall():
177 key = " ".join(i[:2]) + " source"
180 # Build up a dictionary of what should be in the suite
183 split_line = line.strip().split()
184 if len(split_line) != 3:
185 utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
187 key = " ".join(split_line)
190 # Check to see which packages need removed and remove them
191 for key in current.keys():
192 if not desired.has_key(key):
193 (package, version, architecture) = key.split()
195 if architecture == "source":
196 session.execute("""DELETE FROM src_associations WHERE id = :pkid""", {'pkid': pkid})
198 session.execute("""DELETE FROM bin_associations WHERE id = :pkid""", {'pkid': pkid})
199 Logger.log(["removed", key, pkid])
201 # Check to see which packages need added and add them
202 for key in desired.keys():
203 if not current.has_key(key):
204 (package, version, architecture) = key.split()
205 pkid = get_id (package, version, architecture, session)
208 if architecture == "source":
209 session.execute("""INSERT INTO src_associations (suite, source)
210 VALUES (:suiteid, :pkid)""", {'suiteid': suite_id, 'pkid': pkid})
212 session.execute("""INSERT INTO bin_associations (suite, bin)
213 VALUES (:suiteid, :pkid)""", {'suiteid': suite_id, 'pkid': pkid})
214 Logger.log(["added", key, pkid])
219 britney_changelog(current, suite, session)
221 #######################################################################################
223 def process_file(file, suite, action, session, britney=False):
225 set_suite(file, suite, session, britney)
228 suite_id = suite.suite_id
230 lines = file.readlines()
232 # Our session is already in a transaction
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]))
239 (package, version, architecture) = split_line
241 pkid = get_id(package, version, architecture, session)
245 if architecture == "source":
246 # Find the existing association ID, if any
247 q = session.execute("""SELECT id FROM src_associations
248 WHERE suite = :suiteid and source = :pkid""",
249 {'suiteid': suite_id, 'pkid': pkid})
252 association_id = None
254 association_id = ql[0][0]
259 utils.warn("'%s_%s_%s' already exists in suite %s." % (package, version, architecture, suite))
262 session.execute("""INSERT INTO src_associations (suite, source)
263 VALUES (:suiteid, :pkid)""",
264 {'suiteid': suite_id, 'pkid': pkid})
265 elif action == "remove":
266 if association_id == None:
267 utils.warn("'%s_%s_%s' doesn't exist in suite %s." % (package, version, architecture, suite))
270 session.execute("""DELETE FROM src_associations WHERE id = :pkid""", {'pkid': association_id})
272 # Find the existing associations ID, if any
273 q = session.execute("""SELECT id FROM bin_associations
274 WHERE suite = :suiteid and bin = :pkid""",
275 {'suiteid': suite_id, 'pkid': pkid})
278 association_id = None
280 association_id = ql[0][0]
285 utils.warn("'%s_%s_%s' already exists in suite %s." % (package, version, architecture, suite))
288 session.execute("""INSERT INTO bin_associations (suite, bin)
289 VALUES (:suiteid, :pkid)""",
290 {'suiteid': suite_id, 'pkid': pkid})
291 elif action == "remove":
292 if association_id == None:
293 utils.warn("'%s_%s_%s' doesn't exist in suite %s." % (package, version, architecture, suite))
296 session.execute("""DELETE FROM bin_associations WHERE id = :pkid""", {'pkid': association_id})
300 #######################################################################################
302 def get_list(suite, session):
303 suite_id = suite.suite_id
305 q = session.execute("""SELECT b.package, b.version, a.arch_string
306 FROM binaries b, bin_associations ba, architecture a
307 WHERE ba.suite = :suiteid
308 AND ba.bin = b.id AND b.architecture = a.id""", {'suiteid': suite_id})
309 for i in q.fetchall():
313 q = session.execute("""SELECT s.source, s.version
314 FROM source s, src_associations sa
315 WHERE sa.suite = :suiteid
316 AND sa.source = s.id""", {'suiteid': suite_id})
317 for i in q.fetchall():
318 print " ".join(i) + " source"
320 #######################################################################################
327 Arguments = [('a',"add","Control-Suite::Options::Add", "HasArg"),
328 ('b',"britney","Control-Suite::Options::Britney"),
329 ('h',"help","Control-Suite::Options::Help"),
330 ('l',"list","Control-Suite::Options::List","HasArg"),
331 ('r',"remove", "Control-Suite::Options::Remove", "HasArg"),
332 ('s',"set", "Control-Suite::Options::Set", "HasArg")]
334 for i in ["add", "britney", "help", "list", "remove", "set", "version" ]:
335 if not cnf.has_key("Control-Suite::Options::%s" % (i)):
336 cnf["Control-Suite::Options::%s" % (i)] = ""
339 file_list = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv);
340 except SystemError, e:
343 Options = cnf.SubTree("Control-Suite::Options")
348 session = DBConn().session()
352 for i in ("add", "list", "remove", "set"):
353 if cnf["Control-Suite::Options::%s" % (i)] != "":
354 suite_name = cnf["Control-Suite::Options::%s" % (i)]
355 suite = get_suite(suite_name, session=session)
357 utils.fubar("Unknown suite '%s'." % (suite_name))
360 utils.fubar("Can only perform one action at a time.")
365 utils.fubar("No action specified.")
367 # Safety/Sanity check
368 # XXX: This should be stored in the database
369 if action == "set" and suite_name not in ["testing"]:
370 utils.fubar("Will not reset suite %s" % (suite_name))
373 if action == "set" and cnf["Control-Suite::Options::Britney"]:
377 get_list(suite, session)
379 Logger = daklog.Logger(cnf.Cnf, "control-suite")
382 process_file(utils.open_file(f), suite, action, session, britney)
384 process_file(sys.stdin, suite, action, session, britney)
387 #######################################################################################
389 if __name__ == '__main__':