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