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