]> git.decadent.org.uk Git - dak.git/blob - dak/generate_filelist.py
Merge commit 'twerner/msfl' into merge
[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     """
40     args = { 'suite': suite.suite_id,
41              'component': component.component_id }
42     return fetch(query, args, session)
43
44 def getBinaries(suite, component, architecture, type, session):
45     query = """
46         SELECT path, filename
47             FROM binfiles_suite_component_arch
48             WHERE suite = :suite AND component = :component AND type = :type AND
49                   (architecture = :architecture OR architecture = 2)
50     """
51     args = { 'suite': suite.suite_id,
52              'component': component.component_id,
53              'architecture': architecture.arch_id,
54              'type': type }
55     return fetch(query, args, session)
56
57 def listPath(suite, component, architecture = None, type = None):
58     """returns full path to the list file"""
59     suffixMap = { 'deb': "binary-",
60                   'udeb': "debian-installer_binary-" }
61     if architecture:
62         suffix = suffixMap[type] + architecture.arch_string
63     else:
64         suffix = "source"
65     filename = "%s_%s_%s.list" % \
66         (suite.suite_name, component.component_name, suffix)
67     pathname = os.path.join(Config()["Dir::Lists"], filename)
68     return utils.open_file(pathname, "w")
69
70 def writeSourceList(suite, component, session):
71     file = listPath(suite, component)
72     for filename in getSources(suite, component, session):
73         file.write(filename + '\n')
74     file.close()
75
76 def writeBinaryList(suite, component, architecture, type, session):
77     file = listPath(suite, component, architecture, type)
78     for filename in getBinaries(suite, component, architecture, type, session):
79         file.write(filename + '\n')
80     file.close()
81
82 def usage():
83     print """Usage: dak generate_filelist [OPTIONS]
84 Create filename lists for apt-ftparchive.
85
86   -s, --suite=SUITE                    act on this suite
87   -c, --component=COMPONENT    act on this component
88   -a, --architecture=ARCH        act on this architecture
89   -h, --help                                 show this help and exit
90
91 ARCH, COMPONENT and SUITE can be comma (or space) separated list, e.g.
92     --suite=testing,unstable"""
93     sys.exit()
94
95 def main():
96     cnf = Config()
97     Arguments = [('h', "help",         "Filelist::Options::Help"),
98                  ('s', "suite",        "Filelist::Options::Suite", "HasArg"),
99                  ('c', "component",    "Filelist::Options::Component", "HasArg"),
100                  ('a', "architecture", "Filelist::Options::Architecture", "HasArg")]
101     query_suites = DBConn().session().query(Suite)
102     suites = [suite.suite_name for suite in query_suites.all()]
103     if not cnf.has_key('Filelist::Options::Suite'):
104         cnf['Filelist::Options::Suite'] = ','.join(suites)
105     # we can ask the database for components if 'mixed' is gone
106     if not cnf.has_key('Filelist::Options::Component'):
107         cnf['Filelist::Options::Component'] = 'main,contrib,non-free'
108     query_architectures = DBConn().session().query(Architecture)
109     architectures = \
110         [architecture.arch_string for architecture in query_architectures.all()]
111     if not cnf.has_key('Filelist::Options::Architecture'):
112         cnf['Filelist::Options::Architecture'] = ','.join(architectures)
113     cnf['Filelist::Options::Help'] = ''
114     apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
115     Options = cnf.SubTree("Filelist::Options")
116     if Options['Help']:
117         usage()
118     session = DBConn().session()
119     suite_arch = session.query(SuiteArchitecture)
120     for suite_name in utils.split_args(Options['Suite']):
121         suite = query_suites.filter_by(suite_name = suite_name).one()
122         join = suite_arch.filter_by(suite_id = suite.suite_id)
123         for component_name in utils.split_args(Options['Component']):
124             component = session.query(Component).\
125                 filter_by(component_name = component_name).one()
126             for architecture_name in utils.split_args(Options['Architecture']):
127                 architecture = query_architectures.\
128                     filter_by(arch_string = architecture_name).one()
129                 try:
130                     join.filter_by(arch_id = architecture.arch_id).one()
131                     if architecture_name == 'source':
132                         writeSourceList(suite, component, session)
133                     elif architecture_name != 'all':
134                         writeBinaryList(suite, component, architecture, 'deb', session)
135                         writeBinaryList(suite, component, architecture, 'udeb', session)
136                 except:
137                     pass
138     # this script doesn't change the database
139     session.rollback()
140
141 if __name__ == '__main__':
142     main()
143