~unit193/ubuntu-dev-tools/update-deb-mirror

1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
1
#!/usr/bin/python
2
#
3
# Copyright (C) 2011, Stefano Rivera <stefanor@ubuntu.com>
4
#
5
# Permission to use, copy, modify, and/or distribute this software for any
6
# purpose with or without fee is hereby granted, provided that the above
7
# copyright notice and this permission notice appear in all copies.
8
#
9
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17
import optparse
1245.1.4 by Stefano Rivera
Rename tool, and make output less verbose by default
18
import sys
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
19
20
from ubuntutools.lp.lpapicache import (Launchpad, Distribution, PersonTeam,
1245.1.7 by Stefano Rivera
Catch PackageNotFoundException
21
                                       Packageset, PackageNotFoundException,
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
22
                                       SeriesNotFoundException)
1383 by Benjamin Drung
Move devscripts.logger to ubuntutools.logger.
23
from ubuntutools.logger import Logger
1245.1.8 by Stefano Rivera
Pocket awareness
24
from ubuntutools.misc import split_release_pocket
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
25
26
1245.1.6 by Stefano Rivera
Break up main()
27
def parse_arguments():
28
    '''Parse arguments and return (options, package)'''
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
29
    parser = optparse.OptionParser('%prog [options] package')
30
    parser.add_option('-r', '--release', default=None, metavar='RELEASE',
31
                      help='Use RELEASE, rather than the current development '
32
                           'release')
1245.1.4 by Stefano Rivera
Rename tool, and make output less verbose by default
33
    parser.add_option('-a', '--list-uploaders',
34
                      default=False, action='store_true',
35
                      help='List all the people/teams with upload rights')
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
36
    parser.add_option('-t', '--list-team-members',
37
                      default=False, action='store_true',
1245.1.4 by Stefano Rivera
Rename tool, and make output less verbose by default
38
                      help='List all team members of teams with upload rights '
39
                           '(implies --list-uploaders)')
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
40
    options, args = parser.parse_args()
41
42
    if len(args) != 1:
43
        parser.error("One (and only one) package must be specified")
44
    package = args[0]
45
1245.1.4 by Stefano Rivera
Rename tool, and make output less verbose by default
46
    if options.list_team_members:
47
        options.list_uploaders = True
48
1245.1.6 by Stefano Rivera
Break up main()
49
    return (options, package)
50
51
52
def main():
53
    '''Query upload permissions'''
54
    options, package = parse_arguments()
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
55
    # Need to be logged in to see uploaders:
56
    Launchpad.login()
57
58
    ubuntu = Distribution('ubuntu')
59
    archive = ubuntu.getArchive()
60
    if options.release is None:
1245.1.11 by Stefano Rivera
Don't list None as a release
61
        options.release = ubuntu.getDevelopmentSeries().name
62
    try:
63
        release, pocket = split_release_pocket(options.release)
64
        series = ubuntu.getSeries(release)
65
    except SeriesNotFoundException, e:
66
        Logger.error(str(e))
67
        sys.exit(2)
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
68
1245.1.7 by Stefano Rivera
Catch PackageNotFoundException
69
    try:
70
        spph = archive.getSourcePackage(package)
71
    except PackageNotFoundException, e:
72
        Logger.error(str(e))
73
        sys.exit(2)
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
74
    component = spph.getComponent()
1245.1.13 by Stefano Rivera
non-release pockets are ok for non-dev releases
75
    if (options.list_uploaders and (pocket != 'Release' or series.status in
76
            ('Experimental', 'Active Development', 'Pre-release Freeze'))):
1245.1.12 by Stefano Rivera
Actually, skip all upload rights when the release is non-dev
77
78
        component_uploader = archive.getUploadersForComponent(
79
                component_name=component)[0]
80
        print "All upload permissions for %s:" % package
81
        print
82
        print "Component (%s)" % component
83
        print "============" + ("=" * len(component))
84
        print_uploaders([component_uploader], options.list_team_members)
1245.1.4 by Stefano Rivera
Rename tool, and make output less verbose by default
85
86
        packagesets = sorted(Packageset.setsIncludingSource(
87
                distroseries=series,
88
                sourcepackagename=package))
89
        if packagesets:
90
            print
91
            print "Packagesets"
92
            print "==========="
93
            for packageset in packagesets:
94
                print
95
                print "%s:" % packageset.name
96
                print_uploaders(archive.getUploadersForPackageset(
97
                    packageset=packageset), options.list_team_members)
98
99
        ppu_uploaders = archive.getUploadersForPackage(
100
                source_package_name=package)
101
        if ppu_uploaders:
102
            print
103
            print "Per-Package-Uploaders"
104
            print "====================="
105
            print
106
            print_uploaders(ppu_uploaders, options.list_team_members)
107
        print
108
1245.1.8 by Stefano Rivera
Pocket awareness
109
    if PersonTeam.me.canUploadPackage(archive, series, package, component,
110
                                      pocket):
1245.1.9 by Stefano Rivera
Tweak minimal output
111
        print "You can upload %s to %s." % (package, options.release)
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
112
    else:
1245.1.9 by Stefano Rivera
Tweak minimal output
113
        print ("You can not upload %s to %s, yourself."
114
               % (package, options.release))
1245.1.8 by Stefano Rivera
Pocket awareness
115
        if (series.status in ('Current Stable Release', 'Supported', 'Obsolete')
116
                and pocket == 'Release'):
117
            print ("%s is in the '%s' state. "
118
                   "You may want to query the %s-proposed pocket."
119
                   % (release, series.status, release))
1245.1.9 by Stefano Rivera
Tweak minimal output
120
        else:
121
            print ("But you can still contribute to it via the sponsorship "
122
                   "process: https://wiki.ubuntu.com/SponsorshipProcess")
1245.1.11 by Stefano Rivera
Don't list None as a release
123
            if not options.list_uploaders:
124
                print ("To see who has the necessary upload rights, "
125
                       "use the --list-uploaders option.")
1245.1.4 by Stefano Rivera
Rename tool, and make output less verbose by default
126
        sys.exit(1)
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
127
128
129
def print_uploaders(uploaders, expand_teams=False, prefix=''):
1245.1.2 by Stefano Rivera
UI tweaks
130
    """Given a list of uploaders, pretty-print them all
131
    Each line is prefixed with prefix.
132
    If expand_teams is set, recurse, adding more spaces to prefix on each
133
    recursion.
134
    """
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
135
    for uploader in sorted(uploaders, key=lambda p: p.display_name):
136
        print ("%s* %s (%s)%s"
137
               % (prefix, uploader.display_name, uploader.name,
138
                  ' [team]' if uploader.is_team else ''))
139
        if expand_teams and uploader.is_team:
140
            print_uploaders(uploader.participants, True, prefix=prefix + '  ')
141
1245.1.2 by Stefano Rivera
UI tweaks
142
1245.1.1 by Stefano Rivera
New Tool: who-can-upload (LP: #876554)
143
if __name__ == '__main__':
144
    main()