]> git.decadent.org.uk Git - dak.git/blob - dak/generate_filelist.py
50a0949a9040712f64bb1f5007cdc887aaf36149
[dak.git] / dak / generate_filelist.py
1 #!/usr/bin/python
2
3 """
4 Generate file lists for apt-ftparchive.
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 utils
28 import apt_pkg, os, sys
29
30 def fetch(query, args, session):
31     return [path + filename for (path, filename) in \
32         session.execute(query, args).fetchall()]
33
34 def getSources(suite, component, session):
35     query = """
36         SELECT path, filename
37             FROM srcfiles_suite_component
38             WHERE suite = :suite AND component = :component
39             ORDER BY filename
40     """
41     args = { 'suite': suite.suite_id,
42              'component': component.component_id }
43     return fetch(query, args, session)
44
45 def getBinaries(suite, component, architecture, type, session):
46     query = """
47 CREATE TEMP TABLE gf_candidates (
48     filename text,
49     path text,
50     architecture integer,
51     src integer,
52     source text);
53
54 INSERT INTO gf_candidates (filename, path, architecture, src, source)
55     SELECT f.filename, l.path, b.architecture, b.source as src, s.source
56         FROM binaries b
57         JOIN bin_associations ba ON b.id = ba.bin
58         JOIN source s ON b.source = s.id
59         JOIN files f ON b.file = f.id
60         JOIN location l ON f.location = l.id
61         WHERE ba.suite = :suite AND b.type = :type AND
62             l.component = :component AND b.architecture IN (2, :architecture);
63
64 WITH arch_any AS
65
66     (SELECT path, filename FROM gf_candidates
67         WHERE architecture > 2),
68
69      arch_all_with_any AS
70     (SELECT path, filename FROM gf_candidates
71         WHERE architecture = 2 AND
72               src IN (SELECT src FROM gf_candidates WHERE architecture > 2)),
73
74      arch_all_without_any AS
75     (SELECT path, filename FROM gf_candidates
76         WHERE architecture = 2 AND
77               source NOT IN (SELECT DISTINCT source FROM gf_candidates WHERE architecture > 2)),
78
79      filelist AS
80     (SELECT * FROM arch_any
81     UNION
82     SELECT * FROM arch_all_with_any
83     UNION
84     SELECT * FROM arch_all_without_any)
85
86     SELECT * FROM filelist ORDER BY filename
87     """
88     args = { 'suite': suite.suite_id,
89              'component': component.component_id,
90              'architecture': architecture.arch_id,
91              'type': type }
92     return fetch(query, args, session)
93
94 def listPath(suite, component, architecture = None, type = None):
95     """returns full path to the list file"""
96     suffixMap = { 'deb': "binary-",
97                   'udeb': "debian-installer_binary-" }
98     if architecture:
99         suffix = suffixMap[type] + architecture.arch_string
100     else:
101         suffix = "source"
102     filename = "%s_%s_%s.list" % \
103         (suite.suite_name, component.component_name, suffix)
104     pathname = os.path.join(Config()["Dir::Lists"], filename)
105     return utils.open_file(pathname, "w")
106
107 def writeSourceList(suite, component, session):
108     file = listPath(suite, component)
109     for filename in getSources(suite, component, session):
110         file.write(filename + '\n')
111     file.close()
112
113 def writeBinaryList(suite, component, architecture, type):
114     file = listPath(suite, component, architecture, type)
115     session = DBConn().session()
116     for filename in getBinaries(suite, component, architecture, type, session):
117         file.write(filename + '\n')
118     session.close()
119     file.close()
120
121 def usage():
122     print """Usage: dak generate_filelist [OPTIONS]
123 Create filename lists for apt-ftparchive.
124
125   -s, --suite=SUITE            act on this suite
126   -c, --component=COMPONENT    act on this component
127   -a, --architecture=ARCH      act on this architecture
128   -h, --help                   show this help and exit
129
130 ARCH, COMPONENT and SUITE can be comma (or space) separated list, e.g.
131     --suite=testing,unstable"""
132     sys.exit()
133
134 def main():
135     cnf = Config()
136     Arguments = [('h', "help",         "Filelist::Options::Help"),
137                  ('s', "suite",        "Filelist::Options::Suite", "HasArg"),
138                  ('c', "component",    "Filelist::Options::Component", "HasArg"),
139                  ('a', "architecture", "Filelist::Options::Architecture", "HasArg")]
140     query_suites = DBConn().session().query(Suite)
141     suites = [suite.suite_name for suite in query_suites.all()]
142     if not cnf.has_key('Filelist::Options::Suite'):
143         cnf['Filelist::Options::Suite'] = ','.join(suites)
144     # we can ask the database for components if 'mixed' is gone
145     if not cnf.has_key('Filelist::Options::Component'):
146         cnf['Filelist::Options::Component'] = 'main,contrib,non-free'
147     query_architectures = DBConn().session().query(Architecture)
148     architectures = \
149         [architecture.arch_string for architecture in query_architectures.all()]
150     if not cnf.has_key('Filelist::Options::Architecture'):
151         cnf['Filelist::Options::Architecture'] = ','.join(architectures)
152     cnf['Filelist::Options::Help'] = ''
153     apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
154     Options = cnf.SubTree("Filelist::Options")
155     if Options['Help']:
156         usage()
157     session = DBConn().session()
158     suite_arch = session.query(SuiteArchitecture)
159     for suite_name in utils.split_args(Options['Suite']):
160         suite = query_suites.filter_by(suite_name = suite_name).one()
161         join = suite_arch.filter_by(suite_id = suite.suite_id)
162         for component_name in utils.split_args(Options['Component']):
163             component = session.query(Component).\
164                 filter_by(component_name = component_name).one()
165             for architecture_name in utils.split_args(Options['Architecture']):
166                 architecture = query_architectures.\
167                     filter_by(arch_string = architecture_name).one()
168                 try:
169                     join.filter_by(arch_id = architecture.arch_id).one()
170                     if architecture_name == 'source':
171                         writeSourceList(suite, component, session)
172                     elif architecture_name != 'all':
173                         writeBinaryList(suite, component, architecture, 'deb')
174                         writeBinaryList(suite, component, architecture, 'udeb')
175                 except:
176                     pass
177     # this script doesn't change the database
178     session.close()
179
180 if __name__ == '__main__':
181     main()
182