1643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# Copyright (C) 2009 Google Inc. All rights reserved.
2643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#
3643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# Redistribution and use in source and binary forms, with or without
4643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# modification, are permitted provided that the following conditions are
5643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# met:
6643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#
7643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#     * Redistributions of source code must retain the above copyright
8643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# notice, this list of conditions and the following disclaimer.
9643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#     * Redistributions in binary form must reproduce the above
10643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# copyright notice, this list of conditions and the following disclaimer
11643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# in the documentation and/or other materials provided with the
12643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# distribution.
13643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#     * Neither the name of Google Inc. nor the names of its
14643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# contributors may be used to endorse or promote products derived from
15643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# this software without specific prior written permission.
16643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#
17643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
292fc2651226baac27029e38c9d6ef883fa32084dbSteve Blockfrom webkitpy.tool import steps
30d0825bca7fe65beaee391d30da42e937db621564Steve Block
31dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.common.checkout.scm import CheckoutNeedsUpdate
32dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.common.system.deprecated_logging import log
332fc2651226baac27029e38c9d6ef883fa32084dbSteve Blockfrom webkitpy.common.system.executive import ScriptError
342fc2651226baac27029e38c9d6ef883fa32084dbSteve Blockfrom webkitpy.tool.bot.queueengine import QueueEngine
35d0825bca7fe65beaee391d30da42e937db621564Steve Block
36d0825bca7fe65beaee391d30da42e937db621564Steve Block
37d0825bca7fe65beaee391d30da42e937db621564Steve Blockclass StepSequenceErrorHandler():
38d0825bca7fe65beaee391d30da42e937db621564Steve Block    @classmethod
39d0825bca7fe65beaee391d30da42e937db621564Steve Block    def handle_script_error(cls, tool, patch, script_error):
40d0825bca7fe65beaee391d30da42e937db621564Steve Block        raise NotImplementedError, "subclasses must implement"
41643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
42e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    @classmethod
43e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    def handle_checkout_needs_update(cls, tool, state, options, error):
44e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        raise NotImplementedError, "subclasses must implement"
45e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
46643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
47643ca7872b450ea4efacab6188849e5aac2ba161Steve Blockclass StepSequence(object):
48643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    def __init__(self, steps):
49d0825bca7fe65beaee391d30da42e937db621564Steve Block        self._steps = steps or []
50643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
51643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    def options(self):
52643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        collected_options = [
53d0825bca7fe65beaee391d30da42e937db621564Steve Block            steps.Options.parent_command,
54d0825bca7fe65beaee391d30da42e937db621564Steve Block            steps.Options.quiet,
55643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        ]
56643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        for step in self._steps:
57643ca7872b450ea4efacab6188849e5aac2ba161Steve Block            collected_options = collected_options + step.options()
58643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        # Remove duplicates.
59643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        collected_options = sorted(set(collected_options))
60643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        return collected_options
61643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
62d0825bca7fe65beaee391d30da42e937db621564Steve Block    def _run(self, tool, options, state):
63643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        for step in self._steps:
64d0825bca7fe65beaee391d30da42e937db621564Steve Block            step(tool, options).run(state)
65643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
66d0825bca7fe65beaee391d30da42e937db621564Steve Block    def run_and_handle_errors(self, tool, options, state=None):
67d0825bca7fe65beaee391d30da42e937db621564Steve Block        if not state:
68d0825bca7fe65beaee391d30da42e937db621564Steve Block            state = {}
69643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        try:
70d0825bca7fe65beaee391d30da42e937db621564Steve Block            self._run(tool, options, state)
71643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        except CheckoutNeedsUpdate, e:
72643ca7872b450ea4efacab6188849e5aac2ba161Steve Block            log("Commit failed because the checkout is out of date.  Please update and try again.")
73e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            if options.parent_command:
74e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke                command = tool.command_by_name(options.parent_command)
75e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke                command.handle_checkout_needs_update(tool, state, options, e)
76d0825bca7fe65beaee391d30da42e937db621564Steve Block            QueueEngine.exit_after_handled_error(e)
77643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        except ScriptError, e:
78643ca7872b450ea4efacab6188849e5aac2ba161Steve Block            if not options.quiet:
79643ca7872b450ea4efacab6188849e5aac2ba161Steve Block                log(e.message_with_output())
80643ca7872b450ea4efacab6188849e5aac2ba161Steve Block            if options.parent_command:
81643ca7872b450ea4efacab6188849e5aac2ba161Steve Block                command = tool.command_by_name(options.parent_command)
82d0825bca7fe65beaee391d30da42e937db621564Steve Block                command.handle_script_error(tool, state, e)
83d0825bca7fe65beaee391d30da42e937db621564Steve Block            QueueEngine.exit_after_handled_error(e)
84