~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
#!/usr/bin/python3

# Copyright (C) 2011  Iain Lane
# Copyright (C) 2011  Stefano Rivera

# 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; version 3 of the License.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from optparse import OptionParser
import re

from launchpadlib.launchpad import Launchpad


def blacklist_package(options, pkg, reason):
    try:
        dsd = options.devel_series.getDifferencesTo(
            source_package_name_filter=pkg, parent_series=options.sid)[0]
    except IndexError:
        print("Couldn't blacklist %s (no DSD)" % pkg)
        return False

    if dsd.status == "Blacklisted always":
        print("%s already blacklisted" % pkg)
        return False

    if not options.dry_run:
        dsd.blacklist(all=True, comment=reason)
    return True


def main():
    parser = OptionParser(usage="usage: %prog [options] sync-blacklist.txt")
    parser.add_option(
        "-l", "--launchpad", dest="launchpad_instance", default="production")
    parser.add_option(
        "-n", "--dry-run", default=False, action="store_true",
        help="don't actually blacklist anything")
    options, args = parser.parse_args()
    if len(args) < 1:
        parser.error("requires input file as argument")
    blacklist = args[0]

    lp = Launchpad.login_with(
        'sync-blacklist', options.launchpad_instance, version='devel')
    ubuntu = lp.distributions['ubuntu']
    debian = lp.distributions['debian']

    options.devel_series = ubuntu.current_series
    options.sid = debian.current_series

    # Read blacklist
    applicable_comments = []
    with open(blacklist) as blacklist_file:
        for line in blacklist_file:
            if not line.strip():
                applicable_comments = []
                continue

            m = re.match(r'^\s*([a-z0-9.+-]+)?\s*(?:#\s*(.+)?)?$', line)
            source, comment = m.groups()
            if source:
                comments = applicable_comments[:]
                if comment:
                    comments.append(comment)
                if not comments:
                    comments.append("None given")
                comments.append("(from sync-blacklist.txt)")
                print("blacklisting %s (reason: %s)..."
                      % (source, '; '.join(comments)), end="")
                if blacklist_package(options, source, '\n'.join(comments)):
                    print("done")
            elif comment:
                applicable_comments.append(comment)


if __name__ == "__main__":
    main()