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
29import os.path
30import re
31import shutil
32import urllib
33
34from webkitpy.common.net.buildbot import BuildBot
35from webkitpy.common.net.layouttestresults import LayoutTestResults
36from webkitpy.common.system.user import User
37from webkitpy.layout_tests.layout_package import test_failures
38from webkitpy.layout_tests.port import factory
39from webkitpy.tool.grammar import pluralize
40from webkitpy.tool.multicommandtool import AbstractDeclarativeCommand
41
42
43# FIXME: I'm not sure where this logic should go in the end.
44# For now it's here, until we have a second need for it.
45class BuilderToPort(object):
46    _builder_name_to_port_name = {
47        r"SnowLeopard": "mac-snowleopard",
48        r"Leopard": "mac-leopard",
49        r"Tiger": "mac-tiger",
50        r"Windows": "win",
51        r"GTK": "gtk",
52        r"Qt": "qt",
53        r"Chromium Mac": "chromium-mac",
54        r"Chromium Linux": "chromium-linux",
55        r"Chromium Win": "chromium-win",
56    }
57
58    def _port_name_for_builder_name(self, builder_name):
59        for regexp, port_name in self._builder_name_to_port_name.items():
60            if re.match(regexp, builder_name):
61                return port_name
62
63    def port_for_builder(self, builder_name):
64        port_name = self._port_name_for_builder_name(builder_name)
65        assert(port_name)  # Need to update _builder_name_to_port_name
66        port = factory.get(port_name)
67        assert(port)  # Need to update _builder_name_to_port_name
68        return port
69
70
71class Rebaseline(AbstractDeclarativeCommand):
72    name = "rebaseline"
73    help_text = "Replaces local expected.txt files with new results from build bots"
74
75    # FIXME: This should share more code with FailureReason._builder_to_explain
76    def _builder_to_pull_from(self):
77        builder_statuses = self._tool.buildbot.builder_statuses()
78        red_statuses = [status for status in builder_statuses if not status["is_green"]]
79        print "%s failing" % (pluralize("builder", len(red_statuses)))
80        builder_choices = [status["name"] for status in red_statuses]
81        chosen_name = self._tool.user.prompt_with_list("Which builder to pull results from:", builder_choices)
82        # FIXME: prompt_with_list should really take a set of objects and a set of names and then return the object.
83        for status in red_statuses:
84            if status["name"] == chosen_name:
85                return (self._tool.buildbot.builder_with_name(chosen_name), status["build_number"])
86
87    def _replace_expectation_with_remote_result(self, local_file, remote_file):
88        (downloaded_file, headers) = urllib.urlretrieve(remote_file)
89        shutil.move(downloaded_file, local_file)
90
91    def _tests_to_update(self, build):
92        failing_tests = build.layout_test_results().tests_matching_failure_types([test_failures.FailureTextMismatch])
93        return self._tool.user.prompt_with_list("Which test(s) to rebaseline:", failing_tests, can_choose_multiple=True)
94
95    def _results_url_for_test(self, build, test):
96        test_base = os.path.splitext(test)[0]
97        actual_path = test_base + "-actual.txt"
98        return build.results_url() + "/" + actual_path
99
100    def execute(self, options, args, tool):
101        builder, build_number = self._builder_to_pull_from()
102        build = builder.build(build_number)
103        port = BuilderToPort().port_for_builder(builder.name())
104
105        for test in self._tests_to_update(build):
106            results_url = self._results_url_for_test(build, test)
107            # Port operates with absolute paths.
108            absolute_path = os.path.join(port.layout_tests_dir(), test)
109            expected_file = port.expected_filename(absolute_path, ".txt")
110            print test
111            self._replace_expectation_with_remote_result(expected_file, results_url)
112
113        # FIXME: We should handle new results too.
114