1#! /usr/bin/env python3
3"""
4Generate Packages/Sources files
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"""
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.
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.
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
31import sys
32from typing import NoReturn
34import apt_pkg
37def usage() -> NoReturn:
38 print(
39 """Usage: dak generate-packages-sources2 [OPTIONS]
40Generate the Packages/Sources files
42 -a, --archive=ARCHIVE process suites in ARCHIVE
43 -s, --suite=SUITE process this suite
44 Default: All suites not marked 'untouchable'
45 -f, --force Allow processing of untouchable suites
46 CAREFUL: Only to be used at point release time!
47 -h, --help show this help and exit
49SUITE can be a space separated list, e.g.
50 --suite=unstable testing
51"""
52 )
53 sys.exit()
56#############################################################################
59# Here be dragons.
60_sources_query = R"""
61SELECT
63 (SELECT
64 STRING_AGG(
65 CASE
66 WHEN key = 'Source' THEN E'Package\: '
67 WHEN key = 'Files' AND suite.checksums && array['md5sum'] THEN E'Files\:\n ' || f.md5sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
68 WHEN key = 'Files' THEN NULL
69 WHEN key = 'Checksums-Sha1' AND suite.checksums && array['sha1'] THEN E'Checksums-Sha1\:\n ' || f.sha1sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
70 WHEN key = 'Checksums-Sha1' THEN NULL
71 WHEN key = 'Checksums-Sha256' AND suite.checksums && array['sha256'] THEN E'Checksums-Sha256\:\n ' || f.sha256sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
72 WHEN key = 'Checksums-Sha256' THEN NULL
73 ELSE key || E'\: '
74 END || value, E'\n' ORDER BY mk.ordering, mk.key)
75 FROM
76 source_metadata sm
77 JOIN metadata_keys mk ON mk.key_id = sm.key_id
78 WHERE s.id=sm.src_id
79 )
80 ||
81 CASE
82 WHEN src_associations_full.extra_source THEN E'\nExtra-Source-Only\: yes'
83 ELSE ''
84 END
85 ||
86 E'\nDirectory\: pool/' || :component_name || '/' || SUBSTRING(f.filename FROM E'\\A(.*)/[^/]*\\Z')
87 ||
88 E'\nPriority\: ' || COALESCE(pri.priority, 'optional')
89 ||
90 E'\nSection\: ' || COALESCE(sec.section, 'misc')
92FROM
94source s
95JOIN src_associations_full ON src_associations_full.suite = :suite AND s.id = src_associations_full.source
96JOIN files f ON s.file=f.id
97JOIN files_archive_map fam
98 ON fam.file_id = f.id
99 AND fam.archive_id = (SELECT archive_id FROM suite WHERE id = :suite)
100 AND fam.component_id = :component
101LEFT JOIN override o ON o.package = s.source
102 AND o.suite = :overridesuite
103 AND o.component = :component
104 AND o.type = :dsc_type
105LEFT JOIN section sec ON o.section = sec.id
106LEFT JOIN priority pri ON o.priority = pri.id
107LEFT JOIN suite on suite.id = :suite
109ORDER BY
110s.source, s.version
111"""
114def generate_sources(suite_id: int, component_id: int):
115 global _sources_query
116 from daklib.dakmultiprocessing import PROC_STATUS_SUCCESS
117 from daklib.dbconn import Component, DBConn, OverrideType, Suite
118 from daklib.filewriter import SourcesFileWriter
120 session = DBConn().session()
121 dsc_type = (
122 session.query(OverrideType).filter_by(overridetype="dsc").one().overridetype_id
123 )
125 suite = session.query(Suite).get(suite_id)
126 component = session.query(Component).get(component_id)
128 overridesuite_id = suite.get_overridesuite().suite_id
130 writer_args = {
131 "archive": suite.archive.path,
132 "suite": suite.suite_name,
133 "component": component.component_name,
134 }
135 if suite.indices_compression is not None: 135 ↛ 137line 135 didn't jump to line 137, because the condition on line 135 was never false
136 writer_args["compression"] = suite.indices_compression
137 writer = SourcesFileWriter(**writer_args)
138 output = writer.open()
140 # run query and write Sources
141 r = session.execute(
142 _sources_query,
143 {
144 "suite": suite_id,
145 "component": component_id,
146 "component_name": component.component_name,
147 "dsc_type": dsc_type,
148 "overridesuite": overridesuite_id,
149 },
150 )
151 for (stanza,) in r:
152 print(stanza, file=output)
153 print("", file=output)
155 writer.close()
157 message = ["generate sources", suite.suite_name, component.component_name]
158 session.rollback()
159 return (PROC_STATUS_SUCCESS, message)
162#############################################################################
165# Here be large dragons.
166_packages_query = R"""
167WITH
169 tmp AS (
170 SELECT
171 b.id AS binary_id,
172 b.package AS package,
173 b.version AS version,
174 b.architecture AS architecture,
175 b.source AS source_id,
176 s.source AS source,
177 f.filename AS filename,
178 f.size AS size,
179 f.md5sum AS md5sum,
180 f.sha1sum AS sha1sum,
181 f.sha256sum AS sha256sum,
182 (SELECT value FROM binaries_metadata
183 WHERE bin_id = b.id
184 AND key_id = (SELECT key_id FROM metadata_keys WHERE key = 'Priority'))
185 AS fallback_priority,
186 (SELECT value FROM binaries_metadata
187 WHERE bin_id = b.id
188 AND key_id = (SELECT key_id FROM metadata_keys WHERE key = 'Section'))
189 AS fallback_section
190 FROM
191 binaries b
192 JOIN bin_associations ba ON b.id = ba.bin
193 JOIN files f ON f.id = b.file
194 JOIN files_archive_map fam ON f.id = fam.file_id AND fam.archive_id = :archive_id
195 JOIN source s ON b.source = s.id
196 WHERE
197 (b.architecture = :arch_all OR b.architecture = :arch) AND b.type = :type_name
198 AND ba.suite = :suite
199 AND fam.component_id = :component
200 )
202SELECT
203 (SELECT
204 STRING_AGG(key || E'\: ' || value, E'\n' ORDER BY ordering, key)
205 FROM
206 (SELECT key, ordering,
207 CASE WHEN :include_long_description = 'false' AND key = 'Description'
208 THEN SUBSTRING(value FROM E'\\A[^\n]*')
209 ELSE value
210 END AS value
211 FROM
212 binaries_metadata bm
213 JOIN metadata_keys mk ON mk.key_id = bm.key_id
214 WHERE
215 bm.bin_id = tmp.binary_id
216 AND key != ALL (:metadata_skip)
217 ) AS metadata
218 )
219 || COALESCE(E'\n' || (SELECT
220 STRING_AGG(key || E'\: ' || value, E'\n' ORDER BY key)
221 FROM external_overrides eo
222 WHERE
223 eo.package = tmp.package
224 AND eo.suite = :overridesuite AND eo.component = :component
225 ), '')
226 || E'\nSection\: ' || COALESCE(sec.section, tmp.fallback_section)
227 || E'\nPriority\: ' || COALESCE(pri.priority, tmp.fallback_priority)
228 || E'\nFilename\: pool/' || :component_name || '/' || tmp.filename
229 || E'\nSize\: ' || tmp.size
230 || CASE WHEN suite.checksums && array['md5sum'] THEN E'\nMD5sum\: ' || tmp.md5sum ELSE '' END
231 || CASE WHEN suite.checksums && array['sha1'] THEN E'\nSHA1\: ' || tmp.sha1sum ELSE '' END
232 || CASE WHEN suite.checksums && array['sha256'] THEN E'\nSHA256\: ' || tmp.sha256sum ELSE '' END
234FROM
235 tmp
236 LEFT JOIN override o ON o.package = tmp.package
237 AND o.type = :type_id
238 AND o.suite = :overridesuite
239 AND o.component = :component
240 LEFT JOIN section sec ON sec.id = o.section
241 LEFT JOIN priority pri ON pri.id = o.priority
242 LEFT JOIN suite ON suite.id = :suite
244WHERE
245 (
246 architecture <> :arch_all
247 OR
248 (architecture = :arch_all AND source_id IN (SELECT source_id FROM tmp WHERE architecture <> :arch_all))
249 OR
250 (architecture = :arch_all AND source NOT IN (SELECT DISTINCT source FROM tmp WHERE architecture <> :arch_all))
251 )
253ORDER BY tmp.source, tmp.package, tmp.version
254"""
257def generate_packages(
258 suite_id: int, component_id: int, architecture_id: int, type_name: str
259):
260 global _packages_query
261 from daklib.dakmultiprocessing import PROC_STATUS_SUCCESS
262 from daklib.dbconn import Architecture, Component, DBConn, OverrideType, Suite
263 from daklib.filewriter import PackagesFileWriter
265 session = DBConn().session()
266 arch_all_id = session.query(Architecture).filter_by(arch_string="all").one().arch_id
267 type_id = (
268 session.query(OverrideType)
269 .filter_by(overridetype=type_name)
270 .one()
271 .overridetype_id
272 )
274 suite = session.query(Suite).get(suite_id)
275 component = session.query(Component).get(component_id)
276 architecture = session.query(Architecture).get(architecture_id)
278 overridesuite_id = suite.get_overridesuite().suite_id
279 include_long_description = suite.include_long_description
281 # We currently filter out the "Tag" line. They are set by external
282 # overrides and NOT by the maintainer. And actually having it set by
283 # maintainer means we output it twice at the moment -> which breaks
284 # dselect.
285 metadata_skip = ["Section", "Priority", "Tag"]
286 if include_long_description: 286 ↛ 287line 286 didn't jump to line 287, because the condition on line 286 was never true
287 metadata_skip.append("Description-md5")
289 writer_args = {
290 "archive": suite.archive.path,
291 "suite": suite.suite_name,
292 "component": component.component_name,
293 "architecture": architecture.arch_string,
294 "debtype": type_name,
295 }
296 if suite.indices_compression is not None: 296 ↛ 298line 296 didn't jump to line 298, because the condition on line 296 was never false
297 writer_args["compression"] = suite.indices_compression
298 writer = PackagesFileWriter(**writer_args)
299 output = writer.open()
301 r = session.execute(
302 _packages_query,
303 {
304 "archive_id": suite.archive.archive_id,
305 "suite": suite_id,
306 "component": component_id,
307 "component_name": component.component_name,
308 "arch": architecture_id,
309 "type_id": type_id,
310 "type_name": type_name,
311 "arch_all": arch_all_id,
312 "overridesuite": overridesuite_id,
313 "metadata_skip": metadata_skip,
314 "include_long_description": "true" if include_long_description else "false",
315 },
316 )
317 for (stanza,) in r:
318 print(stanza, file=output)
319 print("", file=output)
321 writer.close()
323 message = [
324 "generate-packages",
325 suite.suite_name,
326 component.component_name,
327 architecture.arch_string,
328 ]
329 session.rollback()
330 return (PROC_STATUS_SUCCESS, message)
333#############################################################################
336_translations_query = r"""
337WITH
338 override_suite AS
339 (SELECT
340 s.id AS id,
341 COALESCE(os.id, s.id) AS overridesuite_id
342 FROM suite AS s LEFT JOIN suite AS os ON s.overridesuite = os.suite_name)
344SELECT
345 E'Package\: ' || b.package
346 || E'\nDescription-md5\: ' || bm_description_md5.value
347 || E'\nDescription-en\: ' || bm_description.value
348 || E'\n'
349FROM binaries b
350 -- join tables for suite and component
351 JOIN bin_associations ba ON b.id = ba.bin
352 JOIN override_suite os ON os.id = ba.suite
353 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')
355 -- join tables for Description and Description-md5
356 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')
357 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')
359 -- we want to sort by source name
360 JOIN source s ON b.source = s.id
362WHERE ba.suite = :suite AND o.component = :component
363GROUP BY b.package, bm_description_md5.value, bm_description.value
364ORDER BY MIN(s.source), b.package, bm_description_md5.value
365"""
368def generate_translations(suite_id: int, component_id: int):
369 global _translations_query
370 from daklib.dakmultiprocessing import PROC_STATUS_SUCCESS
371 from daklib.dbconn import Component, DBConn, Suite
372 from daklib.filewriter import TranslationFileWriter
374 session = DBConn().session()
375 suite = session.query(Suite).get(suite_id)
376 component = session.query(Component).get(component_id)
378 writer_args = {
379 "archive": suite.archive.path,
380 "suite": suite.suite_name,
381 "component": component.component_name,
382 "language": "en",
383 }
384 if suite.i18n_compression is not None: 384 ↛ 386line 384 didn't jump to line 386, because the condition on line 384 was never false
385 writer_args["compression"] = suite.i18n_compression
386 writer = TranslationFileWriter(**writer_args)
387 output = writer.open()
389 r = session.execute(
390 _translations_query, {"suite": suite_id, "component": component_id}
391 )
392 for (stanza,) in r:
393 print(stanza, file=output)
395 writer.close()
397 message = ["generate-translations", suite.suite_name, component.component_name]
398 session.rollback()
399 return (PROC_STATUS_SUCCESS, message)
402#############################################################################
405def main():
406 from daklib import daklog
407 from daklib.config import Config
409 cnf = Config()
411 Arguments = [
412 ("h", "help", "Generate-Packages-Sources::Options::Help"),
413 ("a", "archive", "Generate-Packages-Sources::Options::Archive", "HasArg"),
414 ("s", "suite", "Generate-Packages-Sources::Options::Suite", "HasArg"),
415 ("f", "force", "Generate-Packages-Sources::Options::Force"),
416 ("o", "option", "", "ArbItem"),
417 ]
419 apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
420 try:
421 Options = cnf.subtree("Generate-Packages-Sources::Options")
422 except KeyError:
423 Options = {}
425 if "Help" in Options:
426 usage()
428 from daklib.dakmultiprocessing import (
429 PROC_STATUS_SIGNALRAISED,
430 PROC_STATUS_SUCCESS,
431 DakProcessPool,
432 )
434 pool = DakProcessPool()
436 logger = daklog.Logger("generate-packages-sources2")
438 from daklib.dbconn import Archive, DBConn, Suite, get_suite
440 session = DBConn().session()
441 session.execute("SELECT add_missing_description_md5()")
442 session.commit()
444 import daklib.utils
446 if "Suite" in Options:
447 suites = []
448 suite_names = daklib.utils.split_args(Options["Suite"])
449 for s in suite_names:
450 suite = get_suite(s.lower(), session)
451 if suite: 451 ↛ 454line 451 didn't jump to line 454, because the condition on line 451 was never false
452 suites.append(suite)
453 else:
454 print("I: Cannot find suite %s" % s)
455 logger.log(["Cannot find suite %s" % s])
456 else:
457 query = session.query(Suite).filter(Suite.untouchable == False) # noqa:E712
458 if "Archive" in Options: 458 ↛ 463line 458 didn't jump to line 463, because the condition on line 458 was never false
459 archive_names = daklib.utils.split_args(Options["Archive"])
460 query = query.join(Suite.archive).filter(
461 Archive.archive_name.in_(archive_names)
462 )
463 suites = query.all()
465 force = "Force" in Options and Options["Force"]
467 def parse_results(message):
468 # Split out into (code, msg)
469 code, msg = message
470 if code == PROC_STATUS_SUCCESS: 470 ↛ 472line 470 didn't jump to line 472, because the condition on line 470 was never false
471 logger.log(msg)
472 elif code == PROC_STATUS_SIGNALRAISED:
473 logger.log(["E: Subprocess received signal ", msg])
474 else:
475 logger.log(["E: ", msg])
477 # Lock tables so that nobody can change things underneath us
478 session.execute("LOCK TABLE src_associations IN SHARE MODE")
479 session.execute("LOCK TABLE bin_associations IN SHARE MODE")
481 for s in suites:
482 component_ids = [c.component_id for c in s.components]
483 if s.untouchable and not force: 483 ↛ 484line 483 didn't jump to line 484, because the condition on line 483 was never true
484 import daklib.utils
486 daklib.utils.fubar(
487 "Refusing to touch %s (untouchable and not forced)" % s.suite_name
488 )
489 for c in component_ids:
490 pool.apply_async(generate_sources, [s.suite_id, c], callback=parse_results)
491 if not s.include_long_description: 491 ↛ 495line 491 didn't jump to line 495, because the condition on line 491 was never false
492 pool.apply_async(
493 generate_translations, [s.suite_id, c], callback=parse_results
494 )
495 for a in s.architectures:
496 if a == "source":
497 continue
498 pool.apply_async(
499 generate_packages,
500 [s.suite_id, c, a.arch_id, "deb"],
501 callback=parse_results,
502 )
503 pool.apply_async(
504 generate_packages,
505 [s.suite_id, c, a.arch_id, "udeb"],
506 callback=parse_results,
507 )
509 pool.close()
510 pool.join()
512 # this script doesn't change the database
513 session.close()
515 logger.close()
517 sys.exit(pool.overall_status())
520if __name__ == "__main__":
521 main()