]> git.decadent.org.uk Git - dak.git/blob - dak/dominate.py
add dominate.py and db upgrade script #25
[dak.git] / dak / dominate.py
1 #!/usr/bin/python
2
3 """
4 Remove obsolete source and binary associations from suites.
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2009  Torsten Werner <twerner@debian.org>
8 @license: GNU General Public License version 2 or later
9 """
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25 from daklib.dbconn import *
26 from daklib.config import Config
27 from daklib import daklog, utils
28 import apt_pkg, sys
29
30 Options = None
31 Logger = None
32
33 def fetch(reason, query, args, session):
34     idList = []
35     for row in session.execute(query, args).fetchall():
36         (id, package, version, suite_name, architecture) = row
37         if Options['No-Action']:
38             print "Delete %s %s from %s architecture %s (%s)" % \
39                 (package, version, suite_name, architecture, reason)
40         else:
41             Logger.log([reason, package, version, suite_name, architecture])
42         idList.append(id)
43     return idList
44
45 def obsoleteAnyByAllAssociations(suite, session):
46     query = """
47         SELECT obsolete.id, package, obsolete.version, suite_name, arch_string
48             FROM obsolete_any_by_all_associations AS obsolete
49             JOIN architecture ON obsolete.architecture = architecture.id
50             JOIN suite ON obsolete.suite = suite.id
51             WHERE suite = :suite
52     """
53     return fetch('newer_all', query, { 'suite': suite }, session)
54
55 def obsoleteAnyAssociations(suite, session):
56     query = """
57         SELECT obsolete.id, package, obsolete.version, suite_name, arch_string
58             FROM obsolete_any_associations AS obsolete
59             JOIN architecture ON obsolete.architecture = architecture.id
60             JOIN suite ON obsolete.suite = suite.id
61             WHERE suite = :suite
62     """
63     return fetch('newer_any', query, { 'suite': suite }, session)
64
65 def obsoleteSrcAssociations(suite, session):
66     query = """
67         SELECT obsolete.id, source, obsolete.version, suite_name,
68             'source' AS arch_string
69             FROM obsolete_src_associations AS obsolete
70             JOIN suite ON obsolete.suite = suite.id
71             WHERE suite = :suite
72     """
73     return fetch('old_and_unreferenced', query, { 'suite': suite }, session)
74
75 def obsoleteAllAssociations(suite, session):
76     query = """
77         SELECT obsolete.id, package, obsolete.version, suite_name,
78             'all' AS arch_string
79             FROM obsolete_all_associations AS obsolete
80             JOIN suite ON obsolete.suite = suite.id
81             WHERE suite = :suite
82     """
83     return fetch('old_and_unreferenced', query, { 'suite': suite }, session)
84
85 def deleteAssociations(table, idList, session):
86     query = """
87         DELETE
88             FROM %s
89             WHERE id = :id
90     """ % table
91     session.execute(query, [{'id': id} for id in idList])
92
93 def doDaDoDa(suite, session):
94     # keep this part disabled because it is too dangerous
95     #idList = obsoleteAnyByAllAssociations(suite, session)
96     #deleteAssociations('bin_associations', idList, session)
97
98     idList = obsoleteAnyAssociations(suite, session)
99     deleteAssociations('bin_associations', idList, session)
100
101     idList = obsoleteSrcAssociations(suite, session)
102     deleteAssociations('src_associations', idList, session)
103
104     idList = obsoleteAllAssociations(suite, session)
105     deleteAssociations('bin_associations', idList, session)
106
107 def usage():
108     print """Usage: dak dominate [OPTIONS]
109 Remove obsolete source and binary associations from suites.
110
111     -s, --suite=SUITE          act on this suite
112     -h, --help                 show this help and exit
113     -n, --no-action            don't commit changes
114     -f, --force                also clean up untouchable suites
115
116 SUITE can be comma (or space) separated list, e.g.
117     --suite=testing,unstable"""
118     sys.exit()
119
120 def main():
121     global Options, Logger
122     cnf = Config()
123     Arguments = [('h', "help",      "Obsolete::Options::Help"),
124                  ('s', "suite",     "Obsolete::Options::Suite", "HasArg"),
125                  ('n', "no-action", "Obsolete::Options::No-Action"),
126                  ('f', "force",     "Obsolete::Options::Force")]
127     query_suites = DBConn().session().query(Suite)
128     suites = [suite.suite_name for suite in query_suites.all()]
129     if not cnf.has_key('Obsolete::Options::Suite'):
130         cnf['Obsolete::Options::Suite'] = ','.join(suites)
131     cnf['Obsolete::Options::Help'] = ''
132     cnf['Obsolete::Options::No-Action'] = ''
133     cnf['Obsolete::Options::Force'] = ''
134     apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
135     Options = cnf.SubTree("Obsolete::Options")
136     if Options['Help']:
137         usage()
138     Logger = daklog.Logger(cnf.Cnf, "dominate")
139     session = DBConn().session()
140     for suite_name in utils.split_args(Options['Suite']):
141         suite = session.query(Suite).filter_by(suite_name = suite_name).one()
142         if not suite.untouchable or Options['Force']:
143             doDaDoDa(suite.suite_id, session)
144     if Options['No-Action']:
145         session.rollback()
146     else:
147         session.commit()
148
149 if __name__ == '__main__':
150     main()
151