summaryrefslogtreecommitdiffstats
path: root/Tools/Scripts/webkitpy/tool/servers/gardeningserver.py
blob: 09660f60977bc5e32fe9a86ecd513684e644a30f (plain)
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
# Copyright (C) 2011 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1.  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import BaseHTTPServer
import os

from webkitpy.common.memoized import memoized
from webkitpy.tool.servers.reflectionhandler import ReflectionHandler
from webkitpy.layout_tests.controllers.test_expectations_editor import BugManager, TestExpectationsEditor
from webkitpy.layout_tests.models.test_expectations import TestExpectationParser, TestExpectations, TestExpectationSerializer
from webkitpy.layout_tests.models.test_configuration import TestConfigurationConverter
from webkitpy.layout_tests.port import builders


class BuildCoverageExtrapolator(object):
    def __init__(self, test_configuration_converter):
        self._test_configuration_converter = test_configuration_converter

    @memoized
    def _covered_test_configurations_for_builder_name(self):
        coverage = {}
        for builder_name in builders.all_builder_names():
            coverage[builder_name] = self._test_configuration_converter.to_config_set(builders.coverage_specifiers_for_builder_name(builder_name))
        return coverage

    def extrapolate_test_configurations(self, builder_name):
        return self._covered_test_configurations_for_builder_name()[builder_name]


class GardeningExpectationsUpdater(BugManager):
    def __init__(self, tool, port):
        self._converter = TestConfigurationConverter(port.all_test_configurations(), port.configuration_specifier_macros())
        self._extrapolator = BuildCoverageExtrapolator(self._converter)
        self._parser = TestExpectationParser(port, [], allow_rebaseline_modifier=False)
        self._path_to_test_expectations_file = port.path_to_test_expectations_file()
        self._tool = tool

    def close_bug(self, bug_id, reference_bug_id=None):
        # FIXME: Implement this properly.
        pass

    def create_bug(self):
        return "BUG_NEW"

    def update_expectations(self, failure_info_list):
        expectation_lines = self._parser.parse(self._tool.filesystem.read_text_file(self._path_to_test_expectations_file))
        editor = TestExpectationsEditor(expectation_lines, self)
        updated_expectation_lines = []
        # FIXME: Group failures by testName+failureTypeList.
        for failure_info in failure_info_list:
            expectation_set = set(filter(lambda expectation: expectation is not None,
                                         map(TestExpectations.expectation_from_string, failure_info['failureTypeList'])))
            assert(expectation_set)
            test_name = failure_info['testName']
            assert(test_name)
            builder_name = failure_info['builderName']
            affected_test_configuration_set = self._extrapolator.extrapolate_test_configurations(builder_name)
            updated_expectation_lines.extend(editor.update_expectation(test_name, affected_test_configuration_set, expectation_set))
        self._tool.filesystem.write_text_file(self._path_to_test_expectations_file, TestExpectationSerializer.list_to_string(expectation_lines, self._converter, reconstitute_only_these=updated_expectation_lines))


class GardeningHTTPServer(BaseHTTPServer.HTTPServer):
    def __init__(self, httpd_port, config):
        server_name = ''
        self.tool = config['tool']
        BaseHTTPServer.HTTPServer.__init__(self, (server_name, httpd_port), GardeningHTTPRequestHandler)

    def url(self):
        return 'file://' + os.path.join(GardeningHTTPRequestHandler.STATIC_FILE_DIRECTORY, 'garden-o-matic.html')


class GardeningHTTPRequestHandler(ReflectionHandler):
    STATIC_FILE_NAMES = frozenset()

    STATIC_FILE_DIRECTORY = os.path.join(
        os.path.dirname(__file__),
        '..',
        '..',
        '..',
        '..',
        'BuildSlaveSupport',
        'build.webkit.org-config',
        'public_html',
        'TestFailures')

    allow_cross_origin_requests = True

    def _run_webkit_patch(self, args):
        return self.server.tool.executive.run_command([self.server.tool.path()] + args, cwd=self.server.tool.scm().checkout_root)

    @memoized
    def _expectations_updater(self):
        # FIXME: Should split failure_info_list into lists per port, then edit each expectations file separately.
        # For now, assume Chromium port.
        port = self.server.tool.get("chromium-win-win7")
        return GardeningExpectationsUpdater(self.server.tool, port)

    def rollout(self):
        revision = self.query['revision'][0]
        reason = self.query['reason'][0]
        self._run_webkit_patch([
            'rollout',
            '--force-clean',
            '--non-interactive',
            revision,
            reason,
        ])
        self._serve_text('success')

    def ping(self):
        self._serve_text('pong')

    def updateexpectations(self):
        self._expectations_updater().update_expectations(self._read_entity_body_as_json())
        self._serve_text('success')

    def rebaseline(self):
        builder = self.query['builder'][0]
        command = [ 'rebaseline-test' ]

        if 'suffixes' in self.query:
            command.append('--suffixes')
            command.append(self.query['suffixes'][0])

        command.append(builder)
        command.append(self.query['test'][0])

        command.extend(builders.fallback_port_names_for_new_port(builder))
        self._run_webkit_patch(command)
        self._serve_text('success')

    def _builders_to_fetch_from(self, builders):
        # This routine returns the subset of builders that will cover all of the baseline search paths
        # used in the input list. In particular, if the input list contains both Release and Debug
        # versions of a configuration, we *only* return the Release version (since we don't save
        # debug versions of baselines).
        release_builders = set()
        debug_builders = set()
        builders_to_fallback_paths = {}
        for builder in builders:
            port = self.server.tool.port_factory.get_from_builder_name(builder)
            if port.test_configuration().build_type == 'Release':
                release_builders.add(builder)
            else:
                debug_builders.add(builder)
        for builder in list(release_builders) + list(debug_builders):
            port = self.server.tool.port_factory.get_from_builder_name(builder)
            fallback_path = port.baseline_search_path()
            if fallback_path not in builders_to_fallback_paths.values():
                builders_to_fallback_paths[builder] = fallback_path
        return builders_to_fallback_paths.keys()

    def rebaselineall(self):
        # FIXME: Optimize this to run in parallel, cache zips, etc.
        test_list = self._read_entity_body_as_json()
        for test in test_list:
            all_suffixes = set()
            builders = self._builders_to_fetch_from(test_list[test])
            for builder in builders:
                suffixes = test_list[test][builder]
                all_suffixes.update(suffixes)
                self._run_webkit_patch(['rebaseline-test', '--suffixes', ','.join(suffixes), builder, test])
            self._run_webkit_patch(['optimize-baselines', '--suffixes', ','.join(all_suffixes), test])
        self._serve_text('success')