]> git.decadent.org.uk Git - dak.git/blob - natalie.py
changelog entry for ryan's shania fix; whitespace + override fix for w-p-u; copyright...
[dak.git] / natalie.py
1 #!/usr/bin/env python
2
3 # Manipulate override files
4 # Copyright (C) 2000, 2001  James Troup <james@nocrew.org>
5 # $Id: natalie.py,v 1.12 2001-11-04 22:28:44 troup Exp $
6
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.
11
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.
16
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
20
21 ################################################################################
22
23 import errno, os, pg, pwd, string, sys, time;
24 import utils, db_access, logging;
25 import apt_pkg;
26
27 ################################################################################
28
29 Cnf = None;
30 projectB = None;
31 Logger = None;
32
33 ################################################################################
34
35 def usage (exit_code=0):
36     print """Usage: natalie.py [OPTIONS]
37   -h, --help               this help
38   -c, --component=CMPT     list/set overrides by component
39                                   (contrib,*main,non-free)
40   -s, --suite=SUITE        list/set overrides by suite
41                                   (experimental,stable,testing,*unstable)
42   -t, --type=TYPE          list/set overrides by type
43                                   (*deb,dsc,udeb)
44   -S, --set                set overrides from stdin
45   -l, --list               list overrides on stdout
46
47  starred (*) values are default"""
48     sys.exit(exit_code)
49
50 ################################################################################
51
52 def init ():
53     global projectB;
54
55     projectB = pg.connect('projectb', None);
56     db_access.init(Cnf, projectB);
57
58 def process_file (file, suite, component, type):
59     suite_id = db_access.get_suite_id(suite);
60     if suite_id == -1:
61         utils.fubar("Suite '%s' not recognised." % (suite));
62
63     component_id = db_access.get_component_id(component);
64     if component_id == -1:
65         utils.fubar("Component '%s' not recognised." % (component));
66
67     type_id = db_access.get_override_type_id(type);
68     if type_id == -1:
69         utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)" % (type));
70
71     # --set is done mostly internal for performance reasons; most
72     # invocations of --set will be updates and making people wait 2-3
73     # minutes while 6000 select+inserts are run needlessly isn't cool.
74
75     original = {};
76     new = {};
77     c_skipped = 0;
78     c_added = 0;
79     c_updated = 0;
80     c_removed = 0;
81     c_error = 0;
82
83     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"
84                        % (suite_id, component_id, type_id));
85     for i in q.getresult():
86         original[i[0]] = i[1:];
87
88     start_time = time.time();
89     projectB.query("BEGIN WORK");
90     for line in file.readlines():
91         line = string.strip(utils.re_comments.sub('', line[:-1]))
92         if line == "":
93             continue;
94
95         maintainer_override = None;
96         if type == "dsc":
97             split_line = string.split(line, None, 2);
98             if len(split_line) == 2:
99                 (package, section) = split_line;
100             elif len(split_line) == 3:
101                 (package, section, maintainer_override) = split_line;
102             else:
103                 utils.warn("'%s' does not break into 'package section [maintainer-override]'." % (line));
104                 c_error = c_error + 1;
105                 continue;
106             priority = "source";
107         else: # binary or udeb
108             split_line = string.split(line, None, 3);
109             if len(split_line) == 3:
110                 (package, priority, section) = split_line;
111             elif len(split_line) == 4:
112                 (package, priority, section, maintainer_override) = split_line;
113             else:
114                 utils.warn("'%s' does not break into 'package priority section [maintainer-override]'." % (line));
115                 c_error = c_error + 1;
116                 continue;
117
118         section_id = db_access.get_section_id(section);
119         if section_id == -1:
120             utils.warn("'%s' is not a valid section. ['%s' in suite %s, component %s]." % (section, package, suite, component));
121             c_error = c_error + 1;
122             continue;
123         priority_id = db_access.get_priority_id(priority);
124         if priority_id == -1:
125             utils.warn("'%s' is not a valid priority. ['%s' in suite %s, component %s]." % (priority, package, suite, component));
126             c_error = c_error + 1;
127             continue;
128
129         if new.has_key(package):
130             utils.warn("Can't insert duplicate entry for '%s'; ignoring all but the first. [suite %s, component %s]" % (package, suite, component));
131             c_error = c_error + 1;
132             continue;
133         new[package] = "";
134         if original.has_key(package):
135             (old_priority_id, old_section_id, old_maintainer_override, old_priority, old_section) = original[package];
136             if old_priority_id == priority_id and old_section_id == section_id and old_maintainer_override == maintainer_override:
137                 # Same?  Ignore it
138                 c_skipped = c_skipped + 1;
139                 continue;
140             else:
141                 # Changed?  Delete the old one so we can reinsert it with the new information
142                 c_updated = c_updated + 1;
143                 projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
144                                % (suite_id, component_id, package, type_id));
145                 # Log changes
146                 if old_priority_id != priority_id:
147                     Logger.log(["changed priority",package,old_priority,priority]);
148                 if old_section_id != section_id:
149                     Logger.log(["changed section",package,old_section,section]);
150                 if old_maintainer_override != maintainer_override:
151                     Logger.log(["changed maintainer override",package,old_maintainer_override,maintainer_override]);
152                 update_p = 1;
153         else:
154             c_added = c_added + 1;
155             update_p = 0;
156
157         if maintainer_override:
158             projectB.query("INSERT INTO override (suite, component, type, package, priority, section, maintainer) VALUES (%s, %s, %s, '%s', %s, %s, '%s')"
159                            % (suite_id, component_id, type_id, package, priority_id, section_id, maintainer_override));
160         else:
161             projectB.query("INSERT INTO override (suite, component, type, package, priority, section) VALUES (%s, %s, %s, '%s', %s, %s)"
162                            % (suite_id, component_id, type_id, package, priority_id, section_id));
163
164         if not update_p:
165             Logger.log(["new override",suite,component,type,package,priority,section,maintainer_override]);
166
167     # Delete any packages which were removed
168     for package in original.keys():
169         if not new.has_key(package):
170             projectB.query("DELETE FROM override WHERE suite = %s AND component = %s AND package = '%s' AND type = %s"
171                            % (suite_id, component_id, package, type_id));
172             c_removed = c_removed + 1;
173             Logger.log(["removed override",suite,component,type,package]);
174
175     projectB.query("COMMIT WORK");
176     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);
177     Logger.log(["set complete",c_updated, c_added, c_removed, c_skipped, c_error]);
178
179 ################################################################################
180
181 def list(suite, component, type):
182     suite_id = db_access.get_suite_id(suite);
183     if suite_id == -1:
184         utils.fubar("Suite '%s' not recognised." % (suite));
185
186     component_id = db_access.get_component_id(component);
187     if component_id == -1:
188         utils.fubar("Component '%s' not recognised." % (component));
189
190     type_id = db_access.get_override_type_id(type);
191     if type_id == -1:
192         utils.fubar("Type '%s' not recognised. (Valid types are deb, udeb and dsc)" % (type));
193
194     if type == "dsc":
195         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));
196         for i in q.getresult():
197             print utils.result_join(i);
198     else:
199         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));
200         for i in q.getresult():
201             print utils.result_join(i[:-1]);
202
203 ################################################################################
204
205 def main ():
206     global Cnf, projectB, Logger;
207
208     apt_pkg.init();
209
210     Cnf = apt_pkg.newConfiguration();
211     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
212     Arguments = [('h',"help","Natalie::Options::Help"),
213                  ('c',"component", "Natalie::Options::Component", "HasArg"),
214                  ('l',"list", "Natalie::Options::List"),
215                  ('s',"suite","Natalie::Options::Suite", "HasArg"),
216                  ('S',"set","Natalie::Options::Set"),
217                  ('t',"type","Natalie::Options::Type", "HasArg")];
218
219     # Default arguments
220     for i in ["help", "list", "set" ]:
221         Cnf["Natalie::Options::%s" % (i)] = "";
222     Cnf["Natalie::Options::Component"] = "main";
223     Cnf["Natalie::Options::Suite"] = "unstable";
224     Cnf["Natalie::Options::Type"] = "deb";
225
226     file_list = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
227
228     if Cnf["Natalie::Options::Help"]:
229         usage();
230
231     init();
232
233     action = None;
234     for i in [ "list", "set" ]:
235         if Cnf["Natalie::Options::%s" % (i)]:
236             if action != None:
237                 utils.fubar("Can not perform more than one action at once.");
238             action = i;
239
240     (suite, component, type) = (Cnf["Natalie::Options::Suite"], Cnf["Natalie::Options::Component"], Cnf["Natalie::Options::Type"])
241
242     if action == "list":
243         list(suite, component, type);
244     else:
245         Logger = logging.Logger(Cnf, "natalie");
246         if file_list != []:
247             for file in file_list:
248                 process_file(utils.open_file(file), suite, component, type);
249         else:
250             process_file(sys.stdin, suite, component, type);
251         Logger.close();
252
253 #######################################################################################
254
255 if __name__ == '__main__':
256     main()
257