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 #######################################################################################
46 import dak.lib.utils, dak.lib.database, dak.lib.logging
48 #######################################################################################
54 ################################################################################
56 def usage (exit_code=0):
57 print """Usage: dak control-suite [OPTIONS] [FILE]
58 Display or alter the contents of a suite using FILE(s), or stdin.
60 -a, --add=SUITE add to SUITE
61 -h, --help show this help and exit
62 -l, --list=SUITE list the contents of SUITE
63 -r, --remove=SUITE remove from SUITE
64 -s, --set=SUITE set SUITE"""
68 #######################################################################################
70 def get_id (package, version, architecture):
71 if architecture == "source":
72 q = projectB.query("SELECT id FROM source WHERE source = '%s' AND version = '%s'" % (package, version))
74 q = projectB.query("SELECT b.id FROM binaries b, architecture a WHERE b.package = '%s' AND b.version = '%s' AND (a.arch_string = '%s' OR a.arch_string = 'all') AND b.architecture = a.id" % (package, version, architecture))
78 dak.lib.utils.warn("Couldn't find '%s~%s~%s'." % (package, version, architecture))
81 dak.lib.utils.warn("Found more than one match for '%s~%s~%s'." % (package, version, architecture))
86 #######################################################################################
88 def set_suite (file, suite_id):
89 lines = file.readlines()
91 projectB.query("BEGIN WORK")
93 # Build up a dictionary of what is currently in the suite
95 q = projectB.query("SELECT b.package, b.version, a.arch_string, ba.id FROM binaries b, bin_associations ba, architecture a WHERE ba.suite = %s AND ba.bin = b.id AND b.architecture = a.id" % (suite_id))
100 q = projectB.query("SELECT s.source, s.version, sa.id FROM source s, src_associations sa WHERE sa.suite = %s AND sa.source = s.id" % (suite_id))
103 key = " ".join(i[:2]) + " source"
106 # Build up a dictionary of what should be in the suite
109 split_line = line.strip().split()
110 if len(split_line) != 3:
111 dak.lib.utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
113 key = " ".join(split_line)
116 # Check to see which packages need removed and remove them
117 for key in current.keys():
118 if not desired.has_key(key):
119 (package, version, architecture) = key.split()
121 if architecture == "source":
122 q = projectB.query("DELETE FROM src_associations WHERE id = %s" % (id))
124 q = projectB.query("DELETE FROM bin_associations WHERE id = %s" % (id))
125 Logger.log(["removed",key,id])
127 # Check to see which packages need added and add them
128 for key in desired.keys():
129 if not current.has_key(key):
130 (package, version, architecture) = key.split()
131 id = get_id (package, version, architecture)
134 if architecture == "source":
135 q = projectB.query("INSERT INTO src_associations (suite, source) VALUES (%s, %s)" % (suite_id, id))
137 q = projectB.query("INSERT INTO bin_associations (suite, bin) VALUES (%s, %s)" % (suite_id, id))
138 Logger.log(["added",key,id])
140 projectB.query("COMMIT WORK")
142 #######################################################################################
144 def process_file (file, suite, action):
146 suite_id = dak.lib.database.get_suite_id(suite)
149 set_suite (file, suite_id)
152 lines = file.readlines()
154 projectB.query("BEGIN WORK")
157 split_line = line.strip().split()
158 if len(split_line) != 3:
159 dak.lib.utils.warn("'%s' does not break into 'package version architecture'." % (line[:-1]))
162 (package, version, architecture) = split_line
164 id = get_id(package, version, architecture)
168 if architecture == "source":
169 # Find the existing assoications ID, if any
170 q = projectB.query("SELECT id FROM src_associations WHERE suite = %s and source = %s" % (suite_id, id))
173 assoication_id = None
175 assoication_id = ql[0][0]
179 dak.lib.utils.warn("'%s~%s~%s' already exists in suite %s." % (package, version, architecture, suite))
182 q = projectB.query("INSERT INTO src_associations (suite, source) VALUES (%s, %s)" % (suite_id, id))
183 elif action == "remove":
184 if assoication_id == None:
185 dak.lib.utils.warn("'%s~%s~%s' doesn't exist in suite %s." % (package, version, architecture, suite))
188 q = projectB.query("DELETE FROM src_associations WHERE id = %s" % (assoication_id))
190 # Find the existing assoications ID, if any
191 q = projectB.query("SELECT id FROM bin_associations WHERE suite = %s and bin = %s" % (suite_id, id))
194 assoication_id = None
196 assoication_id = ql[0][0]
200 dak.lib.utils.warn("'%s~%s~%s' already exists in suite %s." % (package, version, architecture, suite))
203 q = projectB.query("INSERT INTO bin_associations (suite, bin) VALUES (%s, %s)" % (suite_id, id))
204 elif action == "remove":
205 if assoication_id == None:
206 dak.lib.utils.warn("'%s~%s~%s' doesn't exist in suite %s." % (package, version, architecture, suite))
209 q = projectB.query("DELETE FROM bin_associations WHERE id = %s" % (assoication_id))
211 projectB.query("COMMIT WORK")
213 #######################################################################################
215 def get_list (suite):
216 suite_id = dak.lib.database.get_suite_id(suite)
218 q = projectB.query("SELECT b.package, b.version, a.arch_string FROM binaries b, bin_associations ba, architecture a WHERE ba.suite = %s AND ba.bin = b.id AND b.architecture = a.id" % (suite_id))
224 q = projectB.query("SELECT s.source, s.version FROM source s, src_associations sa WHERE sa.suite = %s AND sa.source = s.id" % (suite_id))
227 print " ".join(i) + " source"
229 #######################################################################################
232 global Cnf, projectB, Logger
234 Cnf = dak.lib.utils.get_conf()
236 Arguments = [('a',"add","Control-Suite::Options::Add", "HasArg"),
237 ('h',"help","Control-Suite::Options::Help"),
238 ('l',"list","Control-Suite::Options::List","HasArg"),
239 ('r',"remove", "Control-Suite::Options::Remove", "HasArg"),
240 ('s',"set", "Control-Suite::Options::Set", "HasArg")]
242 for i in ["add", "help", "list", "remove", "set", "version" ]:
243 if not Cnf.has_key("Control-Suite::Options::%s" % (i)):
244 Cnf["Control-Suite::Options::%s" % (i)] = ""
246 file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
247 Options = Cnf.SubTree("Control-Suite::Options")
252 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"],int(Cnf["DB::Port"]))
254 dak.lib.database.init(Cnf, projectB)
258 for i in ("add", "list", "remove", "set"):
259 if Cnf["Control-Suite::Options::%s" % (i)] != "":
260 suite = Cnf["Control-Suite::Options::%s" % (i)]
261 if dak.lib.database.get_suite_id(suite) == -1:
262 dak.lib.utils.fubar("Unknown suite '%s'." %(suite))
265 dak.lib.utils.fubar("Can only perform one action at a time.")
270 dak.lib.utils.fubar("No action specified.")
272 # Safety/Sanity check
273 if action == "set" and suite != "testing":
274 dak.lib.utils.fubar("Will not reset a suite other than testing.")
279 Logger = dak.lib.logging.Logger(Cnf, "control-suite")
281 for file in file_list:
282 process_file(dak.lib.utils.open_file(file), suite, action)
284 process_file(sys.stdin, suite, action)
287 #######################################################################################
289 if __name__ == '__main__':