import_repository.py 8.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#! /usr/bin/env python
#
# Copyright (C) 2015, Ansgar Burchardt <ansgar@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from __future__ import print_function

import daklib.archive
import daklib.config
import daklib.dbconn
import daklib.import_repository
import daklib.utils

import apt_pkg
import sys

from collections import defaultdict

32

33 34 35 36 37 38 39 40 41
def usage(status=0):
    print("""
dak import-repository
  --keyring=/usr/share/keyring/debian-archive-keyring.gpg
  [--key=${fingerprint}]
  [--architectures=a,b,c (default: architectures in origin suite)]
  [--components=main,contrib (default: components in origin suite)]
  [--target-suite=${suite} (default: origin suite name)]
  [--add-overrides]
Ansgar's avatar
Ansgar committed
42
  [--max-packages=${n} (import at maximum ${n} packages, default: no limit)]
43 44 45 46 47 48 49 50 51 52 53 54 55 56
  http://httpredir.debian.org/debian unstable

Things to think about:
 - Import Built-Using sources
   - all / only referenced
 - Remove old packages:
   - by-source: remove source X_v, if no X exists upstream
   - by-version: remove source X_v, if no X_v exists upstream
   (X denotes package name, v version, X_v package at a specific version)
 - Import all or only newest?
 - Expire binary packages?
""")
    sys.exit(status)

57

58 59 60 61 62 63 64
def entry_is_newer(entry, packages):
    version = entry['Version']
    for p in packages[entry['Package']]:
        if apt_pkg.version_compare(version, p.version) <= 0:
            return False
    return True

65

66 67 68
def entry_in_packages(entry, packages):
    return entry['Package'] in packages

69

70 71 72 73 74 75 76 77 78 79 80
def get_packages_in_suite(suite):
    sources = defaultdict(list)
    for s in suite.sources:
        sources[s.source].append(s)

    packages = defaultdict(list)
    for b in suite.binaries:
        packages[b.package].append(b)

    return sources, packages

81

Ansgar's avatar
Ansgar committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
def import_sources(base, sources, transaction, target_suite, component, target_sources, extra_sources, extra_sources_comp, max_packages=None):
    n = 0
    for entry in sources:
        if max_packages is not None and n > max_packages:
            break
        if entry.get('Extra-Source-Only', 'no') == 'yes':
            # Remember package, we might need to import it later.
            key = (entry['Package'], entry['Version'])
            extra_sources[key] = entry
            extra_sources_comp[key].add(c)
            continue
        if not entry_in_packages(entry, target_sources) or entry_is_newer(entry, target_sources):
            print("Importing {0}={1}".format(entry['Package'], entry['Version']))
            daklib.import_repository.import_source_to_suite(base, entry, transaction, target_suite, component)
            n += 1
            #transaction.commit()
    return n

100

Ansgar's avatar
Ansgar committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
def import_built_using(base, source, version, transaction, target_suite, component, extra_sources, extra_sources_comp):
    if not daklib.import_repository.source_in_archive(bu_source, bu_version, target_suite.archive):
        print("Importing extra source {0}={1}".format(bu_source, bu_version))
        key = (bu_source, bu_version)
        extra_entry = extra_sources.get(key)
        if extra_entry is None:
            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))
        extra_components = extra_sources_comp[key]
        if c in components:
            extra_component = component
        else:
            # TODO: Take preferred components from those listed...
            raise Exception("Not implemented.")
        daklib.import_repository.import_source_to_suite(base, extra_entry, transaction, target_suite, extra_component)

116

Ansgar's avatar
Ansgar committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
def import_packages(base, packages, transaction, target_suite, component, architecture, target_binaries, extra_sources, extra_sources_comp, max_packages=None):
    n = 0
    for entry in packages:
        if max_packages is not None and n > max_packages:
            break
        if not entry_in_packages(entry, target_binaries) or entry_is_newer(entry, target_binaries):
            print("Importing {0}={1} ({2})".format(entry['Package'], entry['Version'], architecture))
            # Import Built-Using sources:
            for bu_source, bu_version in daklib.utils.parse_built_using(entry):
                import_built_using(base, bu_source, bu_version, transaction, target_suite, component, extra_sources, extra_sources_comp)
            # Import binary:
            daklib.import_repository.import_package_to_suite(base, entry, transaction, target_suite, component)
            n += 1
            #transaction.commit()
    return n

