]> git.decadent.org.uk Git - dak.git/blob - dak/generate_packages_sources2.py
Use overridesuite when joining override table.
[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   -5, --description-md5        Allow to use Description-md5 instead of
42                                Description for Packages index and generate
43                                Translation-en
44                                NOTE: suite.include_long_descriptions needs to
45                                be set to false for this.
46   -h, --help                   show this help and exit
47
48 SUITE can be a space seperated list, e.g.
49    --suite=unstable testing
50 """
51     sys.exit()
52
53 #############################################################################
54
55 # Here be dragons.
56 _sources_query = R"""
57 SELECT
58
59   (SELECT
60      STRING_AGG(
61        CASE
62          WHEN key = 'Source' THEN 'Package\: '
63          WHEN key = 'Files' THEN E'Files\:\n ' || f.md5sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
64          WHEN key = 'Checksums-Sha1' THEN E'Checksums-Sha1\:\n ' || f.sha1sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
65          WHEN key = 'Checksums-Sha256' THEN E'Checksums-Sha256\:\n ' || f.sha256sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
66          ELSE key || '\: '
67        END || value, E'\n' ORDER BY mk.ordering, mk.key)
68    FROM
69      source_metadata sm
70      JOIN metadata_keys mk ON mk.key_id = sm.key_id
71    WHERE s.id=sm.src_id
72   )
73   ||
74   E'\nDirectory\: pool/' || SUBSTRING(f.filename FROM E'\\A(.*)/[^/]*\\Z')
75   ||
76   E'\nPriority\: ' || pri.priority
77   ||
78   E'\nSection\: ' || sec.section
79
80 FROM
81
82 source s
83 JOIN src_associations sa ON s.id = sa.source
84 JOIN files f ON s.file=f.id
85 JOIN override o ON o.package = s.source
86 JOIN section sec ON o.section = sec.id
87 JOIN priority pri ON o.priority = pri.id
88
89 WHERE
90   sa.suite = :suite
91   AND o.suite = :overridesuite AND o.component = :component AND o.type = :dsc_type
92
93 ORDER BY
94 s.source, s.version
95 """
96
97 def generate_sources(suite_id, component_id):
98     global _sources_query
99     from daklib.filewriter import SourcesFileWriter
100     from daklib.dbconn import Component, DBConn, OverrideType, Suite
101     from daklib.dakmultiprocessing import PROC_STATUS_SUCCESS
102
103     session = DBConn().session()
104     dsc_type = session.query(OverrideType).filter_by(overridetype='dsc').one().overridetype_id
105
106     suite = session.query(Suite).get(suite_id)
107     component = session.query(Component).get(component_id)
108
109     overridesuite_id = suite.get_overridesuite().suite_id
110
111     writer = SourcesFileWriter(suite=suite.suite_name, component=component.component_name)
112     output = writer.open()
113
114     # run query and write Sources
115     r = session.execute(_sources_query, {"suite": suite_id, "component": component_id, "dsc_type": dsc_type, "overridesuite": overridesuite_id})
116     for (stanza,) in r:
117         print >>output, stanza
118         print >>output, ""
119
120     writer.close()
121
122     message = ["generate sources", suite.suite_name, component.component_name]
123     session.rollback()
124     return (PROC_STATUS_SUCCESS, message)
125
126 #############################################################################
127
128 # Here be large dragons.
129 _packages_query = R"""
130 WITH
131
132   tmp AS (
133     SELECT
134       b.id AS binary_id,
135       b.package AS package,
136       b.version AS version,
137       b.architecture AS architecture,
138       b.source AS source_id,
139       s.source AS source,
140       f.filename AS filename,
141       f.size AS size,
142       f.md5sum AS md5sum,
143       f.sha1sum AS sha1sum,
144       f.sha256sum AS sha256sum
145     FROM
146       binaries b
147       JOIN bin_associations ba ON b.id = ba.bin
148       JOIN files f ON f.id = b.file
149       JOIN location l ON l.id = f.location
150       JOIN source s ON b.source = s.id
151     WHERE
152       (b.architecture = :arch_all OR b.architecture = :arch) AND b.type = :type_name
153       AND ba.suite = :suite
154       AND l.component = :component
155   )
156
157 SELECT
158   (SELECT
159      STRING_AGG(key || '\: ' || value, E'\n' ORDER BY mk.ordering, mk.key)
160    FROM
161      binaries_metadata bm
162      JOIN metadata_keys mk ON mk.key_id = bm.key_id
163    WHERE
164      bm.bin_id = tmp.binary_id
165      AND key != ALL (:metadata_skip)
166   )
167   || COALESCE(E'\n' || (SELECT
168      STRING_AGG(key || '\: ' || value, E'\n' ORDER BY key)
169    FROM external_overrides eo
170    WHERE
171      eo.package = tmp.package
172      AND eo.suite = :overridesuite AND eo.component = :component
173   ), '')
174   || E'\nSection\: ' || sec.section
175   || E'\nPriority\: ' || pri.priority
176   || E'\nFilename\: pool/' || tmp.filename
177   || E'\nSize\: ' || tmp.size
178   || E'\nMD5sum\: ' || tmp.md5sum
179   || E'\nSHA1\: ' || tmp.sha1sum
180   || E'\nSHA256\: ' || tmp.sha256sum
181
182 FROM
183   tmp
184   JOIN override o ON o.package = tmp.package
185   JOIN section sec ON sec.id = o.section
186   JOIN priority pri ON pri.id = o.priority
187
188 WHERE
189   (
190       architecture <> :arch_all
191     OR
192       (architecture = :arch_all AND source_id IN (SELECT source_id FROM tmp WHERE architecture <> :arch_all))
193     OR
194       (architecture = :arch_all AND source NOT IN (SELECT DISTINCT source FROM tmp WHERE architecture <> :arch_all))
195   )
196   AND
197     o.type = :type_id AND o.suite = :overridesuite AND o.component = :component
198
199 ORDER BY tmp.source, tmp.package, tmp.version
200 """
201
202 def generate_packages(suite_id, component_id, architecture_id, type_name, use_description_md5):
203     global _packages_query
204     from daklib.filewriter import PackagesFileWriter
205     from daklib.dbconn import Architecture, Component, DBConn, OverrideType, Suite
206     from daklib.dakmultiprocessing import PROC_STATUS_SUCCESS
207
208     session = DBConn().session()
209     arch_all_id = session.query(Architecture).filter_by(arch_string='all').one().arch_id
210     type_id = session.query(OverrideType).filter_by(overridetype=type_name).one().overridetype_id
211
212     suite = session.query(Suite).get(suite_id)
213     component = session.query(Component).get(component_id)
214     architecture = session.query(Architecture).get(architecture_id)
215
216     overridesuite_id = suite.get_overridesuite().suite_id
217
218     # We currently filter out the "Tag" line. They are set by external
219     # overrides and NOT by the maintainer. And actually having it set by
220     # maintainer means we output it twice at the moment -> which breaks
221     # dselect.
222     metadata_skip = ["Section", "Priority", "Tag"]
223     if suite.include_long_description or not use_description_md5:
224         metadata_skip.append("Description-md5")
225     else:
226         metadata_skip.append("Description")
227
228     writer = PackagesFileWriter(suite=suite.suite_name, component=component.component_name,
229             architecture=architecture.arch_string, debtype=type_name)
230     output = writer.open()
231
232     r = session.execute(_packages_query, {"suite": suite_id, "component": component_id,
233         "arch": architecture_id, "type_id": type_id, "type_name": type_name, "arch_all": arch_all_id,
234         "overridesuite": overridesuite_id, "metadata_skip": metadata_skip})
235     for (stanza,) in r:
236         print >>output, stanza
237         print >>output, ""
238
239     writer.close()
240
241     message = ["generate-packages", suite.suite_name, component.component_name, architecture.arch_string]
242     session.rollback()
243     return (PROC_STATUS_SUCCESS, message)
244
245 #############################################################################
246
247 _translations_query = """
248 WITH
249   override_suite AS
250     (SELECT
251       s.id AS id,
252       COALESCE(os.id, s.id) AS overridesuite_id
253       FROM suite AS s LEFT JOIN suite AS os ON s.overridesuite = os.suite_name)
254
255 SELECT
256      'Package\: ' || b.package
257   || E'\nDescription-md5\: ' || bm_description_md5.value
258   || E'\nDescription-en\: ' || bm_description.value
259   || E'\n'
260 FROM binaries b
261   -- join tables for suite and component
262   JOIN bin_associations ba ON b.id = ba.bin
263   JOIN override_suite os ON os.id = ba.suite
264   JOIN override o ON b.package = o.package AND o.suite = os.overridesuite_id AND o.type = (SELECT id FROM override_type WHERE type = 'deb')
265
266   -- join tables for Description and Description-md5
267   JOIN binaries_metadata bm_description ON b.id = bm_description.bin_id AND bm_description.key_id = (SELECT key_id FROM metadata_keys WHERE key = 'Description')
268   JOIN binaries_metadata bm_description_md5 ON b.id = bm_description_md5.bin_id AND bm_description_md5.key_id = (SELECT key_id FROM metadata_keys WHERE key = 'Description-md5')
269
270   -- we want to sort by source name
271   JOIN source s ON b.source = s.id
272
273 WHERE ba.suite = :suite AND o.component = :component
274 GROUP BY s.source, b.package, bm_description_md5.value, bm_description.value
275 ORDER BY s.source, b.package, bm_description_md5.value
276 """
277
278 def generate_translations(suite_id, component_id):
279     global _translations_query
280     from daklib.filewriter import TranslationFileWriter
281     from daklib.dbconn import DBConn, Suite, Component
282     from daklib.dakmultiprocessing import PROC_STATUS_SUCCESS
283
284     session = DBConn().session()
285     suite = session.query(Suite).get(suite_id)
286     component = session.query(Component).get(component_id)
287
288     writer = TranslationFileWriter(suite=suite.suite_name, component=component.component_name, language="en")
289     output = writer.open()
290
291     r = session.execute(_translations_query, {"suite": suite_id, "component": component_id})
292     for (stanza,) in r:
293         print >>output, stanza
294
295     writer.close()
296
297     message = ["generate-translations", suite.suite_name, component.component_name]
298     session.rollback()
299     return (PROC_STATUS_SUCCESS, message)
300
301 #############################################################################
302
303 def main():
304     from daklib.config import Config
305     from daklib import daklog
306
307     cnf = Config()
308
309     Arguments = [('h',"help","Generate-Packages-Sources::Options::Help"),
310                  ('5','description-md5',"Generate-Packages-Sources::Options::Description-md5"),
311                  ('s',"suite","Generate-Packages-Sources::Options::Suite"),
312                  ('f',"force","Generate-Packages-Sources::Options::Force"),
313                  ('o','option','','ArbItem')]
314
315     suite_names = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
316     try:
317         Options = cnf.SubTree("Generate-Packages-Sources::Options")
318     except KeyError:
319         Options = {}
320
321     if Options.has_key("Help"):
322         usage()
323
324     from daklib.dakmultiprocessing import DakProcessPool, PROC_STATUS_SUCCESS, PROC_STATUS_SIGNALRAISED
325     pool = DakProcessPool()
326
327     logger = daklog.Logger('generate-packages-sources2')
328
329     from daklib.dbconn import Component, DBConn, get_suite, Suite
330     session = DBConn().session()
331     session.execute("SELECT add_missing_description_md5()")
332     session.commit()
333
334     if Options.has_key("Suite"):
335         suites = []
336         for s in suite_names:
337             suite = get_suite(s.lower(), session)
338             if suite:
339                 suites.append(suite)
340             else:
341                 print "I: Cannot find suite %s" % s
342                 logger.log(['Cannot find suite %s' % s])
343     else:
344         suites = session.query(Suite).filter(Suite.untouchable == False).all()
345
346     use_description_md5 = Options.has_key("Description-md5") and Options["Description-md5"]
347     force = Options.has_key("Force") and Options["Force"]
348
349     component_ids = [ c.component_id for c in session.query(Component).all() ]
350
351     def parse_results(message):
352         # Split out into (code, msg)
353         code, msg = message
354         if code == PROC_STATUS_SUCCESS:
355             logger.log([msg])
356         elif code == PROC_STATUS_SIGNALRAISED:
357             logger.log(['E: Subprocess recieved signal ', msg])
358         else:
359             logger.log(['E: ', msg])
360
361     for s in suites:
362         if s.untouchable and not force:
363             utils.fubar("Refusing to touch %s (untouchable and not forced)" % s.suite_name)
364         for c in component_ids:
365             pool.apply_async(generate_sources, [s.suite_id, c], callback=parse_results)
366             if use_description_md5 and not s.include_long_description:
367                 pool.apply_async(generate_translations, [s.suite_id, c], callback=parse_results)
368             for a in s.architectures:
369                 if a == 'source':
370                     continue
371                 pool.apply_async(generate_packages, [s.suite_id, c, a.arch_id, 'deb', use_description_md5], callback=parse_results)
372                 pool.apply_async(generate_packages, [s.suite_id, c, a.arch_id, 'udeb', use_description_md5], callback=parse_results)
373
374     pool.close()
375     pool.join()
376
377     # this script doesn't change the database
378     session.close()
379
380     logger.close()
381
382     sys.exit(pool.overall_status())
383
384 if __name__ == '__main__':
385     main()