~jml/+junk/whip

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from itertools import chain, imap
from operator import attrgetter
import os

from launchpadlib.launchpad import Launchpad
from launchpadlib.uris import LPNET_SERVICE_ROOT


APP_NAME = 'jml-bug-jam'
CACHE_DIR = os.path.expanduser('~/.launchpadlib/cache')
SERVICE_ROOT = LPNET_SERVICE_ROOT


BUG_STATUSES = [
    "New",
    "Incomplete",
    "Invalid",
    "Won't Fix",
    "Opinion",
    "Confirmed",
    "Triaged",
    "In Progress",
    "Fix Committed",
    'Fix Released',
    ]


CLOSED_BUG_STATUSES = [
    "Opinion",
    "Invalid",
    "Won't Fix",
    'Fix Released',
    ]


FIXED_BUG_STATUSES = [
    'Fix Released',
    ]


MP_STATUSES = [
    'Work in progress',
    'Needs review',
    'Approved',
    'Rejected',
    'Merged',
    'Code failed to merge',
    'Queued',
    'Superseded',
    ]


BRANCH_STATUSES = [
    'Experimental',
    'Development',
    'Mature',
    'Merged',
    'Abandoned',
    ]


def get_bug_id(bug_task):
    """Get the bug ID for a bug task."""
    return int(bug_task.self_link.split('/')[-1])


def get_launchpad():
    return Launchpad.login_anonymously(APP_NAME, SERVICE_ROOT, CACHE_DIR)


def get_assigned_bugs(person):
    return imap(BugTask, person.searchTasks(assignee=person))


def get_open_branches(person):
    return (
        branch for branch in imap(
            Branch,
            person.getBranches(status=['Development', 'Experimental']))
        if not branch.is_linked and branch.target)


def get_merge_proposals(person):
    return imap(
        MergeProposal,
        person.getMergeProposals(
            status=['Work in progress', 'Needs review', 'Approved']))


def get_requested_reviews(person):
    return imap(
        MergeProposal, person.getRequestedReviews(status=['Needs review']))


class Branch(object):

    def __init__(self, lp_branch):
        self._lp_branch = lp_branch

    @property
    def target(self):
        """The project or source package that the branch is against."""
        if self._lp_branch.project is not None:
            return Target(self._lp_branch.project)
        if self._lp_branch.sourcepackage is not None:
            return Target(self._lp_branch.sourcepackage)
        return None

    @property
    def is_linked(self):
        """Is the branch linked to something interesting?"""
        return not self._lp_branch.bzr_identity.endswith(
            self._lp_branch.unique_name)

    def __str__(self):
        return self._lp_branch.bzr_identity


class MergeProposal(object):

    def __init__(self, lp_mp):
        self._lp_mp = lp_mp

    @property
    def source_branch(self):
        return Branch(self._lp_mp.source_branch)

    @property
    def target_branch(self):
        return Branch(self._lp_mp.target_branch)

    @property
    def target(self):
        return self.target_branch.target

    @property
    def status(self):
        return self._lp_mp.queue_status

    def __str__(self):
        return '%s => %s' % (self.source_branch, self.target_branch)


class BugTask(object):

    def __init__(self, lp_bugtask):
        self._lp_bugtask = lp_bugtask

    @property
    def bug_id(self):
        return get_bug_id(self._lp_bugtask)

    @property
    def target(self):
        return Target(self._lp_bugtask.target)

    def __str__(self):
        return '%s (%s)' % (self._lp_bugtask.title, self._lp_bugtask.status)


class Target(object):

    def __init__(self, lp_target):
        self._lp_target = lp_target

    def __str__(self):
        return self.name

    @property
    def name(self):
        return self._lp_target.name


class Dashboard(object):

    # AXES:
    # - person
    # - target
    # - type of item
    # - status of item

    def __init__(self, person, assigned_bugs, open_branches, merge_proposals,
                 requested_reviews):
        self._person = person
        self._assigned_bugs = assigned_bugs
        self._open_branches = open_branches
        self._merge_proposals = merge_proposals
        self._requested_reviews = requested_reviews

    @classmethod
    def load(cls, person):
        return cls(
            person,
            get_assigned_bugs(person),
            get_open_branches(person),
            get_merge_proposals(person),
            get_requested_reviews(person),
            )

    @property
    def all_inventory(self):
        return chain(
            self._assigned_bugs, self._open_branches, self._merge_proposals,
            self._requested_reviews)

    def grouped_by_target(self, ordered=False):
        targets = {}
        inventory = {'bugs': self._assigned_bugs,
                     'branches': self._open_branches,
                     'merge-proposals': self._merge_proposals,
                     'requested-reviews': self._requested_reviews}
        for name, items in inventory.iteritems():
            for item in items:
                target_name = item.target.name
                if target_name not in targets:
                    targets[target_name] = {}
                if name not in targets[target_name]:
                    targets[target_name][name] = []
                targets[target_name][name].append(item)
        if ordered:
            targets = sorted((k, sorted(v.items())) for k, v in targets.items())
        return targets

    def summary_by_target(self):
        targets = self.grouped_by_target(ordered=True)
        for name, items in targets:
            items = ['%s=%s' % (k, len(v)) for k, v in items]
            print '%s: %s' % (name, ', '.join(items))

    def full_by_target(self):
        targets = self.grouped_by_target(ordered=True)
        for name, items in targets:
            print '= %s =' % (name,)
            print
            for item_type, item_values in items:
                print '== %s ==' % (item_type,)
                print
                for item in item_values:
                    print ' * %s' % (item,)
                print

    def dump(self):
        self.summary_by_target()


def run(args):
    username = args[1]
    lp = get_launchpad()
    person = lp.people[username]
    dashboard = Dashboard.load(person)
    dashboard.dump()
    return 0