]> git.decadent.org.uk Git - dak.git/blob - halle
parse_changes and build_file_list 2nd argument sanity changes
[dak.git] / halle
1 #!/usr/bin/env python
2
3 # Remove obsolete .changes files from proposed-updates
4 # Copyright (C) 2001, 2002  James Troup <james@nocrew.org>
5 # $Id: halle,v 1.7 2002-05-18 23:54: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 os, pg, re, sys, string;
24 import utils, db_access;
25 import apt_pkg;
26
27 ################################################################################
28
29 Cnf = None;
30 projectB = None;
31 Options = None;
32 pu = {};
33
34 re_isdeb = re.compile (r"^(.+)_(.+?)_(.+?).deb$");
35
36 ################################################################################
37
38 def usage (exit_code=0):
39     print """Usage: halle [OPTION] <CHANGES FILE | ADMIN FILE>[...]
40 Remove obsolete changes files from proposed-updates.
41
42   -v, --verbose              be more verbose about what is being done
43   -h, --help                 show this help and exit
44
45 Need either changes files or an admin.txt file with a '.joey' suffix."""
46     sys.exit(exit_code)
47
48 ################################################################################
49
50 def check_changes (filename):
51     try:
52         changes = utils.parse_changes(filename);
53         files = utils.build_file_list(changes);
54     except:
55         utils.warn("Couldn't read changes file '%s'." % (filename));
56         return;
57     num_files = len(files.keys());
58     for file in files.keys():
59         if utils.re_isadeb.match(file) != None:
60             m = re_isdeb.match(file);
61             pkg = m.group(1);
62             version = m.group(2);
63             arch = m.group(3);
64             if Options["debug"]:
65                 print "BINARY: %s ==> %s_%s_%s" % (file, pkg, version, arch);
66         else:
67             m = utils.re_issource.match(file)
68             if m != None:
69                 pkg = m.group(1);
70                 version = m.group(2);
71                 type = m.group(3);
72                 if type != "dsc":
73                     del files[file];
74                     num_files = num_files - 1;
75                     continue;
76                 arch = "source";
77                 if Options["debug"]:
78                     print "SOURCE: %s ==> %s_%s_%s" % (file, pkg, version, arch);
79             else:
80                 utils.fubar("unknown type, fix me");
81         if not pu.has_key(pkg):
82             utils.fubar("%s doesn't seem to exist in p-u?? (from %s [%s])" % (pkg, file, filename));
83         if not pu[pkg].has_key(arch):
84             utils.fubar("%s doesn't seem to exist for %s in p-u?? (from %s [%s])" % (pkg, arch, file, filename));
85         pu_version = utils.re_no_epoch.sub('', pu[pkg][arch]);
86         if pu_version == version:
87             if Options["verbose"]:
88                 print "%s: ok" % (file);
89         else:
90             if Options["verbose"]:
91                 print "%s: superseded, removing. [%s]" % (file, pu_version);
92             del files[file];
93
94     new_num_files = len(files.keys());
95     if new_num_files == 0:
96         print "%s: no files left, superseded by %s" % (filename, pu_version);
97         dest = Cnf["Dir::Morgue"] + "/misc/";
98         utils.move(filename, dest);
99     elif new_num_files < num_files:
100         print "%s: lost files, MWAAP." % (filename);
101     else:
102         if Options["verbose"]:
103             print "%s: ok" % (filename);
104
105 ################################################################################
106
107 def check_joey (filename):
108     file = utils.open_file(filename);
109
110     cwd = os.getcwd();
111     os.chdir("%s/dists/proposed-updates" % (Cnf["Dir::Root"]));
112
113     for line in file.readlines():
114         line = line[:-1];
115         if string.find(line, 'install') != -1:
116             split_line = string.split(line);
117             install_type = split_line[0];
118             if [ "install", "install-u", "sync-install" ].count(install_type) == 0:
119                 utils.fubar("Unknown install type ('%s') from: %s" % (install_type, line));
120             changes_filename = split_line[1]
121             if len(split_line) != 2:
122                 utils.fubar("Parse error (more than 2 elements): %s" % (line));
123             if Options["debug"]:
124                 print "Processing %s..." % (changes_filename);
125             check_changes(changes_filename);
126
127     os.chdir(cwd);
128
129 ################################################################################
130
131 def init_pu ():
132     global pu;
133
134     q = projectB.query("""
135 SELECT b.package, b.version, a.arch_string
136   FROM bin_associations ba, binaries b, suite su, architecture a
137   WHERE b.id = ba.bin AND ba.suite = su.id
138     AND su.suite_name = 'proposed-updates' AND a.id = b.architecture
139 UNION SELECT s.source, s.version, 'source'
140   FROM src_associations sa, source s, suite su
141   WHERE s.id = sa.source AND sa.suite = su.id
142     AND su.suite_name = 'proposed-updates'
143 ORDER BY package, version, arch_string;
144 """);
145     ql = q.getresult();
146     for i in ql:
147         pkg = i[0];
148         version = i[1];
149         arch = i[2];
150         if not pu.has_key(pkg):
151             pu[pkg] = {};
152         pu[pkg][arch] = version;
153
154 def main ():
155     global Cnf, projectB, Options;
156
157     Cnf = utils.get_conf()
158
159     Arguments = [('d', "debug", "Halle::Options::Debug"),
160                  ('v',"verbose","Halle::Options::Verbose"),
161                  ('h',"help","Halle::Options::Help")];
162     for i in [ "debug", "verbose", "help" ]:
163         if not Cnf.has_key("Halle::Options::%s" % (i)):
164             Cnf["Halle::Options::%s" % (i)] = "";
165
166     arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
167     Options = Cnf.SubTree("Halle::Options")
168
169     if Options["Help"]:
170         usage(0);
171     if not arguments:
172         utils.fubar("need at least one package name as an argument.");
173
174     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
175     db_access.init(Cnf, projectB);
176
177     init_pu();
178
179     for file in arguments:
180         if file[-8:] == ".changes":
181             check_changes(file);
182         elif file[-5:] == ".joey":
183             check_joey(file);
184         else:
185             utils.fubar("Unrecognised file type: '%s'." % (file));
186
187 #######################################################################################
188
189 if __name__ == '__main__':
190     main()
191