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"""
70 #######################################################################################
72 def get_id(package, version, architecture, session):
73 if architecture == "source":
74 q = session.execute("SELECT id FROM source WHERE source = :package AND version = :version",
75 {'package': package, 'version': version})
77 q = session.execute("""SELECT b.id FROM binaries b, architecture a
78 WHERE b.package = :package AND b.version = :version
79 AND (a.arch_string = :arch OR a.arch_string = 'all')
80 AND b.architecture = a.id""",
81 {'package': package, 'version': version, 'arch': architecture})
85 utils.warn("Couldn't find '%s_%s_%s'." % (package, version, architecture))
89 utils.warn("Found more than one match for '%s_%s_%s'." % (package, version, architecture))
94 #######################################################################################
96 def set_suite(file, suite, session):
97 suite_id = suite.suite_id
98 lines = file.readlines()
100 # Our session is already in a transaction
102 # Build up a dictionary of what is currently in the suite
104 q = session.execute("""SELECT b.package, b.version, a.arch_string, ba.id
105 FROM binaries b, bin_associations ba, architecture a
106 WHERE ba.suite = :suiteid
107 AND ba.bin = b.id AND b.architecture = a.id""", {'suiteid': suite_id})
108 for i in q.fetchall():
109 key = " ".join(i[:3])
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_id})
116 for i in q.fetchall():
117 key = " ".join(i[:2]) + " source"
120 # Build up a dictionary of what should be in the suite
123 split_line = line.strip().split()
124 if len(split_line) != 3:
125 utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
127 key = " ".join(split_line)
130 # Check to see which packages need removed and remove them
131 for key in current.keys():
132 if not desired.has_key(key):
133 (package, version, architecture) = key.split()
135 if architecture == "source":
136 session.execute("""DELETE FROM src_associations WHERE id = :pkid""", {'pkid': pkid})
138 session.execute("""DELETE FROM bin_associations WHERE id = :pkid""", {'pkid': pkid})
139 Logger.log(["removed", key, pkid])
141 # Check to see which packages need added and add them
142 for key in desired.keys():
143 if not current.has_key(key):
144 (package, version, architecture) = key.split()
145 pkid = get_id (package, version, architecture)
148 if architecture == "source":
149 session.execute("""INSERT INTO src_associations (suite, source)
150 VALUES (:suiteid, :pkid)""", {'suiteid': suite_id, 'pkid': pkid})
152 session.execute("""INSERT INTO bin_associations (suite, bin)
153 VALUES (:suiteid, :pkid)""", {'suiteid': suite_id, 'pkid': pkid})
154 Logger.log(["added", key, pkid])
158 #######################################################################################
160 def process_file(file, suite, action, session):
162 set_suite(file, suite, session)
165 suite_id = suite.suite_id
167 lines = file.readlines()
169 # Our session is already in a transaction
171 split_line = line.strip().split()
172 if len(split_line) != 3:
173 utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
176 (package, version, architecture) = split_line
178 pkid = get_id(package, version, architecture)
182 if architecture == "source":
183 # Find the existing association ID, if any
184 q = session.execute("""SELECT id FROM src_associations
185 WHERE suite = :suiteid and source = :pkid""",
186 {'suiteid': suite_id, 'pkid': pkid})
189 association_id = None
191 association_id = ql[0][0]
196 utils.warn("'%s_%s_%s' already exists in suite %s." % (package, version, architecture, suite))
199 session.execute("""INSERT INTO src_associations (suite, source)
200 VALUES (:suiteid, :pkid)""",
201 {'suiteid': suite_id, 'pkid': pkid})
202 elif action == "remove":
203 if association_id == None:
204 utils.warn("'%s_%s_%s' doesn't exist in suite %s." % (package, version, architecture, suite))
207 session.execute("""DELETE FROM src_associations WHERE id = :pkid""", {'pkid': association_id})
209 # Find the existing associations ID, if any
210 q = session.execute("""SELECT id FROM bin_associations
211 WHERE suite = :suiteid and bin = :pkid""",
212 {'suiteid': suite_id, 'pkid': pkid})
215 association_id = None
217 association_id = ql[0][0]
222 utils.warn("'%s_%s_%s' already exists in suite %s." % (package, version, architecture, suite))
225 session.execute("""INSERT INTO bin_associations (suite, bin)
227 {'suiteid': suite_id, 'pkid': pkid})
228 elif action == "remove":
229 if association_id == None:
230 utils.warn("'%s_%s_%s' doesn't exist in suite %s." % (package, version, architecture, suite))
233 session.execute("""DELETE FROM bin_associations WHERE id = :pkid""", {'pkid': association_id})
237 #######################################################################################
239 def get_list(suite, session):
240 suite_id = suite.suite_id
242 q = session.execute("""SELECT b.package, b.version, a.arch_string
243 FROM binaries b, bin_associations ba, architecture a
244 WHERE ba.suite = :suiteid
245 AND ba.bin = b.id AND b.architecture = a.id""", {'suiteid': suite_id})
246 for i in q.fetchall():
250 q = session.execute("""SELECT s.source, s.version
251 FROM source s, src_associations sa
252 WHERE sa.suite = :suiteid
253 AND sa.source = s.id""", {'suiteid': suite_id})
254 for i in q.fetchall():
255 print " ".join(i) + " source"
257 #######################################################################################
264 Arguments = [('a',"add","Control-Suite::Options::Add", "HasArg"),
265 ('h',"help","Control-Suite::Options::Help"),
266 ('l',"list","Control-Suite::Options::List","HasArg"),
267 ('r',"remove", "Control-Suite::Options::Remove", "HasArg"),
268 ('s',"set", "Control-Suite::Options::Set", "HasArg")]
270 for i in ["add", "help", "list", "remove", "set", "version" ]:
271 if not cnf.has_key("Control-Suite::Options::%s" % (i)):
272 cnf["Control-Suite::Options::%s" % (i)] = ""
275 file_list = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv);
276 except SystemError, e:
279 Options = cnf.SubTree("Control-Suite::Options")
284 session = DBConn().session()
288 for i in ("add", "list", "remove", "set"):
289 if cnf["Control-Suite::Options::%s" % (i)] != "":
290 suite_name = cnf["Control-Suite::Options::%s" % (i)]
291 suite = get_suite(suite_name, session=session)
293 utils.fubar("Unknown suite '%s'." % (suite_name))
296 utils.fubar("Can only perform one action at a time.")
301 utils.fubar("No action specified.")
303 # Safety/Sanity check
304 # XXX: This should be stored in the database
305 if action == "set" and suite_name not in ["testing", "etch-m68k"]:
306 utils.fubar("Will not reset suite %s" % (suite_name))
309 get_list(suite, session)
311 Logger = daklog.Logger(cnf.Cnf, "control-suite")
314 process_file(utils.open_file(f), suite, action, session)
316 process_file(sys.stdin, suite, action, session)
319 #######################################################################################
321 if __name__ == '__main__':