~brian-murray/ubuntu-archive-tools/identify-security

753 by Stéphane Graber
Add the packageset report
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
# Copyright (C) 2013 Canonical Ltd.
5
# Author: Stéphane Graber <stgraber@ubuntu.com>
6
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; version 3 of the License.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
754 by Stéphane Graber
Re-order imports
19
import argparse
753 by Stéphane Graber
Add the packageset report
20
import os
21
import time
22
23
from launchpadlib.launchpad import Launchpad
24
from codecs import open
25
26
parser = argparse.ArgumentParser(
27
    description="Generate list of packages and uploaders for all packagesets.")
28
parser.add_argument("target", metavar="TARGET",
29
                    help="Target directory")
30
parser.add_argument("-a", "--all", action="store_true",
31
                    help="Sync all series instead of just the active ones")
32
args = parser.parse_args()
33
34
# Authenticated login to Launchpad as anonymous
35
# doesn't let us list the uploaders
36
lp = Launchpad.login_with('package_sets_report', 'production')
37
38
ubuntu = lp.distributions['ubuntu']
39
40
# Get the list of series
41
if args.all:
42
    ubuntu_series = [series for series in ubuntu.series
43
                     if series.status != "Future"]
44
else:
45
    ubuntu_series = [series for series in ubuntu.series if series.active]
46
47
# cache
48
teams = {}
49
50
for series in ubuntu_series:
51
    series_name = str(series.name)
52
53
    if not os.path.exists(os.path.join(args.target, series_name)):
54
        os.makedirs(os.path.join(args.target, series_name))
55
56
    for pkgset in lp.packagesets.getBySeries(distroseries=series):
57
        report = ""
58
        report += "Name: %s\n" % pkgset.name
59
        report += "Description: %s\n" % pkgset.description
60
        report += "Owner: %s\n" % pkgset.owner.display_name
61
        report += "Creation date: %s\n" % pkgset.date_created
62
63
        # List all the source packages
64
        report += "\nPackages:\n"
65
        for pkg in sorted(list(pkgset.getSourcesIncluded())):
66
            report += " - %s\n" % str(pkg)
67
68
        # List all the sub-package sets
69
        report += "\nSub-package sets:\n"
70
        for child in sorted(list(pkgset.setsIncluded(direct_inclusion=True))):
71
            report += " - %s\n" % child.name
72
73
        # List all the uploaders, when it's a team, show the members count
74
        report += "\nUploaders:\n"
75
        for archive in ubuntu.archives:
76
            for uploader in sorted(list(archive.getUploadersForPackageset(
77
                    packageset=pkgset)),
78
                    key=lambda uploader: uploader.person.display_name):
79
80
                if uploader.person.is_team:
81
                    if not uploader.person.name in teams:
82
                        team = uploader.person
83
                        teams[uploader.person.name] = team
84
                    else:
85
                        team = teams[uploader.person.name]
86
87
                    report += " - %s (%s) (%s) (%s) (%s members)\n" % \
88
                              (team.display_name,
89
                               team.name,
90
                               uploader.permission,
91
                               archive.displayname,
92
                               len(team.members))
93
                    for member in sorted(list(team.members),
94
                                         key=lambda person: person.name):
95
                        report += "   - %s (%s)\n" % (member.display_name,
96
                                                      member.name)
97
                else:
98
                    report += " - %s (%s) (%s)\n" % \
99
                              (uploader.person.name,
100
                               uploader.person.display_name,
101
                               uploader.permission)
102
103
        report += "\nGenerated at: %s\n" % time.asctime()
104
        with open(os.path.join(args.target, series_name, pkgset.name),
105
                  "w+", encoding="utf-8") as fd:
106
            fd.write(report)