3 # Manipulate override files
4 # Copyright (C) 2000 James Troup <james@nocrew.org>
5 # $Id: natalie.py,v 1.1 2001-01-10 05:58:26 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 pg, string, sys, time
24 import utils, db_access
27 ################################################################################
32 ################################################################################
37 projectB = pg.connect('projectb', 'localhost');
38 db_access.init(Cnf, projectB);
40 def process_file (file, suite, component, type, action):
41 suite_id = db_access.get_suite_id(suite);
43 sys.stderr.write("Suite '%s' not recognised.\n" % (suite));
46 component_id = db_access.get_component_id(component);
47 if component_id == -1:
48 sys.stderr.write("Component '%s' not recognised.\n" % (component));
51 type_id = db_access.get_override_type_id(type);
53 sys.stderr.write("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)\n" % (type));
56 # --set is done mostly internal for performance reasons; most
57 # invocations of --set will be updates and making people wait 2-3
58 # minutes while 6000 select+inserts are run needlessly isn't cool.
68 q = projectB.query("SELECT package, priority, section, maintainer FROM override WHERE suite = %s AND component = %s AND type = %s"
69 % (suite_id, component_id, type_id));
70 for i in q.getresult():
71 original[i[0]] = i[1:];
73 start_time = time.time();
74 projectB.query("BEGIN WORK");
75 for line in file.readlines():
76 line = string.strip(utils.re_comments.sub('', line[:-1]))
80 maintainer_override = "";
82 split_line = string.split(line, None, 2);
83 if len(split_line) == 2:
84 (package, section) = split_line;
85 elif len(split_line) == 3:
86 (package, section, maintainer_override) = split_line;
88 sys.stderr.write("W: '%s' does not break into 'package section [maintainer override]'.\n" % (line));
89 c_error = c_error + 1;
92 else: # binary or udeb
93 split_line = string.split(line, None, 3);
94 if len(split_line) == 3:
95 (package, priority, section) = split_line;
96 elif len(split_line) == 4:
97 (package, priority, section, maintainer_override) = split_line;
99 sys.stderr.write("W: '%s' does not break into 'package priority section [maintainer override]'.\n" % (line));
100 c_error = c_error + 1;
103 section_id = db_access.get_section_id(section);
105 sys.stderr.write("W: '%s' is not a valid section. ['%s' in suite %s, component %s].\n" % (section, package, suite, component));
106 c_error = c_error + 1;
108 priority_id = db_access.get_priority_id(priority);
109 if priority_id == -1:
110 sys.stderr.write("W: '%s' is not a valid priority. ['%s' in suite %s, component %s].\n" % (priority, package, suite, component));
111 c_error = c_error + 1;
114 if new.has_key(package):
115 sys.stderr.write("W: Can't insert duplicate entry for '%s'; ignoring all but the first. [suite %s, component %s]\n" % (package, suite, component));
116 c_error = c_error + 1;
119 if original.has_key(package):
120 (old_priority_id, old_section_id, old_maintainer_override) = original[package];
121 if old_priority_id == priority_id and old_section_id == section_id and old_maintainer_override == maintainer_override:
123 c_skipped = c_skipped + 1;
126 # Changed? Delete the old one so we can reinsert it with the new information
127 c_updated = c_updated + 1;
128 projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
129 % (suite_id, component_id, package, type_id));
131 c_added = c_added + 1;
133 if maintainer_override:
134 projectB.query("INSERT INTO override (suite, component, type, package, priority, section, maintainer) VALUES (%s, %s, %s, '%s', %s, %s, '%s')"
135 % (suite_id, component_id, type_id, package, priority_id, section_id, maintainer_override));
137 projectB.query("INSERT INTO override (suite, component, type, package, priority, section) VALUES (%s, %s, %s, '%s', %s, %s)"
138 % (suite_id, component_id, type_id, package, priority_id, section_id));
141 # Delete any packages which were removed
142 for package in original.keys():
143 if not new.has_key(package):
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));
146 c_removed = c_removed + 1;
148 projectB.query("COMMIT WORK");
149 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);
151 ################################################################################
153 def list(suite, component, type):
154 suite_id = db_access.get_suite_id(suite);
156 sys.stderr.write("Suite '%s' not recognised.\n" % (suite));
159 component_id = db_access.get_component_id(component);
160 if component_id == -1:
161 sys.stderr.write("Component '%s' not recognised.\n" % (component));
164 type_id = db_access.get_override_type_id(type);
166 sys.stderr.write("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)\n" % (type));
170 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));
171 for i in q.getresult():
172 print string.join(i, '\t');
174 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));
175 for i in q.getresult():
176 print string.join(i[:-1], '\t');
178 ################################################################################
181 global Cnf, projectB;
185 Cnf = apt_pkg.newConfiguration();
186 apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
187 Arguments = [('D',"debug","Natalie::Options::Debug", "IntVal"),
188 ('h',"help","Natalie::Options::Help"),
189 ('V',"version","Natalie::Options::Version"),
190 ('c',"component", "Natalie::Options::Component", "HasArg"),
191 ('l',"list", "Natalie::Options::List"),
192 ('s',"suite","Natalie::Options::Suite", "HasArg"),
193 ('S',"set","Natalie::Options::Set"),
194 ('t',"type","Natalie::Options::Type", "HasArg")];
195 file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
200 for i in [ "list", "set" ]:
201 if Cnf["Natalie::Options::%s" % (i)]:
203 sys.stderr.write("Can not perform more than one action at once.\n");
207 (suite, component, type) = (Cnf["Natalie::Options::Suite"], Cnf["Natalie::Options::Component"], Cnf["Natalie::Options::Type"])
210 list(suite, component, type);
213 for file in file_list:
214 process_file(utils.open_file(file,'r'), suite, component, type, action);
216 process_file(sys.stdin, suite, component, type, action);
218 #######################################################################################
220 if __name__ == '__main__':