import_repository.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #! /usr/bin/env python
  2. #
  3. # Copyright (C) 2015, Ansgar Burchardt <ansgar@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. from __future__ import print_function
  19. import daklib.archive
  20. import daklib.config
  21. import daklib.dbconn
  22. import daklib.import_repository
  23. import daklib.utils
  24. import apt_pkg
  25. import sys
  26. from collections import defaultdict
  27. def usage(status=0):
  28. print("""
  29. dak import-repository
  30. --keyring=/usr/share/keyring/debian-archive-keyring.gpg
  31. [--key=${fingerprint}]
  32. [--architectures=a,b,c (default: architectures in origin suite)]
  33. [--components=main,contrib (default: components in origin suite)]
  34. [--target-suite=${suite} (default: origin suite name)]
  35. [--add-overrides]
  36. [--max-packages=${n} (import at maximum ${n} packages, default: no limit)]
  37. http://httpredir.debian.org/debian unstable
  38. Things to think about:
  39. - Import Built-Using sources
  40. - all / only referenced
  41. - Remove old packages:
  42. - by-source: remove source X_v, if no X exists upstream
  43. - by-version: remove source X_v, if no X_v exists upstream
  44. (X denotes package name, v version, X_v package at a specific version)
  45. - Import all or only newest?
  46. - Expire binary packages?
  47. """)
  48. sys.exit(status)
  49. def entry_is_newer(entry, packages):
  50. version = entry['Version']
  51. for p in packages[entry['Package']]:
  52. if apt_pkg.version_compare(version, p.version) <= 0:
  53. return False
  54. return True
  55. def entry_in_packages(entry, packages):
  56. return entry['Package'] in packages
  57. def get_packages_in_suite(suite):
  58. sources = defaultdict(list)
  59. for s in suite.sources:
  60. sources[s.source].append(s)
  61. packages = defaultdict(list)
  62. for b in suite.binaries:
  63. packages[b.package].append(b)
  64. return sources, packages
  65. def import_sources(base, sources, transaction, target_suite, component, target_sources, extra_sources, extra_sources_comp, max_packages=None):
  66. n = 0
  67. for entry in sources:
  68. if max_packages is not None and n > max_packages:
  69. break
  70. if entry.get('Extra-Source-Only', 'no') == 'yes':
  71. # Remember package, we might need to import it later.
  72. key = (entry['Package'], entry['Version'])
  73. extra_sources[key] = entry
  74. extra_sources_comp[key].add(c)
  75. continue
  76. if not entry_in_packages(entry, target_sources) or entry_is_newer(entry, target_sources):
  77. print("Importing {0}={1}".format(entry['Package'], entry['Version']))
  78. daklib.import_repository.import_source_to_suite(base, entry, transaction, target_suite, component)
  79. n += 1
  80. #transaction.commit()
  81. return n
  82. def import_built_using(base, source, version, transaction, target_suite, component, extra_sources, extra_sources_comp):
  83. if not daklib.import_repository.source_in_archive(bu_source, bu_version, target_suite.archive):
  84. print("Importing extra source {0}={1}".format(bu_source, bu_version))
  85. key = (bu_source, bu_version)
  86. extra_entry = extra_sources.get(key)
  87. if extra_entry is None:
  88. raise Exception("Extra source {0}={1} referenced by {2}={3} ({4}) not found in source suite.".format(bu_source, bu_version, entry['Package'], entry['Version'], architecture))
  89. extra_components = extra_sources_comp[key]
  90. if c in components:
  91. extra_component = component
  92. else:
  93. # TODO: Take preferred components from those listed...
  94. raise Exception("Not implemented.")
  95. daklib.import_repository.import_source_to_suite(base, extra_entry, transaction, target_suite, extra_component)
  96. def import_packages(base, packages, transaction, target_suite, component, architecture, target_binaries, extra_sources, extra_sources_comp, max_packages=None):
  97. n = 0
  98. for entry in packages:
  99. if max_packages is not None and n > max_packages:
  100. break
  101. if not entry_in_packages(entry, target_binaries) or entry_is_newer(entry, target_binaries):
  102. print("Importing {0}={1} ({2})".format(entry['Package'], entry['Version'], architecture))
  103. # Import Built-Using sources:
  104. for bu_source, bu_version in daklib.utils.parse_built_using(entry):
  105. import_built_using(base, bu_source, bu_version, transaction, target_suite, component, extra_sources, extra_sources_comp)
  106. # Import binary:
  107. daklib.import_repository.import_package_to_suite(base, entry, transaction, target_suite, component)
  108. n += 1
  109. #transaction.commit()
  110. return n
  111. def main(argv=None):
  112. if argv is None:
  113. argv = sys.argv
  114. arguments = [
  115. ('h', 'help', 'Import-Repository::Help'),
  116. ('k', 'keyring', 'Import-Repository::Keyring', 'HasArg'),
  117. ('K', 'key', 'Import-Repository::Key', 'HasArg'),
  118. ('a', 'architectures', 'Import-Repository::Architectures', 'HasArg'),
  119. ('c', 'components', 'Import-Repository::Components', 'HasArg'),
  120. ('t', 'target-suite', 'Import-Repository::Target-Suite', 'HasArg'),
  121. ('A', 'add-overrides', 'Import-Repository::AddOverrides'),
  122. ('n', 'max-packages', 'Import-Repository::MaxPackages', 'HasArg'),
  123. ]
  124. cnf = daklib.config.Config()
  125. argv = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
  126. options = cnf.subtree('Import-Repository')
  127. if 'Help' in options or len(argv) < 2:
  128. usage(0)
  129. keyring = options.find('Keyring') or None
  130. if keyring is None:
  131. print("Error: No keyring specified")
  132. print()
  133. if 'Key' in options:
  134. raise Exception('Not implemented.')
  135. if 'AddOverrides' in options:
  136. raise Exception('Not implemented.')
  137. if 'MaxPackages' in options:
  138. max_packages = long(options['MaxPackages'])
  139. else:
  140. max_packages = None
  141. base, suite = argv[0:2]
  142. target_suite_name = options.find('Target-Suite') or suite
  143. print("Importing packages from {0}/dists/{1} to {2}".format(base, suite, target_suite_name))
  144. with daklib.archive.ArchiveTransaction() as transaction:
  145. target_suite = daklib.dbconn.get_suite(target_suite_name, transaction.session)
  146. if target_suite is None:
  147. daklib.utils.fubar("Target suite '{0}' is unknown.".format(target_suite_name))
  148. release = daklib.import_repository.obtain_release(base, suite, keyring)
  149. target_sources, target_binaries = get_packages_in_suite(target_suite)
  150. if 'Architectures' in options:
  151. architectures = options['Architectures'].split(',')
  152. else:
  153. architectures = ['all'] + release.architectures()
  154. if 'Components' in options:
  155. components = options['Components'].split(',')
  156. else:
  157. components = release.components()
  158. # TODO: Clean this up...
  159. n = 0
  160. # For Extra-Source-Only sources packages, keep a dict
  161. # (name, version) -> entry and (name, version) -> set of components
  162. # to allow importing needed packages at a later stage
  163. extra_sources = dict()
  164. extra_sources_comp = defaultdict(set)
  165. for c in components:
  166. component = daklib.dbconn.get_component(c, transaction.session)
  167. print("Processing {0}/source...".format(c))
  168. sources = release.sources(c)
  169. imported = import_sources(base, sources, transaction, target_suite, component, target_sources, extra_sources, extra_sources_comp, max_packages)
  170. print(" imported {0} source packages".format(imported))
  171. n += imported
  172. if max_packages is not None:
  173. max_packages -= n
  174. for c in components:
  175. component = daklib.dbconn.get_component(c, transaction.session)
  176. for architecture in architectures:
  177. print("Processing {0}/{1}...".format(c, architecture))
  178. packages = release.packages(c, architecture)
  179. imported = import_packages(base, packages, transaction, target_suite, component, architecture, target_binaries, extra_sources, extra_sources_comp, max_packages)
  180. print(" imported {0} binary packages".format(imported))
  181. n += imported
  182. if max_packages is not None:
  183. max_packages -= n
  184. transaction.rollback()
  185. if __name__ == '__main__':
  186. main()