3 # Manipulate override files
4 # Copyright (C) 2000, 2001 James Troup <james@nocrew.org>
5 # $Id: natalie.py,v 1.5 2001-04-13 20:13:36 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
27 ################################################################################
32 ################################################################################
34 def usage (exit_code):
35 print """Usage: natalie.py [OPTIONS]
36 -D, --debug=VALUE debug
38 -V, --version retrieve version
39 -c, --component=CMPT list/set overrides by component
40 (contrib,*main,non-free)
41 -s, --suite=SUITE list/set overrides by suite
42 (experimental,stable,testing,*unstable)
43 -t, --type=TYPE list/set overrides by type
45 -S, --set set overrides from stdin
46 -l, --list list overrides on stdout
48 starred (*) values are default"""
51 ################################################################################
56 projectB = pg.connect('projectb', None);
57 db_access.init(Cnf, projectB);
59 def process_file (file, suite, component, type, action):
60 suite_id = db_access.get_suite_id(suite);
62 utils.fubar("Suite '%s' not recognised." % (suite));
64 component_id = db_access.get_component_id(component);
65 if component_id == -1:
66 utils.fubar("Component '%s' not recognised." % (component));
68 type_id = db_access.get_override_type_id(type);
70 utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)" % (type));
72 # --set is done mostly internal for performance reasons; most
73 # invocations of --set will be updates and making people wait 2-3
74 # minutes while 6000 select+inserts are run needlessly isn't cool.
84 q = projectB.query("SELECT package, priority, section, maintainer FROM override WHERE suite = %s AND component = %s AND type = %s"
85 % (suite_id, component_id, type_id));
86 for i in q.getresult():
87 original[i[0]] = i[1:];
89 start_time = time.time();
90 projectB.query("BEGIN WORK");
91 for line in file.readlines():
92 line = string.strip(utils.re_comments.sub('', line[:-1]))
96 maintainer_override = "";
98 split_line = string.split(line, None, 2);
99 if len(split_line) == 2:
100 (package, section) = split_line;
101 elif len(split_line) == 3:
102 (package, section, maintainer_override) = split_line;
104 utils.warn("'%s' does not break into 'package section [maintainer-override]'." % (line));
105 c_error = c_error + 1;
108 else: # binary or udeb
109 split_line = string.split(line, None, 3);
110 if len(split_line) == 3:
111 (package, priority, section) = split_line;
112 elif len(split_line) == 4:
113 (package, priority, section, maintainer_override) = split_line;
115 utils.warn("'%s' does not break into 'package priority section [maintainer-override]'." % (line));
116 c_error = c_error + 1;
119 section_id = db_access.get_section_id(section);
121 utils.warn("'%s' is not a valid section. ['%s' in suite %s, component %s]." % (section, package, suite, component));
122 c_error = c_error + 1;
124 priority_id = db_access.get_priority_id(priority);
125 if priority_id == -1:
126 utils.warn("'%s' is not a valid priority. ['%s' in suite %s, component %s]." % (priority, package, suite, component));
127 c_error = c_error + 1;
130 if new.has_key(package):
131 utils.warn("Can't insert duplicate entry for '%s'; ignoring all but the first. [suite %s, component %s]" % (package, suite, component));
132 c_error = c_error + 1;
135 if original.has_key(package):
136 (old_priority_id, old_section_id, old_maintainer_override) = original[package];
137 if old_priority_id == priority_id and old_section_id == section_id and old_maintainer_override == maintainer_override:
139 c_skipped = c_skipped + 1;
142 # Changed? Delete the old one so we can reinsert it with the new information
143 c_updated = c_updated + 1;
144 projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
145 % (suite_id, component_id, package, type_id));
147 c_added = c_added + 1;
149 if maintainer_override:
150 projectB.query("INSERT INTO override (suite, component, type, package, priority, section, maintainer) VALUES (%s, %s, %s, '%s', %s, %s, '%s')"
151 % (suite_id, component_id, type_id, package, priority_id, section_id, maintainer_override));
153 projectB.query("INSERT INTO override (suite, component, type, package, priority, section) VALUES (%s, %s, %s, '%s', %s, %s)"
154 % (suite_id, component_id, type_id, package, priority_id, section_id));
157 # Delete any packages which were removed
158 for package in original.keys():
159 if not new.has_key(package):
160 projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
161 % (suite_id, component_id, package, type_id));
162 c_removed = c_removed + 1;
164 projectB.query("COMMIT WORK");
165 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);
167 ################################################################################
169 def list(suite, component, type):
170 suite_id = db_access.get_suite_id(suite);
172 utils.fubar("Suite '%s' not recognised." % (suite));
174 component_id = db_access.get_component_id(component);
175 if component_id == -1:
176 utils.fubar("Component '%s' not recognised." % (component));
178 type_id = db_access.get_override_type_id(type);
180 utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)\n" % (type));
183 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));
184 for i in q.getresult():
185 print string.join(i, '\t');
187 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));
188 for i in q.getresult():
189 print string.join(i[:-1], '\t');
191 ################################################################################
194 global Cnf, projectB;
198 Cnf = apt_pkg.newConfiguration();
199 apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
200 Arguments = [('D',"debug","Natalie::Options::Debug", "IntVal"),
201 ('h',"help","Natalie::Options::Help"),
202 ('V',"version","Natalie::Options::Version"),
203 ('c',"component", "Natalie::Options::Component", "HasArg"),
204 ('l',"list", "Natalie::Options::List"),
205 ('s',"suite","Natalie::Options::Suite", "HasArg"),
206 ('S',"set","Natalie::Options::Set"),
207 ('t',"type","Natalie::Options::Type", "HasArg")];
208 file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
210 if Cnf["Natalie::Options::Help"]:
216 for i in [ "list", "set" ]:
217 if Cnf["Natalie::Options::%s" % (i)]:
219 utils.fubar("Can not perform more than one action at once.");
222 (suite, component, type) = (Cnf["Natalie::Options::Suite"], Cnf["Natalie::Options::Component"], Cnf["Natalie::Options::Type"])
225 list(suite, component, type);
228 for file in file_list:
229 process_file(utils.open_file(file,'r'), suite, component, type, action);
231 process_file(sys.stdin, suite, component, type, action);
233 #######################################################################################
235 if __name__ == '__main__':