3 # Copyright (C) 2015, Ansgar Burchardt <ansgar@debian.org>
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.
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.
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.
19 from __future__ import print_function
24 import daklib.import_repository
30 from collections import defaultdict
35 --keyring=/usr/share/keyring/debian-archive-keyring.gpg
36 [--key=${fingerprint}]
37 [--architectures=a,b,c (default: architectures in origin suite)]
38 [--components=main,contrib (default: components in origin suite)]
39 [--target-suite=${suite} (default: origin suite name)]
41 [--max-packages=${n} (import at maximum ${n} packages, default: no limit)]
42 http://httpredir.debian.org/debian unstable
44 Things to think about:
45 - Import Built-Using sources
46 - all / only referenced
47 - Remove old packages:
48 - by-source: remove source X_v, if no X exists upstream
49 - by-version: remove source X_v, if no X_v exists upstream
50 (X denotes package name, v version, X_v package at a specific version)
51 - Import all or only newest?
52 - Expire binary packages?
56 def entry_is_newer(entry, packages):
57 version = entry['Version']
58 for p in packages[entry['Package']]:
59 if apt_pkg.version_compare(version, p.version) <= 0:
63 def entry_in_packages(entry, packages):
64 return entry['Package'] in packages
66 def get_packages_in_suite(suite):
67 sources = defaultdict(list)
68 for s in suite.sources:
69 sources[s.source].append(s)
71 packages = defaultdict(list)
72 for b in suite.binaries:
73 packages[b.package].append(b)
75 return sources, packages
77 def import_sources(base, sources, transaction, target_suite, component, target_sources, extra_sources, extra_sources_comp, max_packages=None):
80 if max_packages is not None and n > max_packages:
82 if entry.get('Extra-Source-Only', 'no') == 'yes':
83 # Remember package, we might need to import it later.
84 key = (entry['Package'], entry['Version'])
85 extra_sources[key] = entry
86 extra_sources_comp[key].add(c)
88 if not entry_in_packages(entry, target_sources) or entry_is_newer(entry, target_sources):
89 print("Importing {0}={1}".format(entry['Package'], entry['Version']))
90 daklib.import_repository.import_source_to_suite(base, entry, transaction, target_suite, component)
95 def import_built_using(base, source, version, transaction, target_suite, component, extra_sources, extra_sources_comp):
96 if not daklib.import_repository.source_in_archive(bu_source, bu_version, target_suite.archive):
97 print("Importing extra source {0}={1}".format(bu_source, bu_version))
98 key = (bu_source, bu_version)
99 extra_entry = extra_sources.get(key)
100 if extra_entry is None:
101 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))
102 extra_components = extra_sources_comp[key]
104 extra_component = component
106 # TODO: Take preferred components from those listed...
107 raise Exception("Not implemented.")
108 daklib.import_repository.import_source_to_suite(base, extra_entry, transaction, target_suite, extra_component)
110 def import_packages(base, packages, transaction, target_suite, component, architecture, target_binaries, extra_sources, extra_sources_comp, max_packages=None):
112 for entry in packages:
113 if max_packages is not None and n > max_packages:
115 if not entry_in_packages(entry, target_binaries) or entry_is_newer(entry, target_binaries):
116 print("Importing {0}={1} ({2})".format(entry['Package'], entry['Version'], architecture))
117 # Import Built-Using sources:
118 for bu_source, bu_version in daklib.utils.parse_built_using(entry):
119 import_built_using(base, bu_source, bu_version, transaction, target_suite, component, extra_sources, extra_sources_comp)
121 daklib.import_repository.import_package_to_suite(base, entry, transaction, target_suite, component)
123 #transaction.commit()
131 ('h', 'help', 'Import-Repository::Help'),
132 ('k', 'keyring', 'Import-Repository::Keyring', 'HasArg'),
133 ('K', 'key', 'Import-Repository::Key', 'HasArg'),
134 ('a', 'architectures', 'Import-Repository::Architectures', 'HasArg'),
135 ('c', 'components', 'Import-Repository::Components', 'HasArg'),
136 ('t', 'target-suite', 'Import-Repository::Target-Suite', 'HasArg'),
137 ('A', 'add-overrides', 'Import-Repository::AddOverrides'),
138 ('n', 'max-packages', 'Import-Repository::MaxPackages', 'HasArg'),
141 cnf = daklib.config.Config();
142 argv = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
143 options = cnf.subtree('Import-Repository')
145 if 'Help' in options or len(argv) < 2:
148 keyring = options.find('Keyring') or None
150 print("Error: No keyring specified")
154 raise Exception('Not implemented.')
156 if 'AddOverrides' in options:
157 raise Exception('Not implemented.')
159 if 'MaxPackages' in options:
160 max_packages = long(options['MaxPackages'])
164 base, suite = argv[0:2]
166 target_suite_name = options.find('Target-Suite') or suite
168 print("Importing packages from {0}/dists/{1} to {2}".format(base, suite, target_suite_name))
169 with daklib.archive.ArchiveTransaction() as transaction:
170 target_suite = daklib.dbconn.get_suite(target_suite_name, transaction.session)
171 if target_suite is None:
172 daklib.utils.fubar("Target suite '{0}' is unknown.".format(target_suite_name))
174 release = daklib.import_repository.obtain_release(base, suite, keyring)
175 target_sources, target_binaries = get_packages_in_suite(target_suite)
177 if 'Architectures' in options:
178 architectures = options['Architectures'].split(',')
180 architectures = ['all'] + release.architectures()
182 if 'Components' in options:
183 components = options['Components'].split(',')
185 components = release.components()
187 # TODO: Clean this up...
191 # For Extra-Source-Only sources packages, keep a dict
192 # (name, version) -> entry and (name, version) -> set of components
193 # to allow importing needed packages at a later stage
194 extra_sources = dict()
195 extra_sources_comp = defaultdict(set)
198 component = daklib.dbconn.get_component(c, transaction.session)
199 print("Processing {0}/source...".format(c))
200 sources = release.sources(c)
201 imported = import_sources(base, sources, transaction, target_suite, component, target_sources, extra_sources, extra_sources_comp, max_packages)
202 print(" imported {0} source packages".format(imported))
204 if max_packages is not None:
208 component = daklib.dbconn.get_component(c, transaction.session)
209 for architecture in architectures:
210 print("Processing {0}/{1}...".format(c, architecture))
211 packages = release.packages(c, architecture)
212 imported = import_packages(base, packages, transaction, target_suite, component, architecture, target_binaries, extra_sources, extra_sources_comp, max_packages)
213 print(" imported {0} binary packages".format(imported))
215 if max_packages is not None:
218 transaction.rollback()
220 if __name__ == '__main__':