Clean up ChunkedUpdateDrawingAreaProxy
[WebKit-https.git] / WebKitTools / Scripts / webkitpy / tool / commands / rebaseline.py
1 # Copyright (c) 2010 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
7 #     * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 #     * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 #     * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 import os.path
30 import re
31 import shutil
32 import urllib
33
34 from webkitpy.common.net.buildbot import BuildBot
35 from webkitpy.common.net.layouttestresults import LayoutTestResults
36 from webkitpy.common.system.user import User
37 from webkitpy.layout_tests.port import factory
38 from webkitpy.tool.grammar import pluralize
39 from webkitpy.tool.multicommandtool import AbstractDeclarativeCommand
40
41
42 # FIXME: I'm not sure where this logic should go in the end.
43 # For now it's here, until we have a second need for it.
44 class BuilderToPort(object):
45     _builder_name_to_port_name = {
46         r"SnowLeopard": "mac-snowleopard",
47         r"Leopard": "mac-leopard",
48         r"Tiger": "mac-tiger",
49         r"Windows": "win",
50         r"GTK": "gtk",
51         r"Qt": "qt",
52         r"Chromium Mac": "chromium-mac",
53         r"Chromium Linux": "chromium-linux",
54         r"Chromium Win": "chromium-win",
55     }
56
57     def _port_name_for_builder_name(self, builder_name):
58         for regexp, port_name in self._builder_name_to_port_name.items():
59             if re.match(regexp, builder_name):
60                 return port_name
61
62     def port_for_builder(self, builder_name):
63         port_name = self._port_name_for_builder_name(builder_name)
64         assert(port_name)  # Need to update _builder_name_to_port_name
65         port = factory.get(port_name)
66         assert(port)  # Need to update _builder_name_to_port_name
67         return port
68
69
70 class Rebaseline(AbstractDeclarativeCommand):
71     name = "rebaseline"
72     help_text = "Replaces local expected.txt files with new results from build bots"
73
74     # FIXME: This should share more code with FailureReason._builder_to_explain
75     def _builder_to_pull_from(self):
76         builder_statuses = self._tool.buildbot.builder_statuses()
77         red_statuses = [status for status in builder_statuses if not status["is_green"]]
78         print "%s failing" % (pluralize("builder", len(red_statuses)))
79         builder_choices = [status["name"] for status in red_statuses]
80         chosen_name = self._tool.user.prompt_with_list("Which builder to pull results from:", builder_choices)
81         # FIXME: prompt_with_list should really take a set of objects and a set of names and then return the object.
82         for status in red_statuses:
83             if status["name"] == chosen_name:
84                 return (self._tool.buildbot.builder_with_name(chosen_name), status["build_number"])
85
86     def _replace_expectation_with_remote_result(self, local_file, remote_file):
87         (downloaded_file, headers) = urllib.urlretrieve(remote_file)
88         shutil.move(downloaded_file, local_file)
89
90     def _tests_to_update(self, build):
91         failing_tests = build.layout_test_results().results_matching_keys([LayoutTestResults.fail_key])
92         return self._tool.user.prompt_with_list("Which test(s) to rebaseline:", failing_tests, can_choose_multiple=True)
93
94     def _results_url_for_test(self, build, test):
95         test_base = os.path.splitext(test)[0]
96         actual_path = test_base + "-actual.txt"
97         return build.results_url() + "/" + actual_path
98
99     def execute(self, options, args, tool):
100         builder, build_number = self._builder_to_pull_from()
101         build = builder.build(build_number)
102         port = BuilderToPort().port_for_builder(builder.name())
103
104         for test in self._tests_to_update(build):
105             results_url = self._results_url_for_test(build, test)
106             # Port operates with absolute paths.
107             absolute_path = os.path.join(port.layout_tests_dir(), test)
108             expected_file = port.expected_filename(absolute_path, ".txt")
109             print test
110             self._replace_expectation_with_remote_result(expected_file, results_url)
111
112         # FIXME: We should handle new results too.