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 $
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) or \
168 (old_maintainer_override == "" and maintainer_override == None)):
169 # If it's unchanged or we're in 'add only' mode, ignore it
173 # If it's changed, delete the old one so we can
174 # reinsert it with the new information
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));
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]);
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));
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));
198 Logger.log(["new override",suite,component,type,package,priority,section,maintainer_override]);
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));
207 Logger.log(["removed override",suite,component,type,package]);
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]);
214 ################################################################################
216 def list(suite, component, type):
217 suite_id = db_access.get_suite_id(suite);
219 utils.fubar("Suite '%s' not recognised." % (suite));
221 component_id = db_access.get_component_id(component);
222 if component_id == -1:
223 utils.fubar("Component '%s' not recognised." % (component));
225 type_id = db_access.get_override_type_id(type);
227 utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc)" % (type));
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);
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]);
238 ################################################################################
241 global Cnf, projectB, Logger;
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")];
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";
264 file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
266 if Cnf["Natalie::Options::Help"]:
269 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
270 db_access.init(Cnf, projectB);
273 for i in [ "add", "list", "set" ]:
274 if Cnf["Natalie::Options::%s" % (i)]:
276 utils.fubar("Can not perform more than one action at once.");
279 (suite, component, type) = (Cnf["Natalie::Options::Suite"], Cnf["Natalie::Options::Component"], Cnf["Natalie::Options::Type"])
282 list(suite, component, type);
284 Logger = logging.Logger(Cnf, "natalie");
286 for file in file_list:
287 process_file(utils.open_file(file), suite, component, type, action);
289 process_file(sys.stdin, suite, component, type, action);
292 #######################################################################################
294 if __name__ == '__main__':