Clean up ChunkedUpdateDrawingAreaProxy
[WebKit-https.git] / Tools / Scripts / webkitpy / tool / bot / commitqueuetask.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 from webkitpy.common.system.executive import ScriptError
30 from webkitpy.common.net.layouttestresults import LayoutTestResults
31
32
33 class CommitQueueTaskDelegate(object):
34     def run_command(self, command):
35         raise NotImplementedError("subclasses must implement")
36
37     def command_passed(self, message, patch):
38         raise NotImplementedError("subclasses must implement")
39
40     def command_failed(self, message, script_error, patch):
41         raise NotImplementedError("subclasses must implement")
42
43     def refetch_patch(self, patch):
44         raise NotImplementedError("subclasses must implement")
45
46     def layout_test_results(self):
47         raise NotImplementedError("subclasses must implement")
48
49     def report_flaky_tests(self, patch, flaky_tests):
50         raise NotImplementedError("subclasses must implement")
51
52
53 class CommitQueueTask(object):
54     def __init__(self, delegate, patch):
55         self._delegate = delegate
56         self._patch = patch
57         self._script_error = None
58
59     def _validate(self):
60         # Bugs might get closed, or patches might be obsoleted or r-'d while the
61         # commit-queue is processing.
62         self._patch = self._delegate.refetch_patch(self._patch)
63         if self._patch.is_obsolete():
64             return False
65         if self._patch.bug().is_closed():
66             return False
67         if not self._patch.committer():
68             return False
69         # Reviewer is not required. Missing reviewers will be caught during
70         # the ChangeLog check during landing.
71         return True
72
73     def _run_command(self, command, success_message, failure_message):
74         try:
75             self._delegate.run_command(command)
76             self._delegate.command_passed(success_message, patch=self._patch)
77             return True
78         except ScriptError, e:
79             self._script_error = e
80             self.failure_status_id = self._delegate.command_failed(failure_message, script_error=self._script_error, patch=self._patch)
81             return False
82
83     def _clean(self):
84         return self._run_command([
85             "clean",
86         ],
87         "Cleaned working directory",
88         "Unable to clean working directory")
89
90     def _apply(self):
91         return self._run_command([
92             "apply-attachment",
93             "--non-interactive",
94             self._patch.id(),
95         ],
96         "Applied patch",
97         "Patch does not apply")
98
99     def _build(self):
100         return self._run_command([
101             "build",
102             "--no-clean",
103             "--no-update",
104             "--build-style=both",
105         ],
106         "Built patch",
107         "Patch does not build")
108
109     def _build_without_patch(self):
110         return self._run_command([
111             "build",
112             "--force-clean",
113             "--no-update",
114             "--build-style=both",
115         ],
116         "Able to build without patch",
117         "Unable to build without patch")
118
119     def _test(self):
120         return self._run_command([
121             "build-and-test",
122             "--no-clean",
123             "--no-update",
124             # Notice that we don't pass --build, which means we won't build!
125             "--test",
126             "--non-interactive",
127         ],
128         "Passed tests",
129         "Patch does not pass tests")
130
131     def _build_and_test_without_patch(self):
132         return self._run_command([
133             "build-and-test",
134             "--force-clean",
135             "--no-update",
136             "--build",
137             "--test",
138             "--non-interactive",
139         ],
140         "Able to pass tests without patch",
141         "Unable to pass tests without patch (tree is red?)")
142
143     def _failing_tests_from_last_run(self):
144         results = self._delegate.layout_test_results()
145         if not results:
146             return None
147         return results.failing_tests()
148
149     def _land(self):
150         # Unclear if this should pass --quiet or not.  If --parent-command always does the reporting, then it should.
151         return self._run_command([
152             "land-attachment",
153             "--force-clean",
154             "--ignore-builders",
155             "--non-interactive",
156             "--parent-command=commit-queue",
157             self._patch.id(),
158         ],
159         "Landed patch",
160         "Unable to land patch")
161
162     def _report_flaky_tests(self, flaky_tests):
163         self._delegate.report_flaky_tests(self._patch, flaky_tests)
164
165     def _test_patch(self):
166         if self._patch.is_rollout():
167             return True
168         if self._test():
169             return True
170
171         first_failing_tests = self._failing_tests_from_last_run()
172         if self._test():
173             self._report_flaky_tests(first_failing_tests)
174             return True
175
176         second_failing_tests = self._failing_tests_from_last_run()
177         if first_failing_tests != second_failing_tests:
178             self._report_flaky_tests(first_failing_tests + second_failing_tests)
179             return False
180
181         if self._build_and_test_without_patch():
182             raise self._script_error  # The error from the previous ._test() run is real, report it.
183         return False  # Tree must be red, just retry later.
184
185     def run(self):
186         if not self._validate():
187             return False
188         if not self._clean():
189             return False
190         if not self._apply():
191             raise self._script_error
192         if not self._build():
193             if not self._build_without_patch():
194                 return False
195             raise self._script_error
196         if not self._test_patch():
197             return False
198         # Make sure the patch is still valid before landing (e.g., make sure
199         # no one has set commit-queue- since we started working on the patch.)
200         if not self._validate():
201             return False
202         if not self._land():
203             raise self._script_error
204         return True