]> git.decadent.org.uk Git - dak.git/blob - dak/clean_proposed_updates.py
Cleanup
[dak.git] / dak / clean_proposed_updates.py
1 #!/usr/bin/env python
2
3 # Remove obsolete .changes files from proposed-updates
4 # Copyright (C) 2001, 2002, 2003, 2004, 2006, 2008  James Troup <james@nocrew.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21
22 import os, pg, re, sys
23 import apt_pkg
24 import daklib.database as database
25 import daklib.utils as utils
26
27 ################################################################################
28
29 Cnf = None
30 projectB = None
31 Options = None
32 pu = {}
33
34 re_isdeb = re.compile (r"^(.+)_(.+?)_(.+?).u?deb$")
35
36 ################################################################################
37
38 def usage (exit_code=0):
39     print """Usage: dak clean-proposed-updates [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 f in files.keys():
59         if utils.re_isadeb.match(f):
60             m = re_isdeb.match(f)
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" % (f, pkg, version, arch)
66         else:
67             m = utils.re_issource.match(f)
68             if m:
69                 pkg = m.group(1)
70                 version = m.group(2)
71                 ftype = m.group(3)
72                 if ftype != "dsc":
73                     del files[f]
74                     num_files -= 1
75                     continue
76                 arch = "source"
77                 if Options["debug"]:
78                     print "SOURCE: %s ==> %s_%s_%s" % (f, pkg, version, arch)
79             else:
80                 utils.fubar("unknown type, fix me")
81         if not pu.has_key(pkg):
82             # FIXME
83             utils.warn("%s doesn't seem to exist in %s?? (from %s [%s])" % (pkg, Options["suite"], f, filename))
84             continue
85         if not pu[pkg].has_key(arch):
86             # FIXME
87             utils.warn("%s doesn't seem to exist for %s in %s?? (from %s [%s])" % (pkg, arch, Options["suite"], f, filename))
88             continue
89         pu_version = utils.re_no_epoch.sub('', pu[pkg][arch])
90         if pu_version == version:
91             if Options["verbose"]:
92                 print "%s: ok" % (f)
93         else:
94             if Options["verbose"]:
95                 print "%s: superseded, removing. [%s]" % (f, pu_version)
96             del files[f]
97
98     new_num_files = len(files.keys())
99     if new_num_files == 0:
100         print "%s: no files left, superseded by %s" % (filename, pu_version)
101         dest = Cnf["Dir::Morgue"] + "/misc/"
102         if not Options["no-action"]:
103             utils.move(filename, dest)
104     elif new_num_files < num_files:
105         print "%s: lost files, MWAAP." % (filename)
106     else:
107         if Options["verbose"]:
108             print "%s: ok" % (filename)
109
110 ################################################################################
111
112 def check_joey (filename):
113     f = utils.open_file(filename)
114
115     cwd = os.getcwd()
116     os.chdir("%s/dists/%s" % (Cnf["Dir::Root"]), Options["suite"])
117
118     for line in f.readlines():
119         line = line.rstrip()
120         if line.find('install') != -1:
121             split_line = line.split()
122             if len(split_line) != 2:
123                 utils.fubar("Parse error (not exactly 2 elements): %s" % (line))
124             install_type = split_line[0]
125             if install_type not in [ "install", "install-u", "sync-install" ]:
126                 utils.fubar("Unknown install type ('%s') from: %s" % (install_type, line))
127             changes_filename = split_line[1]
128             if Options["debug"]:
129                 print "Processing %s..." % (changes_filename)
130             check_changes(changes_filename)
131
132     os.chdir(cwd)
133
134 ################################################################################
135
136 def init_pu ():
137     global pu
138
139     q = projectB.query("""
140 SELECT b.package, b.version, a.arch_string
141   FROM bin_associations ba, binaries b, suite su, architecture a
142   WHERE b.id = ba.bin AND ba.suite = su.id
143     AND su.suite_name = '%s' AND a.id = b.architecture
144 UNION SELECT s.source, s.version, 'source'
145   FROM src_associations sa, source s, suite su
146   WHERE s.id = sa.source AND sa.suite = su.id
147     AND su.suite_name = '%s'
148 ORDER BY package, version, arch_string
149 """ % (Options["suite"], Options["suite"]))
150     ql = q.getresult()
151     for i in ql:
152         pkg = i[0]
153         version = i[1]
154         arch = i[2]
155         if not pu.has_key(pkg):
156             pu[pkg] = {}
157         pu[pkg][arch] = version
158
159 def main ():
160     global Cnf, projectB, Options
161
162     Cnf = utils.get_conf()
163
164     Arguments = [('d', "debug", "Clean-Proposed-Updates::Options::Debug"),
165                  ('v', "verbose", "Clean-Proposed-Updates::Options::Verbose"),
166                  ('h', "help", "Clean-Proposed-Updates::Options::Help"),
167                  ('s', "suite", "Clean-Proposed-Updates::Options::Suite", "HasArg"),
168                  ('n', "no-action", "Clean-Proposed-Updates::Options::No-Action"),]
169     for i in [ "debug", "verbose", "help", "no-action" ]:
170         if not Cnf.has_key("Clean-Proposed-Updates::Options::%s" % (i)):
171             Cnf["Clean-Proposed-Updates::Options::%s" % (i)] = ""
172
173     # suite defaults to proposed-updates
174     if not Cnf.has_key("Clean-Proposed-Updates::Options::Suite"):
175         Cnf["Clean-Proposed-Updates::Options::Suite"] = "proposed-updates"
176
177     arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
178     Options = Cnf.SubTree("Clean-Proposed-Updates::Options")
179
180     if Options["Help"]:
181         usage(0)
182     if not arguments:
183         utils.fubar("need at least one package name as an argument.")
184
185     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
186     database.init(Cnf, projectB)
187
188     init_pu()
189
190     for f in arguments:
191         if f.endswith(".changes"):
192             check_changes(f)
193         elif f.endswith(".joey"):
194             check_joey(f)
195         else:
196             utils.fubar("Unrecognised file type: '%s'." % (f))
197
198 #######################################################################################
199
200 if __name__ == '__main__':
201     main()