~ubuntu-archive/ubuntu-archive-tools/trunk

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python3
# -*- coding: utf-8 -*-

# Copyright (C) 2013 Canonical Ltd.
# Author: Iain Lane <iain.lane@canonical.com>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
# USA

import argparse
import apt_pkg
import gzip
import io
import json
import logging
import os
import re
import urllib.request
import sys

PARSED_SEEDS_URL = \
    'http://qa.ubuntuwire.org/ubuntu-seeded-packages/seeded.json.gz'
LOGGER = logging.getLogger(os.path.basename(sys.argv[0]))


class GetPackage():
    apt_cache_initialised = False

    def __init__(self):
        # Initialise python-apt
        if not GetPackage.apt_cache_initialised:
            apt_pkg.init()
            GetPackage.apt_cache_initialised = True

        self.cache = apt_pkg.Cache(None)
        self.pkgrecords = apt_pkg.PackageRecords(self.cache)
        self.depcache = apt_pkg.DepCache(self.cache)

        # Download & parse the seeds
        response = urllib.request.urlopen(PARSED_SEEDS_URL)

        buf = io.BytesIO(response.read())
        f = gzip.GzipFile(fileobj=buf)
        data = f.read().decode('utf-8')
        self.seeded_packages = json.loads(data)

    def getsourcepackage(self, pkg):
        pkg = re.sub(':.*', '', pkg)
        try:
            candidate = self.depcache.get_candidate_ver(self.cache[pkg])
        except KeyError:  # no package found (arch specific?)
            return
        try:
            self.pkgrecords.lookup(candidate.file_list[0])
        except AttributeError:  # no source (pure virtual?)
            return
        return self.pkgrecords.source_pkg or pkg


def main():
    parser = argparse.ArgumentParser(description='Generate a freeze block for'
                                     + ' an Ubuntu milestone')
    parser.add_argument('flavours', nargs='+',
                        help='The participating flavours')
    parser.add_argument('--only-unique', '-u', action='store_true',
                        help='Block only packages unique to FLAVOURS')
    parser.add_argument('--debug', '-d', action='store_true',
                        help='Output some extra debugging')
    parser.add_argument('--not-unique','-n', action='store_true',
                        help='Block only packages also seeded outside of FLAVOURS')
    args = parser.parse_args()

    logging.basicConfig(stream=sys.stderr,
                        level=(logging.DEBUG if args.debug
                               else logging.WARNING))

    if args.not_unique and args.only_unique:
        LOGGER.error("Cannot specify --not-unique and --only-unique at the same time.")
        sys.exit(1)

    packages = GetPackage()

    output = set()
    skip = set()

    flavours = set(args.flavours)

    # binary package: [ [ product, seed ] ]
    # e.g. "gbrainy": [["edubuntu", "dvd"]]
    for k, v in packages.seeded_packages.items():
        source_pkg = packages.getsourcepackage(k)
        seeding_flavours = set([x[0] for x in v if x[1] != "supported"])

        if source_pkg and source_pkg in skip:
            continue

        # If you don't get to freeze others' packages
        if args.only_unique or args.not_unique:
            not_releasing_seeding_flavours = seeding_flavours - flavours
        else:
            not_releasing_seeding_flavours = None

        if not_releasing_seeding_flavours and args.only_unique:
            LOGGER.debug(("Skipping %s (%s binary package) because it's"
                         + " seeded on %s") % (source_pkg, k, v))
            output.discard(source_pkg)
            skip.add(source_pkg)
            continue
        elif source_pkg and flavours < seeding_flavours and not_releasing_seeding_flavours and args.not_unique:
            LOGGER.debug("Adding %s (%s binary package) due to %s (non-unique)"
                         % (source_pkg, k, v))
            output.add(source_pkg)
            continue

        if not args.not_unique and seeding_flavours.intersection(flavours) and source_pkg:
            LOGGER.debug("Adding %s (%s binary package) due to %s"
                         % (source_pkg, k, v))
            output.add(source_pkg)

    print ("block", " ".join(sorted(output)))


if __name__ == "__main__":
    main()