]> git.decadent.org.uk Git - dak.git/blob - dak/generate_filelist.py
generate-filelist: Use multiprocessing
[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 @copyright: 2011  Ansgar Burchardt <ansgar@debian.org>
9 @license: GNU General Public License version 2 or later
10 """
11
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26 ################################################################################
27
28 # Ganneff> Please go and try to lock mhy now. After than try to lock NEW.
29 # twerner> !lock mhy
30 # dak> twerner: You suck, this is already locked by Ganneff
31 # Ganneff> now try with NEW
32 # twerner> !lock NEW
33 # dak> twerner: also locked NEW
34 # mhy> Ganneff: oy, stop using me for locks and highlighting me you tall muppet
35 # Ganneff> hehe :)
36
37 ################################################################################
38
39 from daklib.dbconn import *
40 from daklib.config import Config
41 from daklib import utils, daklog
42 from multiprocessing import Pool
43 import apt_pkg, os, stat, sys
44
45 from daklib.lists import getSources, getBinaries, getArchAll
46
47 def listPath(suite, component, architecture = None, type = None,
48         incremental_mode = False):
49     """returns full path to the list file"""
50     suffixMap = { 'deb': "binary-",
51                   'udeb': "debian-installer_binary-" }
52     if architecture:
53         suffix = suffixMap[type] + architecture.arch_string
54     else:
55         suffix = "source"
56     filename = "%s_%s_%s.list" % \
57         (suite.suite_name, component.component_name, suffix)
58     pathname = os.path.join(Config()["Dir::Lists"], filename)
59     file = utils.open_file(pathname, "a")
60     timestamp = None
61     if incremental_mode:
62         timestamp = os.fstat(file.fileno())[stat.ST_MTIME]
63     else:
64         file.seek(0)
65         file.truncate()
66     return (file, timestamp)
67
68 def writeSourceList(suite_id, component_id, incremental_mode):
69     session = DBConn().session()
70     suite = Suite.get(suite_id, session)
71     component = Component.get(component_id, session)
72     (file, timestamp) = listPath(suite, component,
73             incremental_mode = incremental_mode)
74
75     for _, filename in getSources(suite, component, session, timestamp):
76         file.write(filename + '\n')
77     session.close()
78     file.close()
79     return "sources list for %s %s" % (suite.suite_name, component.component_name)
80
81 def writeAllList(suite_id, component_id, architecture_id, type, incremental_mode):
82     session = DBConn().session()
83     suite = Suite.get(suite_id, session)
84     component = Component.get(component_id, session)
85     architecture = Architecture.get(architecture_id, session)
86     (file, timestamp) = listPath(suite, component, architecture, type,
87             incremental_mode)
88
89     for _, filename in getArchAll(suite, component, architecture, type,
90             session, timestamp):
91         file.write(filename + '\n')
92     session.close()
93     file.close()
94     return "all list for %s %s (arch=%s, type=%s)" % (suite.suite_name, component.component_name, architecture.arch_string, type)
95
96 def writeBinaryList(suite_id, component_id, architecture_id, type, incremental_mode):
97     session = DBConn().session()
98     suite = Suite.get(suite_id, session)
99     component = Component.get(component_id, session)
100     architecture = Architecture.get(architecture_id, session)
101     (file, timestamp) = listPath(suite, component, architecture, type,
102             incremental_mode)
103
104     for _, filename in getBinaries(suite, component, architecture, type,
105             session, timestamp):
106         file.write(filename + '\n')
107     session.close()
108     file.close()
109     return "binary list for %s %s (arch=%s, type=%s)" % (suite.suite_name, component.component_name, architecture.arch_string, type)
110
111 def usage():
112     print """Usage: dak generate_filelist [OPTIONS]
113 Create filename lists for apt-ftparchive.
114
115   -s, --suite=SUITE            act on this suite
116   -c, --component=COMPONENT    act on this component
117   -a, --architecture=ARCH      act on this architecture
118   -h, --help                   show this help and exit
119   -i, --incremental            activate incremental mode
120
121 ARCH, COMPONENT and SUITE can be comma (or space) separated list, e.g.
122     --suite=testing,unstable
123
124 Incremental mode appends only newer files to existing lists."""
125     sys.exit()
126
127 def main():
128     cnf = Config()
129     Logger = daklog.Logger(cnf, 'generate-filelist')
130     Arguments = [('h', "help",         "Filelist::Options::Help"),
131                  ('s', "suite",        "Filelist::Options::Suite", "HasArg"),
132                  ('c', "component",    "Filelist::Options::Component", "HasArg"),
133                  ('a', "architecture", "Filelist::Options::Architecture", "HasArg"),
134                  ('i', "incremental",  "Filelist::Options::Incremental")]
135     session = DBConn().session()
136     query_suites = session.query(Suite)
137     suites = [suite.suite_name for suite in query_suites]
138     if not cnf.has_key('Filelist::Options::Suite'):
139         cnf['Filelist::Options::Suite'] = ','.join(suites).encode()
140     query_components = session.query(Component)
141     components = \
142         [component.component_name for component in query_components]
143     if not cnf.has_key('Filelist::Options::Component'):
144         cnf['Filelist::Options::Component'] = ','.join(components).encode()
145     query_architectures = session.query(Architecture)
146     architectures = \
147         [architecture.arch_string for architecture in query_architectures]
148     if not cnf.has_key('Filelist::Options::Architecture'):
149         cnf['Filelist::Options::Architecture'] = ','.join(architectures).encode()
150     cnf['Filelist::Options::Help'] = ''
151     cnf['Filelist::Options::Incremental'] = ''
152     apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
153     Options = cnf.SubTree("Filelist::Options")
154     if Options['Help']:
155         usage()
156     pool = Pool()
157     query_suites = query_suites. \
158         filter(Suite.suite_name.in_(utils.split_args(Options['Suite'])))
159     query_components = query_components. \
160         filter(Component.component_name.in_(utils.split_args(Options['Component'])))
161     query_architectures = query_architectures. \
162         filter(Architecture.arch_string.in_(utils.split_args(Options['Architecture'])))
163
164     def log(message):
165         Logger.log([message])
166
167     for suite in query_suites:
168         suite_id = suite.suite_id
169         for component in query_components:
170             component_id = component.component_id
171             for architecture in query_architectures:
172                 architecture_id = architecture.arch_id
173                 if architecture not in suite.architectures:
174                     pass
175                 elif architecture.arch_string == 'source':
176                     pool.apply_async(writeSourceList,
177                         (suite_id, component_id, Options['Incremental']), callback=log)
178                 elif architecture.arch_string == 'all':
179                     pool.apply_async(writeAllList,
180                         (suite_id, component_id, architecture_id, 'deb',
181                             Options['Incremental']), callback=log)
182                     pool.apply_async(writeAllList,
183                         (suite_id, component_id, architecture_id, 'udeb',
184                             Options['Incremental']), callback=log)
185                 else: # arch any
186                     pool.apply_async(writeBinaryList,
187                         (suite_id, component_id, architecture_id, 'deb',
188                             Options['Incremental']), callback=log)
189                     pool.apply_async(writeBinaryList,
190                         (suite_id, component_id, architecture_id, 'udeb',
191                             Options['Incremental']), callback=log)
192     pool.close()
193     pool.join()
194     # this script doesn't change the database
195     session.close()
196
197 if __name__ == '__main__':
198     main()
199