3 # Manipulate override files
4 # Copyright (C) 2000, 2001 James Troup <james@nocrew.org>
5 # $Id: natalie.py,v 1.9 2001-09-13 23:51:51 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 import errno, os, pg, pwd, string, sys, time;
24 import utils, db_access, logging;
27 ################################################################################
33 ################################################################################
35 def usage (exit_code):
36 print """Usage: natalie.py [OPTIONS]
37 -D, --debug=VALUE debug
39 -V, --version retrieve version
40 -c, --component=CMPT list/set overrides by component
41 (contrib,*main,non-free)
42 -s, --suite=SUITE list/set overrides by suite
43 (experimental,stable,testing,*unstable)
44 -t, --type=TYPE list/set overrides by type
46 -S, --set set overrides from stdin
47 -l, --list list overrides on stdout
49 starred (*) values are default"""
52 ################################################################################
57 projectB = pg.connect('projectb', None);
58 db_access.init(Cnf, projectB);
60 def process_file (file, suite, component, type):
61 suite_id = db_access.get_suite_id(suite);
63 utils.fubar("Suite '%s' not recognised." % (suite));
65 component_id = db_access.get_component_id(component);
66 if component_id == -1:
67 utils.fubar("Component '%s' not recognised." % (component));
69 type_id = db_access.get_override_type_id(type);
71 utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)" % (type));
73 # --set is done mostly internal for performance reasons; most
74 # invocations of --set will be updates and making people wait 2-3
75 # minutes while 6000 select+inserts are run needlessly isn't cool.
85 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"
86 % (suite_id, component_id, type_id));
87 for i in q.getresult():
88 original[i[0]] = i[1:];
90 start_time = time.time();
91 projectB.query("BEGIN WORK");
92 for line in file.readlines():
93 line = string.strip(utils.re_comments.sub('', line[:-1]))
97 maintainer_override = None;
99 split_line = string.split(line, None, 2);
100 if len(split_line) == 2:
101 (package, section) = split_line;
102 elif len(split_line) == 3:
103 (package, section, maintainer_override) = split_line;
105 utils.warn("'%s' does not break into 'package section [maintainer-override]'." % (line));
106 c_error = c_error + 1;
109 else: # binary or udeb
110 split_line = string.split(line, None, 3);
111 if len(split_line) == 3:
112 (package, priority, section) = split_line;
113 elif len(split_line) == 4:
114 (package, priority, section, maintainer_override) = split_line;
116 utils.warn("'%s' does not break into 'package priority section [maintainer-override]'." % (line));
117 c_error = c_error + 1;
120 section_id = db_access.get_section_id(section);
122 utils.warn("'%s' is not a valid section. ['%s' in suite %s, component %s]." % (section, package, suite, component));
123 c_error = c_error + 1;
125 priority_id = db_access.get_priority_id(priority);
126 if priority_id == -1:
127 utils.warn("'%s' is not a valid priority. ['%s' in suite %s, component %s]." % (priority, package, suite, component));
128 c_error = c_error + 1;
131 if new.has_key(package):
132 utils.warn("Can't insert duplicate entry for '%s'; ignoring all but the first. [suite %s, component %s]" % (package, suite, component));
133 c_error = c_error + 1;
136 if original.has_key(package):
137 (old_priority_id, old_section_id, old_maintainer_override, old_priority, old_section) = original[package];
138 if old_priority_id == priority_id and old_section_id == section_id and old_maintainer_override == maintainer_override:
140 c_skipped = c_skipped + 1;
143 # Changed? Delete the old one so we can reinsert it with the new information
144 c_updated = c_updated + 1;
145 projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
146 % (suite_id, component_id, package, type_id));
148 if old_priority_id != priority_id:
149 Logger.log(["changed priority",package,old_priority,priority]);
150 if old_section_id != section_id:
151 Logger.log(["changed section",package,old_section,section]);
152 if old_maintainer_override != maintainer_override:
153 Logger.log(["changed maintainer override",package,old_maintainer_override,maintainer_override]);
156 c_added = c_added + 1;
159 if maintainer_override:
160 projectB.query("INSERT INTO override (suite, component, type, package, priority, section, maintainer) VALUES (%s, %s, %s, '%s', %s, %s, '%s')"
161 % (suite_id, component_id, type_id, package, priority_id, section_id, maintainer_override));
163 projectB.query("INSERT INTO override (suite, component, type, package, priority, section) VALUES (%s, %s, %s, '%s', %s, %s)"
164 % (suite_id, component_id, type_id, package, priority_id, section_id));
167 Logger.log(["new override",suite,component,type,package,priority,section,maintainer_override]);
169 # Delete any packages which were removed
170 for package in original.keys():
171 if not new.has_key(package):
172 projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
173 % (suite_id, component_id, package, type_id));
174 c_removed = c_removed + 1;
175 Logger.log(["removed override",suite,component,type,package]);
177 projectB.query("COMMIT WORK");
178 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);
179 Logger.log(["set complete",c_updated, c_added, c_removed, c_skipped, c_error]);
181 ################################################################################
183 def list(suite, component, type):
184 suite_id = db_access.get_suite_id(suite);
186 utils.fubar("Suite '%s' not recognised." % (suite));
188 component_id = db_access.get_component_id(component);
189 if component_id == -1:
190 utils.fubar("Component '%s' not recognised." % (component));
192 type_id = db_access.get_override_type_id(type);
194 utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc)" % (type));
197 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));
198 for i in q.getresult():
199 print string.join(i, '\t');
201 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));
202 for i in q.getresult():
204 print string.join(i[:-1], '\t');
206 print string.join(i[:-2], '\t');
208 ################################################################################
211 global Cnf, projectB, Logger;
215 Cnf = apt_pkg.newConfiguration();
216 apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
217 Arguments = [('D',"debug","Natalie::Options::Debug", "IntVal"),
218 ('h',"help","Natalie::Options::Help"),
219 ('V',"version","Natalie::Options::Version"),
220 ('c',"component", "Natalie::Options::Component", "HasArg"),
221 ('l',"list", "Natalie::Options::List"),
222 ('s',"suite","Natalie::Options::Suite", "HasArg"),
223 ('S',"set","Natalie::Options::Set"),
224 ('t',"type","Natalie::Options::Type", "HasArg")];
225 file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
227 if Cnf["Natalie::Options::Help"]:
233 for i in [ "list", "set" ]:
234 if Cnf["Natalie::Options::%s" % (i)]:
236 utils.fubar("Can not perform more than one action at once.");
239 (suite, component, type) = (Cnf["Natalie::Options::Suite"], Cnf["Natalie::Options::Component"], Cnf["Natalie::Options::Type"])
242 list(suite, component, type);
244 Logger = logging.Logger(Cnf, "natalie");
246 for file in file_list:
247 process_file(utils.open_file(file,'r'), suite, component, type);
249 process_file(sys.stdin, suite, component, type);
252 #######################################################################################
254 if __name__ == '__main__':