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