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