133

134 135 136 137 138 139 140 141 142 143 144 145
def main(argv=None):
    if argv is None:
        argv = sys.argv

    arguments = [
        ('h', 'help', 'Import-Repository::Help'),
        ('k', 'keyring', 'Import-Repository::Keyring', 'HasArg'),
        ('K', 'key', 'Import-Repository::Key', 'HasArg'),
        ('a', 'architectures', 'Import-Repository::Architectures', 'HasArg'),
        ('c', 'components', 'Import-Repository::Components', 'HasArg'),
        ('t', 'target-suite', 'Import-Repository::Target-Suite', 'HasArg'),
        ('A', 'add-overrides', 'Import-Repository::AddOverrides'),
Ansgar's avatar
Ansgar committed
146
        ('n', 'max-packages', 'Import-Repository::MaxPackages', 'HasArg'),
147 148
        ]

149
    cnf = daklib.config.Config()
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    argv = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
    options = cnf.subtree('Import-Repository')

    if 'Help' in options or len(argv) < 2:
        usage(0)

    keyring = options.find('Keyring') or None
    if keyring is None:
        print("Error: No keyring specified")
        print()

    if 'Key' in options:
        raise Exception('Not implemented.')

    if 'AddOverrides' in options:
        raise Exception('Not implemented.')

Ansgar's avatar
Ansgar committed
167 168 169 170 171
    if 'MaxPackages' in options:
        max_packages = long(options['MaxPackages'])
    else:
        max_packages = None

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    base, suite = argv[0:2]

    target_suite_name = options.find('Target-Suite') or suite

    print("Importing packages from {0}/dists/{1} to {2}".format(base, suite, target_suite_name))
    with daklib.archive.ArchiveTransaction() as transaction:
        target_suite = daklib.dbconn.get_suite(target_suite_name, transaction.session)
        if target_suite is None:
            daklib.utils.fubar("Target suite '{0}' is unknown.".format(target_suite_name))

        release = daklib.import_repository.obtain_release(base, suite, keyring)
        target_sources, target_binaries = get_packages_in_suite(target_suite)

        if 'Architectures' in options:
            architectures = options['Architectures'].split(',')
        else:
            architectures = ['all'] + release.architectures()

        if 'Components' in options:
            components = options['Components'].split(',')
        else:
            components = release.components()

        # TODO: Clean this up...

Ansgar's avatar
Ansgar committed
197 198 199 200 201
        n = 0

        # For Extra-Source-Only sources packages, keep a dict
        # (name, version) -> entry and (name, version) -> set of components
        # to allow importing needed packages at a later stage
202 203
        extra_sources = dict()
        extra_sources_comp = defaultdict(set)
Ansgar's avatar
Ansgar committed
204

205 206 207 208
        for c in components:
            component = daklib.dbconn.get_component(c, transaction.session)
            print("Processing {0}/source...".format(c))
            sources = release.sources(c)
Ansgar's avatar
Ansgar committed
209 210 211 212 213
            imported = import_sources(base, sources, transaction, target_suite, component, target_sources, extra_sources, extra_sources_comp, max_packages)
            print("  imported {0} source packages".format(imported))
            n += imported
            if max_packages is not None:
                max_packages -= n
214 215 216 217 218 219

        for c in components:
            component = daklib.dbconn.get_component(c, transaction.session)
            for architecture in architectures:
                print("Processing {0}/{1}...".format(c, architecture))
                packages = release.packages(c, architecture)
Ansgar's avatar
Ansgar committed
220 221 222 223 224
                imported = import_packages(base, packages, transaction, target_suite, component, architecture, target_binaries, extra_sources, extra_sources_comp, max_packages)
                print("  imported {0} binary packages".format(imported))
                n += imported
                if max_packages is not None:
                    max_packages -= n
225 226 227

        transaction.rollback()

228

229 230
if __name__ == '__main__':
    main()