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