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