]> git.decadent.org.uk Git - dak.git/blob - dak/import_repository.py
5522be0fa5acdd013144d53bc9bbd00a70b3b6aa
[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   http://httpredir.debian.org/debian unstable
42
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?
52 """)
53     sys.exit(status)
54
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:
59             return False
60     return True
61
62 def entry_in_packages(entry, packages):
63     return entry['Package'] in packages
64
65 def get_packages_in_suite(suite):
66     sources = defaultdict(list)
67     for s in suite.sources:
68         sources[s.source].append(s)
69
70     packages = defaultdict(list)
71     for b in suite.binaries:
72         packages[b.package].append(b)
73
74     return sources, packages
75
76 def main(argv=None):
77     if argv is None:
78         argv = sys.argv
79
80     arguments = [
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'),
88         ]
89
90     cnf = daklib.config.Config();
91     argv = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
92     options = cnf.subtree('Import-Repository')
93
94     if 'Help' in options or len(argv) < 2:
95         usage(0)
96
97     keyring = options.find('Keyring') or None
98     if keyring is None:
99         print("Error: No keyring specified")
100         print()
101
102     if 'Key' in options:
103         raise Exception('Not implemented.')
104
105     if 'AddOverrides' in options:
106         raise Exception('Not implemented.')
107
108     base, suite = argv[0:2]
109
110     target_suite_name = options.find('Target-Suite') or suite
111
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))
117
118         release = daklib.import_repository.obtain_release(base, suite, keyring)
119         target_sources, target_binaries = get_packages_in_suite(target_suite)
120
121         if 'Architectures' in options:
122             architectures = options['Architectures'].split(',')
123         else:
124             architectures = ['all'] + release.architectures()
125
126         if 'Components' in options:
127             components = options['Components'].split(',')
128         else:
129             components = release.components()
130
131         # TODO: Clean this up...
132
133         extra_sources = dict()
134         extra_sources_comp = defaultdict(set)
135         for c in components:
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)
145                     continue
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()
150
151         for c in components:
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]
168                                 if c in components:
169                                     extra_component = component
170                                 else:
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)
175                         # Import binary:
176                         daklib.import_repository.import_package_to_suite(base, entry, transaction, target_suite, component)
177                         #transaction.commit()
178
179         transaction.rollback()
180
181 if __name__ == '__main__':
182     main()