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 http://httpredir.debian.org/debian unstable
43 Things to think about:
44 - Import Built-Using sources
45 - all / only referenced
46 - Remove old packages:
47 - by-source: remove source X_v, if no X exists upstream
48 - by-version: remove source X_v, if no X_v exists upstream
49 (X denotes package name, v version, X_v package at a specific version)
50 - Import all or only newest?
51 - Expire binary packages?
55 def entry_is_newer(entry, packages):
56 version = entry['Version']
57 for p in packages[entry['Package']]:
58 if apt_pkg.version_compare(version, p.version) <= 0:
62 def entry_in_packages(entry, packages):
63 return entry['Package'] in packages
65 def get_packages_in_suite(suite):
66 sources = defaultdict(list)
67 for s in suite.sources:
68 sources[s.source].append(s)
70 packages = defaultdict(list)
71 for b in suite.binaries:
72 packages[b.package].append(b)
74 return sources, packages
81 ('h', 'help', 'Import-Repository::Help'),
82 ('k', 'keyring', 'Import-Repository::Keyring', 'HasArg'),
83 ('K', 'key', 'Import-Repository::Key', 'HasArg'),
84 ('a', 'architectures', 'Import-Repository::Architectures', 'HasArg'),
85 ('c', 'components', 'Import-Repository::Components', 'HasArg'),
86 ('t', 'target-suite', 'Import-Repository::Target-Suite', 'HasArg'),
87 ('A', 'add-overrides', 'Import-Repository::AddOverrides'),
90 cnf = daklib.config.Config();
91 argv = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
92 options = cnf.subtree('Import-Repository')
94 if 'Help' in options or len(argv) < 2:
97 keyring = options.find('Keyring') or None
99 print("Error: No keyring specified")
103 raise Exception('Not implemented.')
105 if 'AddOverrides' in options:
106 raise Exception('Not implemented.')
108 base, suite = argv[0:2]
110 target_suite_name = options.find('Target-Suite') or suite
112 print("Importing packages from {0}/dists/{1} to {2}".format(base, suite, target_suite_name))
113 with daklib.archive.ArchiveTransaction() as transaction:
114 target_suite = daklib.dbconn.get_suite(target_suite_name, transaction.session)
115 if target_suite is None:
116 daklib.utils.fubar("Target suite '{0}' is unknown.".format(target_suite_name))
118 release = daklib.import_repository.obtain_release(base, suite, keyring)
119 target_sources, target_binaries = get_packages_in_suite(target_suite)
121 if 'Architectures' in options:
122 architectures = options['Architectures'].split(',')
124 architectures = ['all'] + release.architectures()
126 if 'Components' in options:
127 components = options['Components'].split(',')
129 components = release.components()
131 # TODO: Clean this up...
133 extra_sources = dict()
134 extra_sources_comp = defaultdict(set)
136 component = daklib.dbconn.get_component(c, transaction.session)
137 print("Processing {0}/source...".format(c))
138 sources = release.sources(c)
139 for entry in sources:
140 if entry.get('Extra-Source-Only', 'no') == 'yes':
141 # Remember package, we might need to import it later.
142 key = (entry['Package'], entry['Version'])
143 extra_sources[key] = entry
144 extra_sources_comp[key].add(c)
146 if not entry_in_packages(entry, target_sources) or entry_is_newer(entry, target_sources):
147 print("Importing {0}={1}".format(entry['Package'], entry['Version']))
148 daklib.import_repository.import_source_to_suite(base, entry, transaction, target_suite, component)
149 #transaction.commit()
152 component = daklib.dbconn.get_component(c, transaction.session)
153 for architecture in architectures:
154 print("Processing {0}/{1}...".format(c, architecture))
155 packages = release.packages(c, architecture)
156 for entry in packages:
157 if not entry_in_packages(entry, target_binaries) or entry_is_newer(entry, target_binaries):
158 print("Importing {0}={1} ({2})".format(entry['Package'], entry['Version'], architecture))
159 # Import Built-Using sources:
160 for bu_source, bu_version in daklib.utils.parse_built_using(entry):
161 if not daklib.import_repository.source_in_archive(bu_source, bu_version, target_suite.archive):
162 print("Importing extra source {0}={1}".format(bu_source, bu_version))
163 key = (bu_source, bu_version)
164 extra_entry = extra_sources.get(key)
165 if extra_entry is None:
166 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))
167 extra_components = extra_sources_comp[key]
169 extra_component = component
171 # TODO: Take preferred components from those listed...
172 raise Exception("Not implemented.")
173 # e.g. a contrib binary package Built-Using a main source
174 daklib.import_repository.import_source_to_suite(base, extra_entry, transaction, target_suite, extra_component)
176 daklib.import_repository.import_package_to_suite(base, entry, transaction, target_suite, component)
177 #transaction.commit()
179 transaction.rollback()
181 if __name__ == '__main__':