~ubuntu-archive/ubuntu-archive-scripts/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/env python3
# -*- coding: utf-8 -*-

# Copyright (C) 2018 Canonical Ltd

# 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.
#
# A copy of the GNU General Public License version 2 is in LICENSE.

import apt_pkg
import argparse
import atexit
from collections import defaultdict
from datetime import datetime
import gzip
import json
import os
import re
import shutil
import tempfile
from textwrap import dedent
from urllib.request import urlopen

import attr
from jinja2 import Environment, FileSystemLoader
import yaml

env = Environment(
    loader=FileSystemLoader(os.path.dirname(os.path.abspath(__file__)) + '/templates'),
    autoescape=True,
    extensions=['jinja2.ext.i18n'],
)
env.install_null_translations(True)

tempdir = None


# copied from component-mismatches
def ensure_tempdir():
    global tempdir
    if not tempdir:
        tempdir = tempfile.mkdtemp(prefix='component-mismatches')
        atexit.register(shutil.rmtree, tempdir)


# copied from component-mismatches
def decompress_open(tagfile):
    ensure_tempdir()
    decompressed = tempfile.mktemp(dir=tempdir)
    fin = gzip.GzipFile(filename=tagfile)
    with open(decompressed, 'wb') as fout:
        fout.write(fin.read())
    return open(decompressed, 'r')


# copied from generate-team-p-m
def get_subscribers_json(packages, subscribers_json):
    if subscribers_json is None:
        j = urlopen("https://ubuntu-archive-team.ubuntu.com/package-team-mapping.json")
    else:
        j = open(subscribers_json, 'rb')
    with j:
        team_to_packages = json.loads(j.read().decode('utf-8'))
    package_to_teams = {}
    for team, team_packages in team_to_packages.items():
        for package in team_packages:
            if package in packages:
                package_to_teams.setdefault(package, []).append(team)
    return package_to_teams


def packages_with_bzr_repos(options):
    pkgs = {}
    for component in options.components.split(','):
        sources_path = "%s/dists/%s/%s/source/Sources.gz" % (
            options.archive_dir, options.suite, component)
        for section in apt_pkg.TagFile(decompress_open(sources_path)):
            if 'Package' in section and 'Vcs-Bzr' in section:
                pkgs[section['Package']] = section['Vcs-Bzr']
    return pkgs


def repo_url(repo):
    return re.sub('^lp:', 'https://code.launchpad.net/', repo)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--archive-dir', action='store', default=os.path.expanduser("~/mirror/ubuntu"))
    parser.add_argument('--components', action='store', default="main,restricted,universe,multiverse")
    parser.add_argument('--suite', action='store')
    parser.add_argument('--subscribers-json', action='store')
    parser.add_argument('output')
    args = parser.parse_args()

    pkgs = packages_with_bzr_repos(args)

    print("getting subscribers")
    subscribers = get_subscribers_json(set(pkgs.keys()), args.subscribers_json)
    for p in set(pkgs.keys()):
        if p not in subscribers:
            subscribers[p] = ['unknown']

    all_teams = set()
    team_to_pkgs = defaultdict(list)
    for package, teams in subscribers.items():
        all_teams |= set(teams)
        for team in teams:
            team_to_pkgs[team].append(package)

    team_to_attn_count = {}
    for team, packages in team_to_pkgs.items():
        team_to_attn_count[team] = len(packages)
        team_to_pkgs[team] = [(package, repo_url(pkgs[package])) for package in sorted(packages)]

    print("rendering")
    t = env.get_template('team-report-bzr.html')
    now = datetime.utcnow()
    with open(args.output, 'w', encoding='utf-8') as fp:
        fp.write(t.render(
            all_teams=all_teams,
            now="%s UTC" % now.strftime("%Y-%m-%d %H:%M:%S"),
            team_to_pkgs=team_to_pkgs,
            team_to_attn_count=team_to_attn_count))


if __name__ == '__main__':
    main()