]> git.decadent.org.uk Git - dak.git/blob - dak/control_overrides.py
control-overrides: Simplify maintainer override handling
[dak.git] / dak / control_overrides.py
1 #!/usr/bin/env python
2
3 """ Bulk manipulation of the overrides """
4 # Copyright (C) 2000, 2001, 2002, 2003, 2006  James Troup <james@nocrew.org>
5
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.
10
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.
15
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
19
20 ################################################################################
21
22 # On 30 Nov 1998, James Troup wrote:
23 #
24 # > James Troup<2> <troup2@debian.org>
25 # >
26 # >    James is a clone of James; he's going to take over the world.
27 # >    After he gets some sleep.
28 #
29 # Could you clone other things too? Sheep? Llamas? Giant mutant turnips?
30 #
31 # Your clone will need some help to take over the world, maybe clone up an
32 # army of penguins and threaten to unleash them on the world, forcing
33 # governments to sway to the new James' will!
34 #
35 # Yes, I can envision a day when James' duplicate decides to take a horrific
36 # vengance on the James that spawned him and unleashes his fury in the form
37 # of thousands upon thousands of chickens that look just like Captin Blue
38 # Eye! Oh the horror.
39 #
40 # Now you'll have to were name tags to people can tell you apart, unless of
41 # course the new clone is truely evil in which case he should be easy to
42 # identify!
43 #
44 # Jason
45 # Chicken. Black. Helicopters.
46 # Be afraid.
47
48 # <Pine.LNX.3.96.981130011300.30365Z-100000@wakko>
49
50 ################################################################################
51
52 import pg, sys, time
53 import apt_pkg
54 from daklib import utils
55 from daklib import database
56 from daklib import logging
57 from daklib.regexes import re_comments
58
59 ################################################################################
60
61 Cnf = None
62 projectB = None
63 Logger = None
64
65 ################################################################################
66
67 def usage (exit_code=0):
68     print """Usage: dak control-overrides [OPTIONS]
69   -h, --help               print this help and exit
70
71   -c, --component=CMPT     list/set overrides by component
72                                   (contrib,*main,non-free)
73   -s, --suite=SUITE        list/set overrides by suite
74                                   (experimental,stable,testing,*unstable)
75   -t, --type=TYPE          list/set overrides by type
76                                   (*deb,dsc,udeb)
77
78   -a, --add                add overrides (changes and deletions are ignored)
79   -S, --set                set overrides
80   -l, --list               list overrides
81
82   -q, --quiet              be less verbose
83
84  starred (*) values are default"""
85     sys.exit(exit_code)
86
87 ################################################################################
88
89 def process_file (file, suite, component, type, action):
90     suite_id = database.get_suite_id(suite)
91     if suite_id == -1:
92         utils.fubar("Suite '%s' not recognised." % (suite))
93
94     component_id = database.get_component_id(component)
95     if component_id == -1:
96         utils.fubar("Component '%s' not recognised." % (component))
97
98     type_id = database.get_override_type_id(type)
99     if type_id == -1:
100         utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)" % (type))
101
102     # --set is done mostly internal for performance reasons; most
103     # invocations of --set will be updates and making people wait 2-3
104     # minutes while 6000 select+inserts are run needlessly isn't cool.
105
106     original = {}
107     new = {}
108     c_skipped = 0
109     c_added = 0
110     c_updated = 0
111     c_removed = 0
112     c_error = 0
113
114     q = projectB.query("SELECT o.package, o.priority, o.section, o.maintainer, p.priority, s.section FROM override o, priority p, section s WHERE o.suite = %s AND o.component = %s AND o.type = %s and o.priority = p.id and o.section = s.id"
115                        % (suite_id, component_id, type_id))
116     for i in q.getresult():
117         original[i[0]] = i[1:]
118
119     start_time = time.time()
120     projectB.query("BEGIN WORK")
121     for line in file.readlines():
122         line = re_comments.sub('', line).strip()
123         if line == "":
124             continue
125
126         maintainer_override = ""
127         if type == "dsc":
128             split_line = line.split(None, 2)
129             if len(split_line) == 2:
130                 (package, section) = split_line
131             elif len(split_line) == 3:
132                 (package, section, maintainer_override) = split_line
133             else:
134                 utils.warn("'%s' does not break into 'package section [maintainer-override]'." % (line))
135                 c_error += 1
136                 continue
137             priority = "source"
138         else: # binary or udeb
139             split_line = line.split(None, 3)
140             if len(split_line) == 3:
141                 (package, priority, section) = split_line
142             elif len(split_line) == 4:
143                 (package, priority, section, maintainer_override) = split_line
144             else:
145                 utils.warn("'%s' does not break into 'package priority section [maintainer-override]'." % (line))
146                 c_error += 1
147                 continue
148
149         section_id = database.get_section_id(section)
150         if section_id == -1:
151             utils.warn("'%s' is not a valid section. ['%s' in suite %s, component %s]." % (section, package, suite, component))
152             c_error += 1
153             continue
154         priority_id = database.get_priority_id(priority)
155         if priority_id == -1:
156             utils.warn("'%s' is not a valid priority. ['%s' in suite %s, component %s]." % (priority, package, suite, component))
157             c_error += 1
158             continue
159
160         if new.has_key(package):
161             utils.warn("Can't insert duplicate entry for '%s'; ignoring all but the first. [suite %s, component %s]" % (package, suite, component))
162             c_error += 1
163             continue
164         new[package] = ""
165         if original.has_key(package):
166             (old_priority_id, old_section_id, old_maintainer_override, old_priority, old_section) = original[package]
167             if action == "add" or old_priority_id == priority_id and \
168                old_section_id == section_id and \
169                old_maintainer_override == maintainer_override:
170                 # If it's unchanged or we're in 'add only' mode, ignore it
171                 c_skipped += 1
172                 continue
173             else:
174                 # If it's changed, delete the old one so we can
175                 # reinsert it with the new information
176                 c_updated += 1
177                 projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
178                                % (suite_id, component_id, package, type_id))
179                 # Log changes
180                 if old_priority_id != priority_id:
181                     Logger.log(["changed priority",package,old_priority,priority])
182                 if old_section_id != section_id:
183                     Logger.log(["changed section",package,old_section,section])
184                 if old_maintainer_override != maintainer_override:
185                     Logger.log(["changed maintainer override",package,old_maintainer_override,maintainer_override])
186                 update_p = 1
187         else:
188             c_added += 1
189             update_p = 0
190
191         if maintainer_override:
192             projectB.query("INSERT INTO override (suite, component, type, package, priority, section, maintainer) VALUES (%s, %s, %s, '%s', %s, %s, '%s')"
193                            % (suite_id, component_id, type_id, package, priority_id, section_id, maintainer_override))
194         else:
195             projectB.query("INSERT INTO override (suite, component, type, package, priority, section,maintainer) VALUES (%s, %s, %s, '%s', %s, %s, '')"
196                            % (suite_id, component_id, type_id, package, priority_id, section_id))
197
198         if not update_p:
199             Logger.log(["new override",suite,component,type,package,priority,section,maintainer_override])
200
201     if not action == "add":
202         # Delete any packages which were removed
203         for package in original.keys():
204             if not new.has_key(package):
205                 projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
206                                % (suite_id, component_id, package, type_id))
207                 c_removed += 1
208                 Logger.log(["removed override",suite,component,type,package])
209
210     projectB.query("COMMIT WORK")
211     if not Cnf["Control-Overrides::Options::Quiet"]:
212         print "Done in %d seconds. [Updated = %d, Added = %d, Removed = %d, Skipped = %d, Errors = %d]" % (int(time.time()-start_time), c_updated, c_added, c_removed, c_skipped, c_error)
213     Logger.log(["set complete",c_updated, c_added, c_removed, c_skipped, c_error])
214
215 ################################################################################
216
217 def list_overrides(suite, component, type):
218     suite_id = database.get_suite_id(suite)
219     if suite_id == -1:
220         utils.fubar("Suite '%s' not recognised." % (suite))
221
222     component_id = database.get_component_id(component)
223     if component_id == -1:
224         utils.fubar("Component '%s' not recognised." % (component))
225
226     type_id = database.get_override_type_id(type)
227     if type_id == -1:
228         utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc)" % (type))
229
230     if type == "dsc":
231         q = projectB.query("SELECT o.package, s.section, o.maintainer FROM override o, section s WHERE o.suite = %s AND o.component = %s AND o.type = %s AND o.section = s.id ORDER BY s.section, o.package" % (suite_id, component_id, type_id))
232         for i in q.getresult():
233             print utils.result_join(i)
234     else:
235         q = projectB.query("SELECT o.package, p.priority, s.section, o.maintainer, p.level FROM override o, priority p, section s WHERE o.suite = %s AND o.component = %s AND o.type = %s AND o.priority = p.id AND o.section = s.id ORDER BY s.section, p.level, o.package" % (suite_id, component_id, type_id))
236         for i in q.getresult():
237             print utils.result_join(i[:-1])
238
239 ################################################################################
240
241 def main ():
242     global Cnf, projectB, Logger
243
244     Cnf = utils.get_conf()
245     Arguments = [('a', "add", "Control-Overrides::Options::Add"),
246                  ('c', "component", "Control-Overrides::Options::Component", "HasArg"),
247                  ('h', "help", "Control-Overrides::Options::Help"),
248                  ('l', "list", "Control-Overrides::Options::List"),
249                  ('q', "quiet", "Control-Overrides::Options::Quiet"),
250                  ('s', "suite", "Control-Overrides::Options::Suite", "HasArg"),
251                  ('S', "set", "Control-Overrides::Options::Set"),
252                  ('t', "type", "Control-Overrides::Options::Type", "HasArg")]
253
254     # Default arguments
255     for i in [ "add", "help", "list", "quiet", "set" ]:
256         if not Cnf.has_key("Control-Overrides::Options::%s" % (i)):
257             Cnf["Control-Overrides::Options::%s" % (i)] = ""
258     if not Cnf.has_key("Control-Overrides::Options::Component"):
259         Cnf["Control-Overrides::Options::Component"] = "main"
260     if not Cnf.has_key("Control-Overrides::Options::Suite"):
261         Cnf["Control-Overrides::Options::Suite"] = "unstable"
262     if not Cnf.has_key("Control-Overrides::Options::Type"):
263         Cnf["Control-Overrides::Options::Type"] = "deb"
264
265     file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
266
267     if Cnf["Control-Overrides::Options::Help"]:
268         usage()
269
270     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
271     database.init(Cnf, projectB)
272
273     action = None
274     for i in [ "add", "list", "set" ]:
275         if Cnf["Control-Overrides::Options::%s" % (i)]:
276             if action:
277                 utils.fubar("Can not perform more than one action at once.")
278             action = i
279
280     (suite, component, otype) = (Cnf["Control-Overrides::Options::Suite"],
281                                  Cnf["Control-Overrides::Options::Component"],
282                                  Cnf["Control-Overrides::Options::Type"])
283
284     if action == "list":
285         list_overrides(suite, component, otype)
286     else:
287         if Cnf.has_key("Suite::%s::Untouchable" % suite) and Cnf["Suite::%s::Untouchable" % suite] != 0:
288             utils.fubar("%s: suite is untouchable" % suite)
289
290         Logger = logging.Logger(Cnf, "control-overrides")
291         if file_list:
292             for f in file_list:
293                 process_file(utils.open_file(f), suite, component, otype, action)
294         else:
295             process_file(sys.stdin, suite, component, otype, action)
296         Logger.close()
297
298 #######################################################################################
299
300 if __name__ == '__main__':
301     main()