]> git.decadent.org.uk Git - dak.git/blob - dak/import_repository.py
Merge remote-tracking branch 'jcristau/cs-set-log-suite'
[dak.git] / dak / import_repository.py
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
19 from __future__ import print_function
20
21 import daklib.archive
22 import daklib.config
23 import daklib.dbconn
24 import daklib.import_repository
25 import daklib.utils
26
27 import apt_pkg
28 import sys
29
30 from collections import defaultdict
31
32 def usage(status=0):
33     print("""
34 dak import-repository
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)]
40   [--add-overrides]
41   [--max-packages=${n} (import at maximum ${n} packages, default: no limit)]
42   http://httpredir.debian.org/debian unstable
43
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?
53 """)
54     sys.exit(status)
55
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:
60             return False
61     return True
62
63 def entry_in_packages(entry, packages):
64     return entry['Package'] in packages
65
66 def get_packages_in_suite(suite):
67     sources = defaultdict(list)
68     for s in suite.sources:
69         sources[s.source].append(s)
70
71     packages = defaultdict(list)
72     for b in suite.binaries:
73         packages[b.package].append(b)
74
75     return sources, packages
76
77 def import_sources(base, sources, transaction, target_suite, component, target_sources, extra_sources, extra_sources_comp, max_packages=None):
78     n = 0
79     for entry in sources:
80         if max_packages is not None and n > max_packages:
81             break
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)
87             continue
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)
91             n += 1
92             #transaction.commit()
93     return n
94
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]
103         if c in components:
104             extra_component = component
105         else:
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)
109
110 def import_packages(base, packages, transaction, target_suite, component, architecture, target_binaries, extra_sources, extra_sources_comp, max_packages=None):
111     n = 0
112     for entry in packages:
113         if max_packages is not None and n > max_packages:
114             break
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)
120             # Import binary:
121             daklib.import_repository.import_package_to_suite(base, entry, transaction, target_suite, component)
122             n += 1
123             #transaction.commit()
124     return n
125
126 def main(argv=None):
127     if argv is None:
128         argv = sys.argv
129
130     arguments = [
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'),
139         ]
140
141     cnf = daklib.config.Config();
142     argv = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
143     options = cnf.subtree('Import-Repository')
144
145     if 'Help' in options or len(argv) < 2:
146         usage(0)
147
148     keyring = options.find('Keyring') or None
149     if keyring is None:
150         print("Error: No keyring specified")
151         print()
152
153     if 'Key' in options:
154         raise Exception('Not implemented.')
155
156     if 'AddOverrides' in options:
157         raise Exception('Not implemented.')
158
159     if 'MaxPackages' in options:
160         max_packages = long(options['MaxPackages'])
161     else:
162         max_packages = None
163
164     base, suite = argv[0:2]
165
166     target_suite_name = options.find('Target-Suite') or suite
167
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))
173
174         release = daklib.import_repository.obtain_release(base, suite, keyring)
175         target_sources, target_binaries = get_packages_in_suite(target_suite)
176
177         if 'Architectures' in options:
178             architectures = options['Architectures'].split(',')
179         else:
180             architectures = ['all'] + release.architectures()
181
182         if 'Components' in options:
183             components = options['Components'].split(',')
184         else:
185             components = release.components()
186
187         # TODO: Clean this up...
188
189         n = 0
190
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)
196
197         for c in components:
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))
203             n += imported
204             if max_packages is not None:
205                 max_packages -= n
206
207         for c in components:
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))
214                 n += imported
215                 if max_packages is not None:
216                     max_packages -= n
217
218         transaction.rollback()
219
220 if __name__ == '__main__':
221     main()