]> git.decadent.org.uk Git - dak.git/blob - natalie.py
sync
[dak.git] / natalie.py
1 #!/usr/bin/env python
2
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 $
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 init():
35     global projectB;
36     
37     projectB = pg.connect('projectb', 'localhost');
38     db_access.init(Cnf, projectB);
39
40 def process_file (file, suite, component, type, action):
41     suite_id = db_access.get_suite_id(suite);
42     if suite_id == -1:
43         sys.stderr.write("Suite '%s' not recognised.\n" % (suite));
44         sys.exit(2);
45
46     component_id = db_access.get_component_id(component);
47     if component_id == -1:
48         sys.stderr.write("Component '%s' not recognised.\n" % (component));
49         sys.exit(2);
50
51     type_id = db_access.get_override_type_id(type);
52     if type_id == -1:
53         sys.stderr.write("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)\n" % (type));
54         sys.exit(2);
55
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.
59     
60     original = {};
61     new = {};
62     c_skipped = 0;
63     c_added = 0;
64     c_updated = 0;
65     c_removed = 0;
66     c_error = 0;
67     
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:];
72
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]))
77         if line == "":
78             continue;
79         
80         maintainer_override = "";
81         if type == "dsc":
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;
87             else:
88                 sys.stderr.write("W: '%s' does not break into 'package section [maintainer override]'.\n" % (line));
89                 c_error = c_error + 1;
90                 continue;
91             priority = "source";
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;
98             else:
99                 sys.stderr.write("W: '%s' does not break into 'package priority section [maintainer override]'.\n" % (line));
100                 c_error = c_error + 1;
101                 continue;
102
103         section_id = db_access.get_section_id(section);
104         if section_id == -1:
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;
107             continue;
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;
112             continue;
113
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;
117             continue;
118         new[package] = "";
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:
122                 # Same?  Ignore it
123                 c_skipped = c_skipped + 1;
124                 continue; 
125             else:
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));
130         else:
131             c_added = c_added + 1;
132             
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));
136         else:
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));
139
140
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;
147
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);
150
151 ################################################################################
152
153 def list(suite, component, type):
154     suite_id = db_access.get_suite_id(suite);
155     if suite_id == -1:
156         sys.stderr.write("Suite '%s' not recognised.\n" % (suite));
157         sys.exit(2);
158
159     component_id = db_access.get_component_id(component);
160     if component_id == -1:
161         sys.stderr.write("Component '%s' not recognised.\n" % (component));
162         sys.exit(2);
163
164     type_id = db_access.get_override_type_id(type);
165     if type_id == -1:
166         sys.stderr.write("Type '%s' not recognised. (Valid types are deb, udeb and dsc.)\n" % (type));
167         sys.exit(2);
168
169     if type == "dsc":
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');
173     else:
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');
177
178 ################################################################################
179
180 def main ():
181     global Cnf, projectB;
182
183     apt_pkg.init();
184     
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);
196
197     init();
198
199     action = None;
200     for i in [ "list", "set" ]:
201         if Cnf["Natalie::Options::%s" % (i)]:
202             if action != None:
203                 sys.stderr.write("Can not perform more than one action at once.\n");
204                 sys.exit(2);
205             action = i;
206
207     (suite, component, type) = (Cnf["Natalie::Options::Suite"], Cnf["Natalie::Options::Component"], Cnf["Natalie::Options::Type"])
208
209     if action == "list":
210         list(suite, component, type);
211     else:
212         if file_list != []:
213             for file in file_list:
214                 process_file(utils.open_file(file,'r'), suite, component, type, action);
215         else:
216             process_file(sys.stdin, suite, component, type, action);
217
218 #######################################################################################
219
220 if __name__ == '__main__':
221     main()
222