3 # Manipulate override files
4 # Copyright (C) 2000, 2001, 2002, 2003 James Troup <james@nocrew.org>
5 # $Id: natalie,v 1.6 2003-04-15 16:03:31 troup Exp $
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.
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.
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
21 ################################################################################
23 # On 30 Nov 1998, James Troup wrote:
25 # > James Troup<2> <troup2@debian.org>
27 # > James is a clone of James; he's going to take over the world.
28 # > After he gets some sleep.
30 # Could you clone other things too? Sheep? Llamas? Giant mutant turnips?
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!
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
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
46 # Chicken. Black. Helicopters.
49 # <Pine.LNX.3.96.981130011300.30365Z-100000@wakko>
51 ################################################################################
54 import utils, db_access, logging;
57 ################################################################################
63 ################################################################################
65 def usage (exit_code=0):
66 print """Usage: natalie.py [OPTIONS]
67 -h, --help print this help and exit
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
76 -a, --add add overrides (changes and deletions are ignored)
77 -S, --set set overrides
78 -l, --list list overrides
80 -q, --quiet be less verbose
82 starred (*) values are default"""
85 ################################################################################
87 def process_file (file, suite, component, type, action):
88 suite_id = db_access.get_suite_id(suite);
90 utils.fubar("Suite '%s' not recognised." % (suite));
92 component_id = db_access.get_component_id(component);
93 if component_id == -1:
94 utils.fubar("Component '%s' not recognised." % (component));
96 type_id = db_access.get_override_type_id(type);
98 utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)" % (type));
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.
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:];
117 start_time = time.time();
118 projectB.query("BEGIN WORK");
119 for line in file.readlines():
120 line = utils.re_comments.sub('', line).strip();
124 maintainer_override = None;
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;
132 utils.warn("'%s' does not break into 'package section [maintainer-override]'." % (line));
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;
143 utils.warn("'%s' does not break into 'package priority section [maintainer-override]'." % (line));
147 section_id = db_access.get_section_id(section);
149 utils.warn("'%s' is not a valid section. ['%s' in suite %s, component %s]." % (section, package, suite, component));
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));
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));
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:
168 # If it's unchanged or we're in 'add only' mode, ignore it
172 # If it's changed, delete the old one so we can
173 # reinsert it with the new information
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));
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]);
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));
193 projectB.query("INSERT INTO override (suite, component, type, package, priority, section) VALUES (%s, %s, %s, '%s', %s, %s)"
194 % (suite_id, component_id, type_id, package, priority_id, section_id));
197 Logger.log(["new override",suite,component,type,package,priority,section,maintainer_override]);
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));
206 Logger.log(["removed override",suite,component,type,package]);
208 projectB.query("COMMIT WORK");
209 if not Cnf["Natalie::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]);
213 ################################################################################
215 def list(suite, component, type):
216 suite_id = db_access.get_suite_id(suite);
218 utils.fubar("Suite '%s' not recognised." % (suite));
220 component_id = db_access.get_component_id(component);
221 if component_id == -1:
222 utils.fubar("Component '%s' not recognised." % (component));
224 type_id = db_access.get_override_type_id(type);
226 utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc)" % (type));
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 utils.result_join(i);
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 utils.result_join(i[:-1]);
237 ################################################################################
240 global Cnf, projectB, Logger;
242 Cnf = utils.get_conf();
243 Arguments = [('a', "add", "Natalie::Options::Add"),
244 ('c', "component", "Natalie::Options::Component", "HasArg"),
245 ('h', "help", "Natalie::Options::Help"),
246 ('l', "list", "Natalie::Options::List"),
247 ('q', "quiet", "Natalie::Options::Quiet"),
248 ('s', "suite", "Natalie::Options::Suite", "HasArg"),
249 ('S', "set", "Natalie::Options::Set"),
250 ('t', "type", "Natalie::Options::Type", "HasArg")];
253 for i in [ "add", "help", "list", "quiet", "set" ]:
254 if not Cnf.has_key("Natalie::Options::%s" % (i)):
255 Cnf["Natalie::Options::%s" % (i)] = "";
256 if not Cnf.has_key("Natalie::Options::Component"):
257 Cnf["Natalie::Options::Component"] = "main";
258 if not Cnf.has_key("Natalie::Options::Suite"):
259 Cnf["Natalie::Options::Suite"] = "unstable";
260 if not Cnf.has_key("Natalie::Options::Type"):
261 Cnf["Natalie::Options::Type"] = "deb";
263 file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
265 if Cnf["Natalie::Options::Help"]:
268 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
269 db_access.init(Cnf, projectB);
272 for i in [ "add", "list", "set" ]:
273 if Cnf["Natalie::Options::%s" % (i)]:
275 utils.fubar("Can not perform more than one action at once.");
278 (suite, component, type) = (Cnf["Natalie::Options::Suite"], Cnf["Natalie::Options::Component"], Cnf["Natalie::Options::Type"])
281 list(suite, component, type);
283 Logger = logging.Logger(Cnf, "natalie");
285 for file in file_list:
286 process_file(utils.open_file(file), suite, component, type, action);
288 process_file(sys.stdin, suite, component, type, action);
291 #######################################################################################
293 if __name__ == '__main__':