1d0825bca7fe65beaee391d30da42e937db621564Steve Block# Copyright (C) 2010 Google Inc. All rights reserved.
2d0825bca7fe65beaee391d30da42e937db621564Steve Block#
3231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# Redistribution and use in source and binary forms, with or without
4231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# modification, are permitted provided that the following conditions are
5231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# met:
6231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#
7231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#     * Redistributions of source code must retain the above copyright
8231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# notice, this list of conditions and the following disclaimer.
9231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#     * Redistributions in binary form must reproduce the above
10231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# copyright notice, this list of conditions and the following disclaimer
11231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# in the documentation and/or other materials provided with the
12231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# distribution.
13231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#     * Neither the name of Google Inc. nor the names of its
14231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# contributors may be used to endorse or promote products derived from
15231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# this software without specific prior written permission.
16231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#
17231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
29d0825bca7fe65beaee391d30da42e937db621564Steve Blockimport os
30231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
31dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.common.system.executive import ScriptError
32dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.tool.steps.abstractstep import AbstractStep
33dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.tool.steps.options import Options
34dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.common.system.deprecated_logging import error
35231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
36d0825bca7fe65beaee391d30da42e937db621564Steve Blockclass CheckStyle(AbstractStep):
37d0825bca7fe65beaee391d30da42e937db621564Steve Block    @classmethod
38d0825bca7fe65beaee391d30da42e937db621564Steve Block    def options(cls):
3921939df44de1705786c545cd1bf519d47250322dBen Murdoch        return AbstractStep.options() + [
40d0825bca7fe65beaee391d30da42e937db621564Steve Block            Options.non_interactive,
41d0825bca7fe65beaee391d30da42e937db621564Steve Block            Options.check_style,
4221939df44de1705786c545cd1bf519d47250322dBen Murdoch            Options.git_commit,
43d0825bca7fe65beaee391d30da42e937db621564Steve Block        ]
44231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
45d0825bca7fe65beaee391d30da42e937db621564Steve Block    def run(self, state):
46d0825bca7fe65beaee391d30da42e937db621564Steve Block        if not self._options.check_style:
47d0825bca7fe65beaee391d30da42e937db621564Steve Block            return
48d0825bca7fe65beaee391d30da42e937db621564Steve Block        os.chdir(self._tool.scm().checkout_root)
4921939df44de1705786c545cd1bf519d47250322dBen Murdoch
506c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen        args = []
516c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen        if self._options.git_commit:
526c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen            args.append("--git-commit")
536c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen            args.append(self._options.git_commit)
546c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen
5528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        args.append("--diff-files")
5628040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        args.extend(self._changed_files(state))
5728040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
586c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen        try:
592daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch            self._tool.executive.run_and_throw_if_fail(self._tool.port().check_webkit_style_command() + args)
60d0825bca7fe65beaee391d30da42e937db621564Steve Block        except ScriptError, e:
61d0825bca7fe65beaee391d30da42e937db621564Steve Block            if self._options.non_interactive:
62d0825bca7fe65beaee391d30da42e937db621564Steve Block                # We need to re-raise the exception here to have the
63d0825bca7fe65beaee391d30da42e937db621564Steve Block                # style-queue do the right thing.
64d0825bca7fe65beaee391d30da42e937db621564Steve Block                raise e
65d0825bca7fe65beaee391d30da42e937db621564Steve Block            if not self._tool.user.confirm("Are you sure you want to continue?"):
66d0825bca7fe65beaee391d30da42e937db621564Steve Block                exit(1)
67