File: lp-bug-dupe-properties

package info (click to toggle)
lptools 0.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 400 kB
  • sloc: python: 1,550; makefile: 6
file content (124 lines) | stat: -rwxr-xr-x 4,179 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python3
#
# Copyright (C) 2012, Canonical Ltd.
# Written by Brian Murray
#
# ##################################################################
#
# 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.
#
# 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.
#
# See file /usr/share/common-licenses/GPL-3 for more details.
#
# ##################################################################

from lptools import config
from datetime import datetime

import argparse
import sys

release_tags = []

def check_duplicate(bug, prop, key):
    if prop == 'reporter':
        return bug.owner.name
    if prop == 'month':
        return datetime.strftime(bug.date_created, '%Y-%m')
    if prop == 'day':
        return datetime.strftime(bug.date_created, '%Y-%m-%d')
    if prop == 'tags':
        return ' '.join(bug.tags)
    if prop == 'rtags':
        rtags = set(release_tags).intersection(bug.tags)
        return ' '.join(list(rtags))
    if prop == 'desc':
        APPORT_TAGS = ['apport-crash', 'apport-bug', 'apport-kerneloops', 'apport-package']
        if not set(APPORT_TAGS).intersection(bug.tags):
            return
        description = bug.description
        if key not in description:
            return
        for line in description.splitlines():
            if not line.startswith(key):
                continue
            if key == line.split(': ')[0]:
                value = line.split(': ')[-1]
                return value


def main():
    parser = argparse.ArgumentParser(prog='lp-bug-dupe-properties')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-r', '--reporter', default=False,
        action='store_true', help='Display reporter of duplicates')
    group.add_argument('-m', '--month', default=False,
        action='store_true', help='Display month duplicates were reported')
    group.add_argument('-d', '--day', default=False,
        action='store_true', help='Display day duplicates were reported')
    group.add_argument('-t', '--tags', default=False,
        action='store_true', help='Display tags of duplicates')
    group.add_argument('-rt', '--rtags', default=False,
        action='store_true', help='Display Ubuntu release tags of duplicates')
    group.add_argument('-D', '--desc', default=False, type=str,
        help='Search apport bug description for this key e.g. Package')
    parser.add_argument('-b', '--bug', type=int,
        help='Bug number of which to check the duplicates')

    opts = parser.parse_args()
    launchpad = config.get_launchpad("bug-dupe-properties")

    bug_number = opts.bug
    bug = launchpad.bugs[bug_number]

    dupe_props = {}

    if opts.reporter:
        search = 'reporter'
    if opts.month:
        search = 'month'
    if opts.day:
        search = 'day'
    if opts.tags:
        search = 'tags'
    if opts.rtags:
        search = 'rtags'
        ubuntu = launchpad.distributions['ubuntu']
        for series in ubuntu.series:
            release_tags.append(series.name)
    if opts.desc:
        search = 'desc'
        key = opts.desc
    else:
        key = None

    if bug.number_of_duplicates == 0:
        print(('LP: #%s has no duplicates!' % bug_number))
        sys.exit(1)

    for dupe in bug.duplicates:
        dupe_num = dupe.id
        prop = check_duplicate(dupe, search, key)
        if prop in list(dupe_props.keys()):
            dupe_props[prop].append(str(dupe_num))
        else:
            dupe_props[prop] = [str(dupe_num)]

    dupe_count = bug.number_of_duplicates
    if dupe_count > 1:
        print(('LP: #%s has %s duplicates' % (bug_number, dupe_count)))
    elif dupe_count == 1:
        print(('LP: #%s has %s duplicate' % (bug_number, dupe_count)))

    for prop, bugs in sorted(dupe_props.items()):
        print(('  %s: %s' % (prop, ' '.join(bugs))))


if __name__ == '__main__':
    main()