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