]> git.decadent.org.uk Git - dak.git/blob - dak/generate_packages_sources2.py
Merge remote branch 'ansgar/p-s-from-db' into merge
[dak.git] / dak / generate_packages_sources2.py
1 #!/usr/bin/python
2
3 """
4 Generate Packages/Sources files
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2011  Ansgar Burchardt <ansgar@debian.org>
8 @copyright: Based on daklib/lists.py and dak/generate_filelist.py:
9             2009-2011  Torsten Werner <twerner@debian.org>
10 @copyright: Based on dak/generate_packages_sources.py:
11             2000, 2001, 2002, 2006  James Troup <james@nocrew.org>
12             2009  Mark Hymers <mhy@debian.org>
13             2010  Joerg Jaspert <joerg@debian.org>
14 @license: GNU General Public License version 2 or later
15 """
16
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write to the Free Software
29 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
31 import apt_pkg, sys
32
33 def usage():
34     print """Usage: dak generate-packages-sources2 [OPTIONS]
35 Generate the Packages/Sources files
36
37   -s, --suite=SUITE            process this suite
38                                Default: All suites not marked 'untouchable'
39   -f, --force                  Allow processing of untouchable suites
40                                CAREFUL: Only to be used at point release time!
41   -h, --help                   show this help and exit
42
43 SUITE can be a space seperated list, e.g.
44    --suite=unstable testing
45 """
46     sys.exit()
47
48 #############################################################################
49
50 # Here be dragons.
51 _sources_query = R"""
52 SELECT
53
54   (SELECT
55      STRING_AGG(
56        CASE
57          WHEN key = 'Source' THEN 'Package\: '
58          WHEN key = 'Files' THEN E'Files\:\n ' || f.md5sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
59          WHEN key = 'Checksums-Sha1' THEN E'Checksums-Sha1\:\n ' || f.sha1sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
60          WHEN key = 'Checksums-Sha256' THEN E'Checksums-Sha256\:\n ' || f.sha256sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
61          ELSE key || '\: '
62        END || value, E'\n' ORDER BY mk.ordering, mk.key)
63    FROM
64      source_metadata sm
65      JOIN metadata_keys mk ON mk.key_id = sm.key_id
66    WHERE s.id=sm.src_id
67   )
68   ||
69   E'\nDirectory\: pool/' || SUBSTRING(f.filename FROM E'\\A(.*)/[^/]*\\Z')
70   ||
71   E'\nPriority\: ' || pri.priority
72   ||
73   E'\nSection\: ' || sec.section
74
75 FROM
76
77 source s
78 JOIN src_associations sa ON s.id = sa.source
79 JOIN files f ON s.file=f.id
80 JOIN override o ON o.package = s.source
81 JOIN section sec ON o.section = sec.id
82 JOIN priority pri ON o.priority = pri.id
83
84 WHERE
85   sa.suite = :suite
86   AND o.suite = :overridesuite AND o.component = :component AND o.type = :dsc_type
87
88 ORDER BY
89 s.source, s.version
90 """
91
92 def generate_sources(suite_id, component_id):
93     global _sources_query
94     from daklib.filewriter import SourcesFileWriter
95     from daklib.dbconn import Component, DBConn, OverrideType, Suite
96
97     session = DBConn().session()
98     dsc_type = session.query(OverrideType).filter_by(overridetype='dsc').one().overridetype_id
99
100     suite = session.query(Suite).get(suite_id)
101     component = session.query(Component).get(component_id)
102
103     overridesuite_id = suite.get_overridesuite().suite_id
104
105     writer = SourcesFileWriter(suite=suite.suite_name, component=component.component_name)
106     output = writer.open()
107
108     # run query and write Sources
109     r = session.execute(_sources_query, {"suite": suite_id, "component": component_id, "dsc_type": dsc_type, "overridesuite": overridesuite_id})
110     for (stanza,) in r:
111         print >>output, stanza
112         print >>output, ""
113
114     writer.close()
115
116     message = ["generate sources", suite.suite_name, component.component_name]
117     session.rollback()
118     return (PROC_STATUS_SUCCESS, message)
119
120 #############################################################################
121
122 # We currently filter out the "Tag" line. They are set by external overrides and
123 # NOT by the maintainer. And actually having it set by maintainer means we output
124 # it twice at the moment -> which breaks dselect.
125 # Here be large dragons.
126 _packages_query = R"""
127 WITH
128
129   tmp AS (
130     SELECT
131       b.id AS binary_id,
132       b.package AS package,
133       b.version AS version,
134       b.architecture AS architecture,
135       b.source AS source_id,
136       s.source AS source,
137       f.filename AS filename,
138       f.size AS size,
139       f.md5sum AS md5sum,
140       f.sha1sum AS sha1sum,
141       f.sha256sum AS sha256sum
142     FROM
143       binaries b
144       JOIN bin_associations ba ON b.id = ba.bin
145       JOIN files f ON f.id = b.file
146       JOIN location l ON l.id = f.location
147       JOIN source s ON b.source = s.id
148     WHERE
149       (b.architecture = :arch_all OR b.architecture = :arch) AND b.type = :type_name
150       AND ba.suite = :suite
151       AND l.component = :component
152   )
153
154 SELECT
155   (SELECT
156      STRING_AGG(key || '\: ' || value, E'\n' ORDER BY mk.ordering, mk.key)
157    FROM
158      binaries_metadata bm
159      JOIN metadata_keys mk ON mk.key_id = bm.key_id
160    WHERE
161      bm.bin_id = tmp.binary_id
162      AND key != 'Section' AND key != 'Priority' AND key != 'Tag'
163   )
164   || COALESCE(E'\n' || (SELECT
165      STRING_AGG(key || '\: ' || value, E'\n' ORDER BY key)
166    FROM external_overrides eo
167    WHERE
168      eo.package = tmp.package
169      AND eo.suite = :overridesuite AND eo.component = :component
170   ), '')
171   || E'\nSection\: ' || sec.section
172   || E'\nPriority\: ' || pri.priority
173   || E'\nFilename\: pool/' || tmp.filename
174   || E'\nSize\: ' || tmp.size
175   || E'\nMD5sum\: ' || tmp.md5sum
176   || E'\nSHA1\: ' || tmp.sha1sum
177   || E'\nSHA256\: ' || tmp.sha256sum
178
179 FROM
180   tmp
181   JOIN override o ON o.package = tmp.package
182   JOIN section sec ON sec.id = o.section
183   JOIN priority pri ON pri.id = o.priority
184
185 WHERE
186   (
187       architecture <> :arch_all
188     OR
189       (architecture = :arch_all AND source_id IN (SELECT source_id FROM tmp WHERE architecture <> :arch_all))
190     OR
191       (architecture = :arch_all AND source NOT IN (SELECT DISTINCT source FROM tmp WHERE architecture <> :arch_all))
192   )
193   AND
194     o.type = :type_id AND o.suite = :overridesuite AND o.component = :component
195
196 ORDER BY tmp.source, tmp.package, tmp.version
197 """
198
199 def generate_packages(suite_id, component_id, architecture_id, type_name):
200     global _packages_query
201     from daklib.filewriter import PackagesFileWriter
202     from daklib.dbconn import Architecture, Component, DBConn, OverrideType, Suite
203
204     session = DBConn().session()
205     arch_all_id = session.query(Architecture).filter_by(arch_string='all').one().arch_id
206     type_id = session.query(OverrideType).filter_by(overridetype=type_name).one().overridetype_id
207
208     suite = session.query(Suite).get(suite_id)
209     component = session.query(Component).get(component_id)
210     architecture = session.query(Architecture).get(architecture_id)
211
212     overridesuite_id = suite.get_overridesuite().suite_id
213
214     writer = PackagesFileWriter(suite=suite.suite_name, component=component.component_name,
215             architecture=architecture.arch_string, debtype=type_name)
216     output = writer.open()
217
218     r = session.execute(_packages_query, {"suite": suite_id, "component": component_id,
219         "arch": architecture_id, "type_id": type_id, "type_name": type_name, "arch_all": arch_all_id,
220         "overridesuite": overridesuite_id})
221     for (stanza,) in r:
222         print >>output, stanza
223         print >>output, ""
224
225     writer.close()
226
227     message = ["generate-packages", suite.suite_name, component.component_name, architecture.arch_string]
228     session.rollback()
229     return (PROC_STATUS_SUCCESS, message)
230
231 #############################################################################
232
233 def main():
234     from daklib.dakmultiprocessing import DakProcessPool, PROC_STATUS_SUCCESS, PROC_STATUS_SIGNALRAISED
235     pool = DakProcessPool()
236
237     from daklib.dbconn import Component, DBConn, get_suite
238     from daklib.config import Config
239     from daklib import daklog
240
241     cnf = Config()
242
243     Arguments = [('h',"help","Generate-Packages-Sources::Options::Help"),
244                  ('s',"suite","Generate-Packages-Sources::Options::Suite"),
245                  ('f',"force","Generate-Packages-Sources::Options::Force")]
246
247     suite_names = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
248     try:
249         Options = cnf.SubTree("Generate-Packages-Sources::Options")
250     except KeyError:
251         Options = {}
252
253     if Options.has_key("Help"):
254         usage()
255
256     logger = daklog.Logger(cnf, 'generate-packages-sources2')
257
258     session = DBConn().session()
259
260     if Options.has_key("Suite"):
261         suites = []
262         for s in suite_names:
263             suite = get_suite(s.lower(), session)
264             if suite:
265                 suites.append(suite)
266             else:
267                 print "I: Cannot find suite %s" % s
268                 logger.log(['Cannot find suite %s' % s])
269     else:
270         suites = session.query(Suite).filter(Suite.untouchable == False).all()
271
272     force = Options.has_key("Force") and Options["Force"]
273
274     component_ids = [ c.component_id for c in session.query(Component).all() ]
275
276     def parse_results(message):
277         # Split out into (code, msg)
278         code, msg = message
279         if code == PROC_STATUS_SUCCESS:
280             logger.log([msg])
281         elif code == PROC_STATUS_SIGNALRAISED:
282             logger.log(['E: Subprocess recieved signal ', msg])
283         else:
284             logger.log(['E: ', msg])
285
286     for s in suites:
287         if s.untouchable and not force:
288             utils.fubar("Refusing to touch %s (untouchable and not forced)" % s.suite_name)
289         for c in component_ids:
290             pool.apply_async(generate_sources, [s.suite_id, c], callback=parse_results)
291             for a in s.architectures:
292                 if a == 'source':
293                     continue
294                 pool.apply_async(generate_packages, [s.suite_id, c, a.arch_id, 'deb'], callback=parse_results)
295                 pool.apply_async(generate_packages, [s.suite_id, c, a.arch_id, 'udeb'], callback=parse_results)
296
297     pool.close()
298     pool.join()
299
300     # this script doesn't change the database
301     session.close()
302
303     logger.close()
304
305     sys.exit(pool.overall_status())
306
307 if __name__ == '__main__':
308     main()