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 changelog FROM suite WHERE id = :suiteid", \
104 {'suiteid': suite.suite_id})
105 brit_file = q.fetchone()[0]
112 q = session.execute("""SELECT s.source, s.version, sa.id
113 FROM source s, src_associations sa
114 WHERE sa.suite = :suiteid
115 AND sa.source = s.id""", {'suiteid': suite.suite_id})
117 for p in q.fetchall():
119 for p in packages.keys():
125 for p in current.keys():
127 if apt_pkg.VersionCompare(current[p], old[p]) > 0:
128 new[p] = [current[p], old[p]]
130 new[p] = [current[p], 0]
132 query = "SELECT source, changelog FROM changelogs WHERE"
134 query += " source = '%s' AND version > '%s' AND version <= '%s'" \
135 % (p, new[p][1], new[p][0])
136 query += " AND architecture LIKE '%source%' AND distribution in \
137 ('unstable', 'experimental', 'testing-proposed-updates') OR"
138 query += " False ORDER BY source, version DESC"
139 q = session.execute(query)
142 brit = utils.open_file(brit_file, 'w')
145 if pu and pu != u[0]:
147 brit.write("%s\n" % u[1])
149 if q.rowcount: brit.write("\n\n\n")
151 for p in list(set(old.keys()).difference(current.keys())):
152 brit.write("REMOVED: %s %s\n" % (p, old[p]))
157 #######################################################################################
159 def set_suite(file, suite, session, britney=False):
160 suite_id = suite.suite_id
161 lines = file.readlines()
163 # Our session is already in a transaction
165 # Build up a dictionary of what is currently in the suite
167 q = session.execute("""SELECT b.package, b.version, a.arch_string, ba.id
168 FROM binaries b, bin_associations ba, architecture a
169 WHERE ba.suite = :suiteid
170 AND ba.bin = b.id AND b.architecture = a.id""", {'suiteid': suite_id})
171 for i in q.fetchall():
172 key = " ".join(i[:3])
175 q = session.execute("""SELECT s.source, s.version, sa.id
176 FROM source s, src_associations sa
177 WHERE sa.suite = :suiteid
178 AND sa.source = s.id""", {'suiteid': suite_id})
179 for i in q.fetchall():
180 key = " ".join(i[:2]) + " source"
183 # Build up a dictionary of what should be in the suite
186 split_line = line.strip().split()
187 if len(split_line) != 3:
188 utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
190 key = " ".join(split_line)
193 # Check to see which packages need removed and remove them
194 for key in current.keys():
195 if not desired.has_key(key):
196 (package, version, architecture) = key.split()
198 if architecture == "source":
199 session.execute("""DELETE FROM src_associations WHERE id = :pkid""", {'pkid': pkid})
201 session.execute("""DELETE FROM bin_associations WHERE id = :pkid""", {'pkid': pkid})
202 Logger.log(["removed", key, pkid])
204 # Check to see which packages need added and add them
205 for key in desired.keys():
206 if not current.has_key(key):
207 (package, version, architecture) = key.split()
208 pkid = get_id (package, version, architecture, session)
211 if architecture == "source":
212 session.execute("""INSERT INTO src_associations (suite, source)
213 VALUES (:suiteid, :pkid)""", {'suiteid': suite_id, 'pkid': pkid})
215 session.execute("""INSERT INTO bin_associations (suite, bin)
216 VALUES (:suiteid, :pkid)""", {'suiteid': suite_id, 'pkid': pkid})
217 Logger.log(["added", key, pkid])
222 britney_changelog(current, suite, session)
224 #######################################################################################
226 def process_file(file, suite, action, session, britney=False):
228 set_suite(file, suite, session, britney)
231 suite_id = suite.suite_id
233 lines = file.readlines()
235 # Our session is already in a transaction
237 split_line = line.strip().split()
238 if len(split_line) != 3:
239 utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
242 (package, version, architecture) = split_line
244 pkid = get_id(package, version, architecture, session)
248 if architecture == "source":
249 # Find the existing association ID, if any
250 q = session.execute("""SELECT id FROM src_associations
251 WHERE suite = :suiteid and source = :pkid""",
252 {'suiteid': suite_id, 'pkid': pkid})
255 association_id = None
257 association_id = ql[0][0]
262 utils.warn("'%s_%s_%s' already exists in suite %s." % (package, version, architecture, suite))
265 session.execute("""INSERT INTO src_associations (suite, source)
266 VALUES (:suiteid, :pkid)""",
267 {'suiteid': suite_id, 'pkid': pkid})
268 elif action == "remove":
269 if association_id == None:
270 utils.warn("'%s_%s_%s' doesn't exist in suite %s." % (package, version, architecture, suite))
273 session.execute("""DELETE FROM src_associations WHERE id = :pkid""", {'pkid': association_id})
275 # Find the existing associations ID, if any
276 q = session.execute("""SELECT id FROM bin_associations
277 WHERE suite = :suiteid and bin = :pkid""",
278 {'suiteid': suite_id, 'pkid': pkid})
281 association_id = None
283 association_id = ql[0][0]
288 utils.warn("'%s_%s_%s' already exists in suite %s." % (package, version, architecture, suite))
291 session.execute("""INSERT INTO bin_associations (suite, bin)
292 VALUES (:suiteid, :pkid)""",
293 {'suiteid': suite_id, 'pkid': pkid})
294 elif action == "remove":
295 if association_id == None:
296 utils.warn("'%s_%s_%s' doesn't exist in suite %s." % (package, version, architecture, suite))
299 session.execute("""DELETE FROM bin_associations WHERE id = :pkid""", {'pkid': association_id})
303 #######################################################################################
305 def get_list(suite, session):
306 suite_id = suite.suite_id
308 q = session.execute("""SELECT b.package, b.version, a.arch_string
309 FROM binaries b, bin_associations ba, architecture a
310 WHERE ba.suite = :suiteid
311 AND ba.bin = b.id AND b.architecture = a.id""", {'suiteid': suite_id})
312 for i in q.fetchall():
316 q = session.execute("""SELECT s.source, s.version
317 FROM source s, src_associations sa
318 WHERE sa.suite = :suiteid
319 AND sa.source = s.id""", {'suiteid': suite_id})
320 for i in q.fetchall():
321 print " ".join(i) + " source"
323 #######################################################################################
330 Arguments = [('a',"add","Control-Suite::Options::Add", "HasArg"),
331 ('b',"britney","Control-Suite::Options::Britney"),
332 ('h',"help","Control-Suite::Options::Help"),
333 ('l',"list","Control-Suite::Options::List","HasArg"),
334 ('r',"remove", "Control-Suite::Options::Remove", "HasArg"),
335 ('s',"set", "Control-Suite::Options::Set", "HasArg")]
337 for i in ["add", "britney", "help", "list", "remove", "set", "version" ]:
338 if not cnf.has_key("Control-Suite::Options::%s" % (i)):
339 cnf["Control-Suite::Options::%s" % (i)] = ""
342 file_list = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv);
343 except SystemError, e:
346 Options = cnf.SubTree("Control-Suite::Options")
351 session = DBConn().session()
355 for i in ("add", "list", "remove", "set"):
356 if cnf["Control-Suite::Options::%s" % (i)] != "":
357 suite_name = cnf["Control-Suite::Options::%s" % (i)]
358 suite = get_suite(suite_name, session=session)
360 utils.fubar("Unknown suite '%s'." % (suite_name))
363 utils.fubar("Can only perform one action at a time.")
368 utils.fubar("No action specified.")
370 # Safety/Sanity check
371 # XXX: This should be stored in the database
372 if action == "set" and suite_name not in ["testing"]:
373 utils.fubar("Will not reset suite %s" % (suite_name))
376 if action == "set" and cnf["Control-Suite::Options::Britney"]:
380 get_list(suite, session)
382 Logger = daklog.Logger(cnf.Cnf, "control-suite")
385 process_file(utils.open_file(f), suite, action, session, britney)
387 process_file(sys.stdin, suite, action, session, britney)
390 #######################################################################################
392 if __name__ == '__main__':