1#!/usr/bin/python
2#
3# Copyright (C) 2013 Google Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""Prints lists of bug numbers / tests whose bugs haven't been modified recently."""
32
33import datetime
34import json
35import optparse
36import re
37import sys
38import time
39import urllib2
40
41from webkitpy.common.system.filesystem import FileSystem
42from webkitpy.common.webkit_finder import WebKitFinder
43
44google_code_url = 'https://www.googleapis.com/projecthosting/v2/projects/chromium/issues/%s?key=AIzaSyDgCqT1Dt5AZWLHo4QJjyMHaCjhnFacGF0'
45crbug_prefix = 'crbug.com/'
46
47class StaleTestPrinter(object):
48    def __init__(self, options):
49        self._days = options.days
50
51    def is_stale(self, bug_number):
52        url = google_code_url % bug_number
53        response = urllib2.urlopen(url)
54        parsed = json.loads(response.read())
55        last_updated = parsed['updated']
56        parsed_time = datetime.datetime.strptime(last_updated.split(".")[0]+"UTC", "%Y-%m-%dT%H:%M:%S%Z")
57        time_delta = datetime.datetime.now() - parsed_time
58        return time_delta.days > 90
59
60    def print_stale_tests(self):
61        finder = WebKitFinder(FileSystem())
62        path_to_expectations = finder.path_from_webkit_base('LayoutTests', 'TestExpectations')
63        expectations = open(path_to_expectations)
64
65        for line in expectations:
66            comment_index = line.find("#")
67            if comment_index == -1:
68                comment_index = len(line)
69
70            remaining_string = re.sub(r"\s+", " ", line[:comment_index].strip())
71            if len(remaining_string) == 0:
72                continue
73
74            is_bug_stale = True
75            parts = line.split(' ')
76            for part in parts:
77                if part.startswith(crbug_prefix):
78                    bug_number = part.split('/')[1]
79                    try:
80                        if not self.is_stale(bug_number):
81                            is_bug_stale = False
82                            break;
83                    except urllib2.HTTPError as error:
84                        if error.code == 404:
85                            print '%s%s does not exist.' % (crbug_prefix, bug_number)
86                        elif error.code == 403:
87                            print '%s%s is not accessible. Not able to tell if it\'s stale.' % (crbug_prefix, bug_number)
88                            is_bug_stale = False
89                        else:
90                            raise error
91
92            if is_bug_stale:
93                print line.strip()
94
95def main(argv):
96    option_parser = optparse.OptionParser()
97    option_parser.add_option('--days', type='int', default=90, help='Number of days to consider a bug stale.'),
98    options, args = option_parser.parse_args(argv)
99
100    printer = StaleTestPrinter(options)
101    printer.print_stale_tests()
102    return 0
103
104if __name__ == '__main__':
105    sys.exit(main(sys.argv[1:]))
106