openbugs.py revision bec39347bb3bb5bf1187ccaf471d26247f28b585
1d0825bca7fe65beaee391d30da42e937db621564Steve Block# Copyright (c) 2010 Google Inc. All rights reserved.
2231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#
3231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# Redistribution and use in source and binary forms, with or without
4231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# modification, are permitted provided that the following conditions are
5231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# met:
6231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#
7231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#     * Redistributions of source code must retain the above copyright
8231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# notice, this list of conditions and the following disclaimer.
9231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#     * Redistributions in binary form must reproduce the above
10231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# copyright notice, this list of conditions and the following disclaimer
11231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# in the documentation and/or other materials provided with the
12231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# distribution.
13231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#     * Neither the name of Google Inc. nor the names of its
14231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# contributors may be used to endorse or promote products derived from
15231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# this software without specific prior written permission.
16231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#
17231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
29d0825bca7fe65beaee391d30da42e937db621564Steve Blockimport re
30643ca7872b450ea4efacab6188849e5aac2ba161Steve Blockimport sys
31231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
32dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.tool.multicommandtool import AbstractDeclarativeCommand
33dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.common.system.deprecated_logging import log
34d0825bca7fe65beaee391d30da42e937db621564Steve Block
35d0825bca7fe65beaee391d30da42e937db621564Steve Block
36d0825bca7fe65beaee391d30da42e937db621564Steve Blockclass OpenBugs(AbstractDeclarativeCommand):
37d0825bca7fe65beaee391d30da42e937db621564Steve Block    name = "open-bugs"
38d0825bca7fe65beaee391d30da42e937db621564Steve Block    help_text = "Finds all bug numbers passed in arguments (or stdin if no args provided) and opens them in a web browser"
39d0825bca7fe65beaee391d30da42e937db621564Steve Block
40d0825bca7fe65beaee391d30da42e937db621564Steve Block    bug_number_regexp = re.compile(r"\b\d{4,6}\b")
41d0825bca7fe65beaee391d30da42e937db621564Steve Block
42d0825bca7fe65beaee391d30da42e937db621564Steve Block    def _open_bugs(self, bug_ids):
43d0825bca7fe65beaee391d30da42e937db621564Steve Block        for bug_id in bug_ids:
44bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen            bug_url = self._tool.bugs.bug_url_for_bug_id(bug_id)
45bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen            self._tool.user.open_url(bug_url)
46d0825bca7fe65beaee391d30da42e937db621564Steve Block
47d0825bca7fe65beaee391d30da42e937db621564Steve Block    # _find_bugs_in_string mostly exists for easy unit testing.
48d0825bca7fe65beaee391d30da42e937db621564Steve Block    def _find_bugs_in_string(self, string):
49d0825bca7fe65beaee391d30da42e937db621564Steve Block        return self.bug_number_regexp.findall(string)
50231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
51d0825bca7fe65beaee391d30da42e937db621564Steve Block    def _find_bugs_in_iterable(self, iterable):
52d0825bca7fe65beaee391d30da42e937db621564Steve Block        return sum([self._find_bugs_in_string(string) for string in iterable], [])
53231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
54d0825bca7fe65beaee391d30da42e937db621564Steve Block    def execute(self, options, args, tool):
55d0825bca7fe65beaee391d30da42e937db621564Steve Block        if args:
56d0825bca7fe65beaee391d30da42e937db621564Steve Block            bug_ids = self._find_bugs_in_iterable(args)
57d0825bca7fe65beaee391d30da42e937db621564Steve Block        else:
58d0825bca7fe65beaee391d30da42e937db621564Steve Block            # This won't open bugs until stdin is closed but could be made to easily.  That would just make unit testing slightly harder.
59d0825bca7fe65beaee391d30da42e937db621564Steve Block            bug_ids = self._find_bugs_in_iterable(sys.stdin)
60231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
61d0825bca7fe65beaee391d30da42e937db621564Steve Block        log("%s bugs found in input." % len(bug_ids))
62231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
63d0825bca7fe65beaee391d30da42e937db621564Steve Block        self._open_bugs(bug_ids)
64