~laney/ubuntu-archive-tools/cm-show-fix-released

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#! /usr/bin/python

from __future__ import print_function

import atexit
import bz2
from collections import defaultdict
import json
import lzma
from optparse import OptionParser
import requests
import shutil
import sys
import tempfile

import apt_pkg
from launchpadlib.launchpad import Launchpad

import lputils


tempdir = None


def ensure_tempdir():
    global tempdir
    if not tempdir:
        tempdir = tempfile.mkdtemp(prefix="unsubscribed-packages")
        atexit.register(shutil.rmtree, tempdir)


def decompress_open(tagfile):
    if tagfile.startswith("http:") or tagfile.startswith("ftp:"):
        url = tagfile
        tagfile = requests.get(url)
        if tagfile.status_code == 404:
            url = url.replace(".xz", ".bz2")
            tagfile = requests.get(url)

    ensure_tempdir()
    decompressed = tempfile.mktemp(dir=tempdir)
    with open(decompressed, "wb") as fout:
        if url.endswith(".xz"):
            fout.write(lzma.decompress(tagfile.content))
        elif url.endswith(".bz2"):
            fout.write(bz2.decompress(tagfile.content))
    return open(decompressed, "r")


def archive_base(archtag):
    if archtag in ("amd64", "i386", "src"):
        return "http://archive.ubuntu.com/ubuntu"
    else:
        return "http://ports.ubuntu.com/ubuntu-ports"


def source_names(options):
    sources = dict()
    for suite in options.suites:
        for component in ["main", "restricted"]:
            url = "%s/dists/%s/%s/source/Sources.xz" % (
                archive_base("src"), suite, component)
            if not options.quiet:
                print("Reading %s ..." % url, file=sys.stderr)
            for section in apt_pkg.TagFile(decompress_open(url)):
                pkg = section["Package"]
                if suite == options.dev_suite:
                    sources[pkg] = True
                else:
                    if sources.get(pkg, False) == True:
                        continue
                    sources[pkg] = False
    return sources


def main():
    parser = OptionParser(
        description="Check for source packages in main or restricted in "
                    "active distro series and return a json file of the teams "
                    "to which they map.")
    parser.add_option(
        "-l", "--launchpad", dest="launchpad_instance", default="production")
    parser.add_option(
        "-u", "--unsubscribed", action="store_true", default=False,
        help="Only return packages which have no subscriber")
    parser.add_option(
        "-p", "--print", action="store_true", default=False,
        dest="display",
        help="Print results to screen instead of a json file")
    parser.add_option(
        "-o", "--output-file", default="package-team-mapping.json",
        help="output JSON to this file")
    parser.add_option(
        "-q", "--quiet", action="store_true", default=False,
        help="Quieten progress messages")
    options, _ = parser.parse_args()
    options.suite = None
    options.distribution = "ubuntu"
    options.launchpad = Launchpad.login_with(
        "unsubscribed-packages", options.launchpad_instance)
    launchpad = options.launchpad
    ubuntu = launchpad.distributions[options.distribution]
    options.suites = []
    for series in ubuntu.series:
        # very few lucid packages are supported
        if series.name == 'lucid':
            continue
        if series.active:
            options.suites.append(series.name)
        # find the dev series
        if series.status in ['Active Development', 'Pre-release Freeze']:
            options.dev_suite = series.name

    lputils.setup_location(options)

    team_names = [
        'checkbox-bugs',
        'desktop-packages',
        'documentation-packages',
        'dx-packages',
        'edubuntu-bugs',
        'foundations-bugs',
        'kernel-packages',
        'kubuntu-bugs',
        'libertine-team',
        'lubuntu-packaging',
        'maas-maintainers',
        'mir-team',
        'phablet-team',
        'pkg-ime',
        'snappy-dev',
        'system-apps-team',
        'translators-packages',
        'ubuntu-apps-bugs',
        'ubuntu-openstack',
        'ubuntu-phonedations-bugs',
        'ubuntu-printing',
        'ubuntu-sdk-bugs',
        'ubuntu-security-bugs',
        'ubuntu-server',
        'ubuntu-webapps-bugs',
        'ubuntuone-hackers',
        'unity-api-team',
        'unity-ui-team',
        'xubuntu-bugs',
        ]

    data = { "unsubscribed": [] }
    subscriptions = defaultdict(list)
    for team_name in team_names:
        data[team_name] = []
        team = launchpad.people[team_name]
        team_subs = team.getBugSubscriberPackages()
        for src_pkg in team_subs:
            subscriptions[src_pkg.name].append(team_name)
            data[team_name].append(src_pkg.name)

    source_packages = source_names(options)
    for source_package in sorted(source_packages):
        # we only care about ones people are not subscribed to in the dev release
        if source_package not in subscriptions and source_packages[source_package]:
            data["unsubscribed"].append(source_package)
            if options.display:
                print("No team is subscribed to: %s" %
                      source_package)
        else:
            if not options.unsubscribed:
                if options.display:
                    print("%s is subscribed to: %s" %
                          (team_name, source_package))

    if not options.display:
        with open(options.output_file, 'w') as json_file:
            json_file.write(json.dumps(data, indent=4))


if __name__ == '__main__':
    main()