1# Copyright (C) 2009 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
29from webkitpy.tool import steps
30
31from webkitpy.common.checkout.scm import CheckoutNeedsUpdate
32from webkitpy.common.system.deprecated_logging import log
33from webkitpy.common.system.executive import ScriptError
34from webkitpy.tool.bot.queueengine import QueueEngine
35
36
37class StepSequenceErrorHandler():
38    @classmethod
39    def handle_script_error(cls, tool, patch, script_error):
40        raise NotImplementedError, "subclasses must implement"
41
42    @classmethod
43    def handle_checkout_needs_update(cls, tool, state, options, error):
44        raise NotImplementedError, "subclasses must implement"
45
46
47class StepSequence(object):
48    def __init__(self, steps):
49        self._steps = steps or []
50
51    def options(self):
52        collected_options = [
53            steps.Options.parent_command,
54            steps.Options.quiet,
55        ]
56        for step in self._steps:
57            collected_options = collected_options + step.options()
58        # Remove duplicates.
59        collected_options = sorted(set(collected_options))
60        return collected_options
61
62    def _run(self, tool, options, state):
63        for step in self._steps:
64            step(tool, options).run(state)
65
66    def run_and_handle_errors(self, tool, options, state=None):
67        if not state:
68            state = {}
69        try:
70            self._run(tool, options, state)
71        except CheckoutNeedsUpdate, e:
72            log("Commit failed because the checkout is out of date.  Please update and try again.")
73            if options.parent_command:
74                command = tool.command_by_name(options.parent_command)
75                command.handle_checkout_needs_update(tool, state, options, e)
76            QueueEngine.exit_after_handled_error(e)
77        except ScriptError, e:
78            if not options.quiet:
79                log(e.message_with_output())
80            if options.parent_command:
81                command = tool.command_by_name(options.parent_command)
82                command.handle_script_error(tool, state, e)
83            QueueEngine.exit_after_handled_error(e)
84