]> git.decadent.org.uk Git - dak.git/blob - dak/control_overrides.py
69949e244f1589df8d6d19c59eb5152e7b4cf829
[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 import daklib.utils, daklib.database, daklib.logging
55
56 ################################################################################
57
58 Cnf = None
59 projectB = None
60 Logger = None
61
62 ################################################################################
63
64 def usage (exit_code=0):
65     print """Usage: dak control-overrides [OPTIONS]
66   -h, --help               print this help and exit
67
68   -c, --component=CMPT     list/set overrides by component
69                                   (contrib,*main,non-free)
70   -s, --suite=SUITE        list/set overrides by suite
71                                   (experimental,stable,testing,*unstable)
72   -t, --type=TYPE          list/set overrides by type
73                                   (*deb,dsc,udeb)
74
75   -a, --add                add overrides (changes and deletions are ignored)
76   -S, --set                set overrides
77   -l, --list               list overrides
78
79   -q, --quiet              be less verbose
80
81  starred (*) values are default"""
82     sys.exit(exit_code)
83
84 ################################################################################
85
86 def process_file (file, suite, component, type, action):
87     suite_id = daklib.database.get_suite_id(suite)
88     if suite_id == -1:
89         daklib.utils.fubar("Suite '%s' not recognised." % (suite))
90
91     component_id = daklib.database.get_component_id(component)
92     if component_id == -1:
93         daklib.utils.fubar("Component '%s' not recognised." % (component))
94
95     type_id = daklib.database.get_override_type_id(type)
96     if type_id == -1:
97         daklib.utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)" % (type))
98
99     # --set is done mostly internal for performance reasons; most
100     # invocations of --set will be updates and making people wait 2-3
101     # minutes while 6000 select+inserts are run needlessly isn't cool.
102
103     original = {}
104     new = {}
105     c_skipped = 0
106     c_added = 0
107     c_updated = 0
108     c_removed = 0
109     c_error = 0
110
111     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"
112                        % (suite_id, component_id, type_id))
113     for i in q.getresult():
114         original[i[0]] = i[1:]
115
116     start_time = time.time()
117     projectB.query("BEGIN WORK")
118     for line in file.readlines():
119         line = daklib.utils.re_comments.sub('', line).strip()
120         if line == "":
121             continue
122
123         maintainer_override = None
124         if type == "dsc":
125             split_line = line.split(None, 2)
126             if len(split_line) == 2:
127                 (package, section) = split_line
128             elif len(split_line) == 3:
129                 (package, section, maintainer_override) = split_line
130             else:
131                 daklib.utils.warn("'%s' does not break into 'package section [maintainer-override]'." % (line))
132                 c_error += 1
133                 continue
134             priority = "source"
135         else: # binary or udeb
136             split_line = line.split(None, 3)
137             if len(split_line) == 3:
138                 (package, priority, section) = split_line
139             elif len(split_line) == 4:
140                 (package, priority, section, maintainer_override) = split_line
141             else:
142                 daklib.utils.warn("'%s' does not break into 'package priority section [maintainer-override]'." % (line))
143                 c_error += 1
144                 continue
145
146         section_id = daklib.database.get_section_id(section)
147         if section_id == -1:
148             daklib.utils.warn("'%s' is not a valid section. ['%s' in suite %s, component %s]." % (section, package, suite, component))
149             c_error += 1
150             continue
151         priority_id = daklib.database.get_priority_id(priority)
152         if priority_id == -1:
153             daklib.utils.warn("'%s' is not a valid priority. ['%s' in suite %s, component %s]." % (priority, package, suite, component))
154             c_error += 1
155             continue
156
157         if new.has_key(package):
158             daklib.utils.warn("Can't insert duplicate entry for '%s'; ignoring all but the first. [suite %s, component %s]" % (package, suite, component))
159             c_error += 1
160             continue
161         new[package] = ""
162         if original.has_key(package):
163             (old_priority_id, old_section_id, old_maintainer_override, old_priority, old_section) = original[package]
164             if action == "add" or old_priority_id == priority_id and \
165                old_section_id == section_id and \
166                ((old_maintainer_override == maintainer_override) or \
167                 (old_maintainer_override == "" and maintainer_override == None)):
168                 # If it's unchanged or we're in 'add only' mode, ignore it
169                 c_skipped += 1
170                 continue
171             else:
172                 # If it's changed, delete the old one so we can
173                 # reinsert it with the new information
174                 c_updated += 1
175                 projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
176                                % (suite_id, component_id, package, type_id))
177                 # Log changes
178                 if old_priority_id != priority_id:
179                     Logger.log(["changed priority",package,old_priority,priority])
180                 if old_section_id != section_id:
181                     Logger.log(["changed section",package,old_section,section])
182                 if old_maintainer_override != maintainer_override:
183                     Logger.log(["changed maintainer override",package,old_maintainer_override,maintainer_override])
184                 update_p = 1
185         else:
186             c_added += 1
187             update_p = 0
188
189         if maintainer_override:
190             projectB.query("INSERT INTO override (suite, component, type, package, priority, section, maintainer) VALUES (%s, %s, %s, '%s', %s, %s, '%s')"
191                            % (suite_id, component_id, type_id, package, priority_id, section_id, maintainer_override))
192         else:
193             projectB.query("INSERT INTO override (suite, component, type, package, priority, section,maintainer) VALUES (%s, %s, %s, '%s', %s, %s, '')"
194                            % (suite_id, component_id, type_id, package, priority_id, section_id))
195
196         if not update_p:
197             Logger.log(["new override",suite,component,type,package,priority,section,maintainer_override])
198
199     if not action == "add":
200         # Delete any packages which were removed
201         for package in original.keys():
202             if not new.has_key(package):
203                 projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
204                                % (suite_id, component_id, package, type_id))
205                 c_removed += 1
206                 Logger.log(["removed override",suite,component,type,package])
207
208     projectB.query("COMMIT WORK")
209     if not Cnf["Control-Overrides::Options::Quiet"]:
210         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)
211     Logger.log(["set complete",c_updated, c_added, c_removed, c_skipped, c_error])
212
213 ################################################################################
214
215 def list(suite, component, type):
216     suite_id = daklib.database.get_suite_id(suite)
217     if suite_id == -1:
218         daklib.utils.fubar("Suite '%s' not recognised." % (suite))
219
220     component_id = daklib.database.get_component_id(component)
221     if component_id == -1:
222         daklib.utils.fubar("Component '%s' not recognised." % (component))
223
224     type_id = daklib.database.get_override_type_id(type)
225     if type_id == -1:
226         daklib.utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc)" % (type))
227
228     if type == "dsc":
229         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))
230         for i in q.getresult():
231             print daklib.utils.result_join(i)
232     else:
233         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))
234         for i in q.getresult():
235             print daklib.utils.result_join(i[:-1])
236
237 ################################################################################
238
239 def main ():
240     global Cnf, projectB, Logger
241
242     Cnf = daklib.utils.get_conf()
243     Arguments = [('a', "add", "Control-Overrides::Options::Add"),
244                  ('c', "component", "Control-Overrides::Options::Component", "HasArg"),
245                  ('h', "help", "Control-Overrides::Options::Help"),
246                  ('l', "list", "Control-Overrides::Options::List"),
247                  ('q', "quiet", "Control-Overrides::Options::Quiet"),
248                  ('s', "suite", "Control-Overrides::Options::Suite", "HasArg"),
249                  ('S', "set", "Control-Overrides::Options::Set"),
250                  ('t', "type", "Control-Overrides::Options::Type", "HasArg")]
251
252     # Default arguments
253     for i in [ "add", "help", "list", "quiet", "set" ]:
254         if not Cnf.has_key("Control-Overrides::Options::%s" % (i)):
255             Cnf["Control-Overrides::Options::%s" % (i)] = ""
256     if not Cnf.has_key("Control-Overrides::Options::Component"):
257         Cnf["Control-Overrides::Options::Component"] = "main"
258     if not Cnf.has_key("Control-Overrides::Options::Suite"):
259         Cnf["Control-Overrides::Options::Suite"] = "unstable"
260     if not Cnf.has_key("Control-Overrides::Options::Type"):
261         Cnf["Control-Overrides::Options::Type"] = "deb"
262
263     file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
264
265     if Cnf["Control-Overrides::Options::Help"]:
266         usage()
267
268     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
269     daklib.database.init(Cnf, projectB)
270
271     action = None
272     for i in [ "add", "list", "set" ]:
273         if Cnf["Control-Overrides::Options::%s" % (i)]:
274             if action:
275                 daklib.utils.fubar("Can not perform more than one action at once.")
276             action = i
277
278     (suite, component, type) = (Cnf["Control-Overrides::Options::Suite"],
279                                 Cnf["Control-Overrides::Options::Component"],
280                                 Cnf["Control-Overrides::Options::Type"])
281
282     if action == "list":
283         list(suite, component, type)
284     else:
285         Logger = daklib.logging.Logger(Cnf, "control-overrides")
286         if file_list:
287             for file in file_list:
288                 process_file(daklib.utils.open_file(file), suite, component, type, action)
289         else:
290             process_file(sys.stdin, suite, component, type, action)
291         Logger.close()
292
293 #######################################################################################
294
295 if __name__ == '__main__':
296     main()
297