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