generate_packages_sources2.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #! /usr/bin/env python3
  2. """
  3. Generate Packages/Sources files
  4. @contact: Debian FTP Master <ftpmaster@debian.org>
  5. @copyright: 2011 Ansgar Burchardt <ansgar@debian.org>
  6. @copyright: Based on daklib/lists.py and dak/generate_filelist.py:
  7. 2009-2011 Torsten Werner <twerner@debian.org>
  8. @copyright: Based on dak/generate_packages_sources.py:
  9. 2000, 2001, 2002, 2006 James Troup <james@nocrew.org>
  10. 2009 Mark Hymers <mhy@debian.org>
  11. 2010 Joerg Jaspert <joerg@debian.org>
  12. @license: GNU General Public License version 2 or later
  13. """
  14. # This program is free software; you can redistribute it and/or modify
  15. # it under the terms of the GNU General Public License as published by
  16. # the Free Software Foundation; either version 2 of the License, or
  17. # (at your option) any later version.
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  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. from typing import NoReturn
  26. import apt_pkg
  27. import sys
  28. def usage() -> NoReturn:
  29. print("""Usage: dak generate-packages-sources2 [OPTIONS]
  30. Generate the Packages/Sources files
  31. -a, --archive=ARCHIVE process suites in ARCHIVE
  32. -s, --suite=SUITE process this suite
  33. Default: All suites not marked 'untouchable'
  34. -f, --force Allow processing of untouchable suites
  35. CAREFUL: Only to be used at point release time!
  36. -h, --help show this help and exit
  37. SUITE can be a space separated list, e.g.
  38. --suite=unstable testing
  39. """)
  40. sys.exit()
  41. #############################################################################
  42. # Here be dragons.
  43. _sources_query = R"""
  44. SELECT
  45. (SELECT
  46. STRING_AGG(
  47. CASE
  48. WHEN key = 'Source' THEN E'Package\: '
  49. WHEN key = 'Files' AND suite.checksums && array['md5sum'] THEN E'Files\:\n ' || f.md5sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
  50. WHEN key = 'Files' THEN NULL
  51. WHEN key = 'Checksums-Sha1' AND suite.checksums && array['sha1'] THEN E'Checksums-Sha1\:\n ' || f.sha1sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
  52. WHEN key = 'Checksums-Sha1' THEN NULL
  53. WHEN key = 'Checksums-Sha256' AND suite.checksums && array['sha256'] THEN E'Checksums-Sha256\:\n ' || f.sha256sum || ' ' || f.size || ' ' || SUBSTRING(f.filename FROM E'/([^/]*)\\Z')
  54. WHEN key = 'Checksums-Sha256' THEN NULL
  55. ELSE key || E'\: '
  56. END || value, E'\n' ORDER BY mk.ordering, mk.key)
  57. FROM
  58. source_metadata sm
  59. JOIN metadata_keys mk ON mk.key_id = sm.key_id
  60. WHERE s.id=sm.src_id
  61. )
  62. ||
  63. CASE
  64. WHEN src_associations_full.extra_source THEN E'\nExtra-Source-Only\: yes'
  65. ELSE ''
  66. END
  67. ||
  68. E'\nDirectory\: pool/' || :component_name || '/' || SUBSTRING(f.filename FROM E'\\A(.*)/[^/]*\\Z')
  69. ||
  70. E'\nPriority\: ' || COALESCE(pri.priority, 'optional')
  71. ||
  72. E'\nSection\: ' || COALESCE(sec.section, 'misc')
  73. FROM
  74. source s
  75. JOIN src_associations_full ON src_associations_full.suite = :suite AND s.id = src_associations_full.source
  76. JOIN files f ON s.file=f.id
  77. JOIN files_archive_map fam
  78. ON fam.file_id = f.id
  79. AND fam.archive_id = (SELECT archive_id FROM suite WHERE id = :suite)
  80. AND fam.component_id = :component
  81. LEFT JOIN override o ON o.package = s.source
  82. AND o.suite = :overridesuite
  83. AND o.component = :component
  84. AND o.type = :dsc_type
  85. LEFT JOIN section sec ON o.section = sec.id
  86. LEFT JOIN priority pri ON o.priority = pri.id
  87. LEFT JOIN suite on suite.id = :suite
  88. ORDER BY
  89. s.source, s.version
  90. """
  91. def generate_sources(suite_id: int, component_id: int):
  92. global _sources_query
  93. from daklib.filewriter import SourcesFileWriter
  94. from daklib.dbconn import Component, DBConn, OverrideType, Suite
  95. from daklib.dakmultiprocessing import PROC_STATUS_SUCCESS
  96. session = DBConn().session()
  97. dsc_type = session.query(OverrideType).filter_by(overridetype='dsc').one().overridetype_id
  98. suite = session.query(Suite).get(suite_id)
  99. component = session.query(Component).get(component_id)
  100. overridesuite_id = suite.get_overridesuite().suite_id
  101. writer_args = {
  102. 'archive': suite.archive.path,
  103. 'suite': suite.suite_name,
  104. 'component': component.component_name
  105. }
  106. if suite.indices_compression is not None:
  107. writer_args['compression'] = suite.indices_compression
  108. writer = SourcesFileWriter(**writer_args)
  109. output = writer.open()
  110. # run query and write Sources
  111. r = session.execute(_sources_query, {"suite": suite_id, "component": component_id, "component_name": component.component_name, "dsc_type": dsc_type, "overridesuite": overridesuite_id})
  112. for (stanza,) in r:
  113. print(stanza, file=output)
  114. print("", file=output)
  115. writer.close()
  116. message = ["generate sources", suite.suite_name, component.component_name]
  117. session.rollback()
  118. return (PROC_STATUS_SUCCESS, message)
  119. #############################################################################
  120. # Here be large dragons.
  121. _packages_query = R"""
  122. WITH
  123. tmp AS (
  124. SELECT
  125. b.id AS binary_id,
  126. b.package AS package,
  127. b.version AS version,
  128. b.architecture AS architecture,
  129. b.source AS source_id,
  130. s.source AS source,
  131. f.filename AS filename,
  132. f.size AS size,
  133. f.md5sum AS md5sum,
  134. f.sha1sum AS sha1sum,
  135. f.sha256sum AS sha256sum,
  136. (SELECT value FROM binaries_metadata
  137. WHERE bin_id = b.id
  138. AND key_id = (SELECT key_id FROM metadata_keys WHERE key = 'Priority'))
  139. AS fallback_priority,
  140. (SELECT value FROM binaries_metadata
  141. WHERE bin_id = b.id
  142. AND key_id = (SELECT key_id FROM metadata_keys WHERE key = 'Section'))
  143. AS fallback_section
  144. FROM
  145. binaries b
  146. JOIN bin_associations ba ON b.id = ba.bin
  147. JOIN files f ON f.id = b.file
  148. JOIN files_archive_map fam ON f.id = fam.file_id AND fam.archive_id = :archive_id
  149. JOIN source s ON b.source = s.id
  150. WHERE
  151. (b.architecture = :arch_all OR b.architecture = :arch) AND b.type = :type_name
  152. AND ba.suite = :suite
  153. AND fam.component_id = :component
  154. )
  155. SELECT
  156. (SELECT
  157. STRING_AGG(key || E'\: ' || value, E'\n' ORDER BY ordering, key)
  158. FROM
  159. (SELECT key, ordering,
  160. CASE WHEN :include_long_description = 'false' AND key = 'Description'
  161. THEN SUBSTRING(value FROM E'\\A[^\n]*')
  162. ELSE value
  163. END AS value
  164. FROM
  165. binaries_metadata bm
  166. JOIN metadata_keys mk ON mk.key_id = bm.key_id
  167. WHERE
  168. bm.bin_id = tmp.binary_id
  169. AND key != ALL (:metadata_skip)
  170. ) AS metadata
  171. )
  172. || COALESCE(E'\n' || (SELECT
  173. STRING_AGG(key || E'\: ' || value, E'\n' ORDER BY key)
  174. FROM external_overrides eo
  175. WHERE
  176. eo.package = tmp.package
  177. AND eo.suite = :overridesuite AND eo.component = :component
  178. ), '')
  179. || E'\nSection\: ' || COALESCE(sec.section, tmp.fallback_section)
  180. || E'\nPriority\: ' || COALESCE(pri.priority, tmp.fallback_priority)
  181. || E'\nFilename\: pool/' || :component_name || '/' || tmp.filename
  182. || E'\nSize\: ' || tmp.size
  183. || CASE WHEN suite.checksums && array['md5sum'] THEN E'\nMD5sum\: ' || tmp.md5sum ELSE '' END
  184. || CASE WHEN suite.checksums && array['sha1'] THEN E'\nSHA1\: ' || tmp.sha1sum ELSE '' END
  185. || CASE WHEN suite.checksums && array['sha256'] THEN E'\nSHA256\: ' || tmp.sha256sum ELSE '' END
  186. FROM
  187. tmp
  188. LEFT JOIN override o ON o.package = tmp.package
  189. AND o.type = :type_id
  190. AND o.suite = :overridesuite
  191. AND o.component = :component
  192. LEFT JOIN section sec ON sec.id = o.section
  193. LEFT JOIN priority pri ON pri.id = o.priority
  194. LEFT JOIN suite ON suite.id = :suite
  195. WHERE
  196. (
  197. architecture <> :arch_all
  198. OR
  199. (architecture = :arch_all AND source_id IN (SELECT source_id FROM tmp WHERE architecture <> :arch_all))
  200. OR
  201. (architecture = :arch_all AND source NOT IN (SELECT DISTINCT source FROM tmp WHERE architecture <> :arch_all))
  202. )
  203. ORDER BY tmp.source, tmp.package, tmp.version
  204. """
  205. def generate_packages(suite_id: int, component_id: int, architecture_id: int, type_name: str):
  206. global _packages_query
  207. from daklib.filewriter import PackagesFileWriter
  208. from daklib.dbconn import Architecture, Component, DBConn, OverrideType, Suite
  209. from daklib.dakmultiprocessing import PROC_STATUS_SUCCESS
  210. session = DBConn().session()
  211. arch_all_id = session.query(Architecture).filter_by(arch_string='all').one().arch_id
  212. type_id = session.query(OverrideType).filter_by(overridetype=type_name).one().overridetype_id
  213. suite = session.query(Suite).get(suite_id)
  214. component = session.query(Component).get(component_id)
  215. architecture = session.query(Architecture).get(architecture_id)
  216. overridesuite_id = suite.get_overridesuite().suite_id
  217. include_long_description = suite.include_long_description
  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 include_long_description:
  224. metadata_skip.append("Description-md5")
  225. writer_args = {
  226. 'archive': suite.archive.path,
  227. 'suite': suite.suite_name,
  228. 'component': component.component_name,
  229. 'architecture': architecture.arch_string,
  230. 'debtype': type_name
  231. }
  232. if suite.indices_compression is not None:
  233. writer_args['compression'] = suite.indices_compression
  234. writer = PackagesFileWriter(**writer_args)
  235. output = writer.open()
  236. r = session.execute(_packages_query, {"archive_id": suite.archive.archive_id,
  237. "suite": suite_id, "component": component_id, 'component_name': component.component_name,
  238. "arch": architecture_id, "type_id": type_id, "type_name": type_name, "arch_all": arch_all_id,
  239. "overridesuite": overridesuite_id, "metadata_skip": metadata_skip,
  240. "include_long_description": 'true' if include_long_description else 'false'})
  241. for (stanza,) in r:
  242. print(stanza, file=output)
  243. print("", file=output)
  244. writer.close()
  245. message = ["generate-packages", suite.suite_name, component.component_name, architecture.arch_string]
  246. session.rollback()
  247. return (PROC_STATUS_SUCCESS, message)
  248. #############################################################################
  249. _translations_query = r"""
  250. WITH
  251. override_suite AS
  252. (SELECT
  253. s.id AS id,
  254. COALESCE(os.id, s.id) AS overridesuite_id
  255. FROM suite AS s LEFT JOIN suite AS os ON s.overridesuite = os.suite_name)
  256. SELECT
  257. E'Package\: ' || b.package
  258. || E'\nDescription-md5\: ' || bm_description_md5.value
  259. || E'\nDescription-en\: ' || bm_description.value
  260. || E'\n'
  261. FROM binaries b
  262. -- join tables for suite and component
  263. JOIN bin_associations ba ON b.id = ba.bin
  264. JOIN override_suite os ON os.id = ba.suite
  265. 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')
  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. -- we want to sort by source name
  270. JOIN source s ON b.source = s.id
  271. WHERE ba.suite = :suite AND o.component = :component
  272. GROUP BY b.package, bm_description_md5.value, bm_description.value
  273. ORDER BY MIN(s.source), b.package, bm_description_md5.value
  274. """
  275. def generate_translations(suite_id: int, component_id: int):
  276. global _translations_query
  277. from daklib.filewriter import TranslationFileWriter
  278. from daklib.dbconn import DBConn, Suite, Component
  279. from daklib.dakmultiprocessing import PROC_STATUS_SUCCESS
  280. session = DBConn().session()
  281. suite = session.query(Suite).get(suite_id)
  282. component = session.query(Component).get(component_id)
  283. writer_args = {
  284. 'archive': suite.archive.path,
  285. 'suite': suite.suite_name,
  286. 'component': component.component_name,
  287. 'language': 'en',
  288. }
  289. if suite.i18n_compression is not None:
  290. writer_args['compression'] = suite.i18n_compression
  291. writer = TranslationFileWriter(**writer_args)
  292. output = writer.open()
  293. r = session.execute(_translations_query, {"suite": suite_id, "component": component_id})
  294. for (stanza,) in r:
  295. print(stanza, file=output)
  296. writer.close()
  297. message = ["generate-translations", suite.suite_name, component.component_name]
  298. session.rollback()
  299. return (PROC_STATUS_SUCCESS, message)
  300. #############################################################################
  301. def main():
  302. from daklib.config import Config
  303. from daklib import daklog
  304. cnf = Config()
  305. Arguments = [('h', "help", "Generate-Packages-Sources::Options::Help"),
  306. ('a', 'archive', 'Generate-Packages-Sources::Options::Archive', 'HasArg'),
  307. ('s', "suite", "Generate-Packages-Sources::Options::Suite", 'HasArg'),
  308. ('f', "force", "Generate-Packages-Sources::Options::Force"),
  309. ('o', 'option', '', 'ArbItem')]
  310. apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
  311. try:
  312. Options = cnf.subtree("Generate-Packages-Sources::Options")
  313. except KeyError:
  314. Options = {}
  315. if "Help" in Options:
  316. usage()
  317. from daklib.dakmultiprocessing import DakProcessPool, PROC_STATUS_SUCCESS, PROC_STATUS_SIGNALRAISED
  318. pool = DakProcessPool()
  319. logger = daklog.Logger('generate-packages-sources2')
  320. from daklib.dbconn import DBConn, get_suite, Suite, Archive
  321. session = DBConn().session()
  322. session.execute("SELECT add_missing_description_md5()")
  323. session.commit()
  324. import daklib.utils
  325. if "Suite" in Options:
  326. suites = []
  327. suite_names = daklib.utils.split_args(Options['Suite'])
  328. for s in suite_names:
  329. suite = get_suite(s.lower(), session)
  330. if suite:
  331. suites.append(suite)
  332. else:
  333. print("I: Cannot find suite %s" % s)
  334. logger.log(['Cannot find suite %s' % s])
  335. else:
  336. query = session.query(Suite).filter(Suite.untouchable == False) # noqa:E712
  337. if 'Archive' in Options:
  338. archive_names = daklib.utils.split_args(Options['Archive'])
  339. query = query.join(Suite.archive).filter(Archive.archive_name.in_(archive_names))
  340. suites = query.all()
  341. force = "Force" in Options and Options["Force"]
  342. def parse_results(message):
  343. # Split out into (code, msg)
  344. code, msg = message
  345. if code == PROC_STATUS_SUCCESS:
  346. logger.log(msg)
  347. elif code == PROC_STATUS_SIGNALRAISED:
  348. logger.log(['E: Subprocess received signal ', msg])
  349. else:
  350. logger.log(['E: ', msg])
  351. # Lock tables so that nobody can change things underneath us
  352. session.execute("LOCK TABLE src_associations IN SHARE MODE")
  353. session.execute("LOCK TABLE bin_associations IN SHARE MODE")
  354. for s in suites:
  355. component_ids = [c.component_id for c in s.components]
  356. if s.untouchable and not force:
  357. import daklib.utils
  358. daklib.utils.fubar("Refusing to touch %s (untouchable and not forced)" % s.suite_name)
  359. for c in component_ids:
  360. pool.apply_async(generate_sources, [s.suite_id, c], callback=parse_results)
  361. if not s.include_long_description:
  362. pool.apply_async(generate_translations, [s.suite_id, c], callback=parse_results)
  363. for a in s.architectures:
  364. if a == 'source':
  365. continue
  366. pool.apply_async(generate_packages, [s.suite_id, c, a.arch_id, 'deb'], callback=parse_results)
  367. pool.apply_async(generate_packages, [s.suite_id, c, a.arch_id, 'udeb'], callback=parse_results)
  368. pool.close()
  369. pool.join()
  370. # this script doesn't change the database
  371. session.close()
  372. logger.close()
  373. sys.exit(pool.overall_status())
  374. if __name__ == '__main__':
  375. main()