~bughelper-dev/bughelper/main

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
253
254
255
256
257
258
259
260
#!/usr/bin/env python

# Written by Henrik Nilsen Omma
# (C) Canonical, Ltd. Licensed under the GPL

import os
import sys
import urllib
import re
import time

try:
    from commandLine import commandLine
    from infoFiles import InfoFiles
    import connector as Connector
    import utils
    import config
    import bzrutils
    import output
    import statistic
except:
    from bugHelper.commandLine import commandLine
    from bugHelper.infoFiles import InfoFiles
    import bugHelper.config as config
    import bugHelper.bzrutils as bzrutils
    import bugHelper.output as output
    import bugHelper.statistic as statistic
    import launchpadbugs.connector as Connector
    import bugHelper.utils as utils
    from launchpadbugs.basebuglistfilter import URLBugListFilter
    from launchpadbugs.lpconstants import BASEURL, BUG
    from launchpadbugs.lphelper import sort
    from launchpadbugs.exceptions import LaunchpadError

def main():
    conf = config.Config("~/.bughelper/config")
    cl = commandLine()
    
    if cl.options.parsemode not in ("text","html"):
        cl.parser.print_usage()
        sys.exit(1)
    Bug = Connector.ConnectBug(method=cl.options.parsemode)
    BugList = Connector.ConnectBugList(method=cl.options.parsemode)
    
    cookie = cl.options.cookie
    if cookie:
        cookie = os.path.expanduser(cookie)
    else:
        try:
            cookie = os.path.expanduser(conf.cookie)
        except:
            if cl.options.debug:
                print >> sys.stderr, "No cookie-file found"
                
    if cookie:
        BugList.authentication=cookie
        Bug.authentication=cookie
        
    try:
        out = output.Output("bughelper",cl.options.format,cl.options.file)
    except output.StyleNotFound, e:
        print >> sys.stderr, e
        sys.exit(1)
    out.print_header()
    start = time.time()
    if cl.options.gen_config:
        sys.exit(0)

    # TODO: Where to print messages like 'Now on revision xx'
    # or 'No revisions to pull.'
    try:
        (bzrinfo,bzrerr) = bzrutils.update_packages_data(conf.packages_dir, conf.update_interval)
        # print (bzrinfo,bzrerr)
    except bzrutils.BzrError, e:
        out.error(e)
    if not cl.options.try_only_clue:
        files = InfoFiles(set((conf.packages_dir, conf.local_packages_dir)))
    else:
        files = InfoFiles(set())
        files.add_simple_clue(cl.options.try_only_clue[0], 
                              cl.options.try_only_clue[1],
                              cl.options.try_only_clue[2])
    if cl.options.try_clue:
        # load existing clues first
        files.get_info_file(cl.options.try_clue[0], cl.options.verbose)
        files.add_simple_clue(cl.options.try_clue[0], 
                              cl.options.try_clue[1],
                              cl.options.try_clue[2])
    if cl.options.bugnr:
        bl = set([cl.options.bugnr]) #FIXME: this should be BugList, not set
    else:
        if not cl.options.url and not cl.options.sourcepackage:
            cl.parser.print_usage()
            sys.exit(1)
            
        bug_filter = URLBugListFilter()
        
        if not cl.options.url:
            cl.options.url = \
                bug_filter("%s/ubuntu/+source/%s/+bugs" %(BASEURL.BUG, cl.options.sourcepackage))
        else:
            try:
                cl.options.url = bug_filter(cl.options.url)
            except ValueError, e:
                print >> sys.stderr, e
                sys.exit(1)
    
        #HACK!!
        # the text-interface of LP does not provide infos like status, importance etc.
        # in a bug-list. [..]
        # move this to the end of the functions-list?
        if cl.options.parsemode == "text":
            from launchpadbugs.basebuglistfilter import get_current_task
            bug_filter.functions.append(lambda x: get_current_task(x, bug_filter.baseurl, Bug))
            
        if cl.options.ignore_conflicts:
            bug_filter._conflicts_error = bug_filter.ADD
             
        if cl.options.minbug:
            from launchpadbugs.basebuglistfilter import minbug
            bug_filter.functions.append(lambda x: minbug(x, int(cl.options.minbug)))
            
        if cl.options.filterbug:
            from launchpadbugs.basebuglistfilter import filterbug
            bug_filter.functions.append(lambda x: filterbug(x, map(int, cl.options.filterbug.split(","))))
          
        if cl.options.status:
            x = set(cl.options.status.split(","))
            if not x <= set(BUG.STATUS.values()).union(set(BUG.STATUS_INCOMPLETE_ADD.values())):
                raise ValueError, "unknown status: %s" %x
            if "Incomplete" in x:
                x.remove("Incomplete")
                x = x.union(set(BUG.STATUS_INCOMPLETE_ADD.keys()))
            for k,i in BUG.STATUS_INCOMPLETE_ADD.iteritems():
                if i in x:
                    x.remove(i)
                    x.add(k)
            bug_filter.add_option("status", x)
            
        if cl.options.importance:
            x = cl.options.importance.split(",")
            if not set(x) <= set(BUG.IMPORTANCE.values()):
                raise ValueError, "unknown status: %s" %x
            bug_filter.add_option("importance", x)
        
        if cl.options.closed_bugs:
            bug_filter.add_option("status", BUG.STATUS.values())
            
        if cl.options.tag:
            bug_filter.add_option("tag", cl.options.tag.split(","))
            
        if cl.options.duplicates:
            bug_filter.add_option("duplicates", ("off",))
            
        if cl.options.reporter:
            bug_filter.add_option("reporter", (cl.options.reporter,))

        try:
            bl = BugList(cl.options.url)
        except LaunchpadError, e:
            if cl.options.sourcepackage:
                print >> sys.stderr, " Maybe package '%s' does not exist in Ubuntu." % cl.options.sourcepackage 
            else:
                print >> sys.stderr, "Error while parsing '%s': %s.%s" %(cl.options.url, e, x)
            sys.exit(1)
            
        if cl.options.upstream and cl.options.sourcepackage:
            l = None
            try:
                l = BugList(bug_filter("%s/%s/+bugs" %(BASEURL.BUG, cl.options.sourcepackage)))
            except LaunchpadError, e:
                print >> sys.stderr, "There is no product <%s> in launchpad.net" %cl.options.sourcepackage
            if l:
                bl += l
            
                
        if cl.options.sourcepackage:
            path = os.path.expanduser(os.path.join(conf.attachments_cache, 
                                                   cl.options.sourcepackage))
            utils.remove_obsolete_attachments(path, bl)

    if not bl:
        if not bug_filter.urlopt:
            out.info("There are currently no open bugs.")
        else:
            out.info("No results for search.")
        out.print_footer()
        sys.exit(1)
    count_bugs = 0
    all_bugs = []
    if cl.options.attachments:
        Bug.attachment_path = conf.attachments_cache
    if cl.options.sort:
        bl = bl.sort(cl.options.sort)
    for bugNum in bl:
        b = bugNum
        if str(type(b)) != "Bug":
            try:
                b = Bug(int(bugNum))
            except LaunchpadError, e:
                if cl.options.debug:
                    print >> sys.stderr, "This error occurs while getting #%s: %s" %(bugNum, e)
                continue
        displayclues = dict()
        try:
            info_file = files.get_info_file(b.sourcepackage, cl.options.verbose)
        except AttributeError, e:
            if cl.options.debug:
                print >> sys.stderr, "This error occurs while getting #%s: %s, \
possible reason: bugreport changed while running bughelper" %(bugNum, e)
            continue
        if not int(bugNum) in info_file.dontlist:
            collected_clues = set()
            if cl.options.heuristic and (not b.sourcepackage or \
                                         not info_file.clues):
                all_files = files.get_available_clue_files()
                for f in all_files:
                    info_file = files.get_info_file(b.sourcepackage, \
                                                    cl.options.verbose)
                    collected_clues.update(info_file.clues)
                    collected_clues.update(info_file.inherited_clues)
            if info_file.clues:
                collected_clues.update(info_file.clues)
                collected_clues.update(info_file.inherited_clues)
                for clue in collected_clues:
                    try:
                        if info_file.clue_matches(clue, b, 
                                              cl.options.case_sensitive):
                            displayclues[clue.info] = set()
                    except AttributeError, e:
                        if cl.options.debug:
                            print >> sys.stderr, e
                    if cl.options.attachments:
                        for a in b.attachments.filter(lambda a: re.match("^.*[^\.gz]$", a.lp_filename)):
                            try:
                                if info_file.clue_matches(clue, a):
                                    if not displayclues.has_key(clue.info):
                                        displayclues[clue.info] = set()
                                    displayclues[clue.info].add(a.url)
                            except (AttributeError, LaunchpadError), e:
                                if cl.options.debug:
                                    print >> sys.stderr, e
                if displayclues:
                    out.print_item({"bug": b, "clue" : displayclues})
                    all_bugs.append(b)
    
    footer_opt = {}
    if cl.options.footer:
        o = list(cl.options.footer)
        if "s" in o:
            footer_opt["statistic"] = statistic.summary(all_bugs)
        if "t" in o:
            footer_opt["time"] = {
                "time": time.strftime("%a, %d %b %Y %H:%M:%S %Z"),
                "duration" : "%i" %(time.time() - start)}
    out.print_footer(footer_opt)

if __name__ == "__main__":
    main()