Package dak :: Module generate_packages_sources2
[hide private]
[frames] | no frames]

Source Code for Module dak.generate_packages_sources2

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