1# Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) 2# 3# Redistribution and use in source and binary forms, with or without 4# modification, are permitted provided that the following conditions 5# are met: 6# 1. Redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer. 8# 2. Redistributions in binary form must reproduce the above copyright 9# notice, this list of conditions and the following disclaimer in the 10# documentation and/or other materials provided with the distribution. 11# 12# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND 13# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR 16# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 18# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 19# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 20# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 21# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 23import codecs 24import logging 25import sys 26 27import webkitpy.style.checker as checker 28from webkitpy.style.patchreader import PatchReader 29from webkitpy.style.checker import StyleProcessor 30from webkitpy.style.filereader import TextFileReader 31from webkitpy.common.host import Host 32 33 34_log = logging.getLogger(__name__) 35 36 37def change_directory(filesystem, checkout_root, paths): 38 """Change the working directory to the WebKit checkout root, if possible. 39 40 If every path in the paths parameter is below the checkout root (or if 41 the paths parameter is empty or None), this method changes the current 42 working directory to the checkout root and converts the paths parameter 43 as described below. 44 This allows the paths being checked to be displayed relative to the 45 checkout root, and for path-specific style checks to work as expected. 46 Path-specific checks include whether files should be skipped, whether 47 custom style rules should apply to certain files, etc. 48 49 Returns: 50 paths: A copy of the paths parameter -- possibly converted, as follows. 51 If this method changed the current working directory to the 52 checkout root, then the list is the paths parameter converted to 53 normalized paths relative to the checkout root. 54 55 Args: 56 paths: A list of paths to the files that should be checked for style. 57 This argument can be None or the empty list if a git commit 58 or all changes under the checkout root should be checked. 59 checkout_root: The path to the root of the WebKit checkout. 60 61 """ 62 if paths is not None: 63 paths = list(paths) 64 65 if paths: 66 # Then try converting all of the paths to paths relative to 67 # the checkout root. 68 rel_paths = [] 69 for path in paths: 70 rel_path = filesystem.relpath(path, checkout_root) 71 if rel_path.startswith(filesystem.pardir): 72 # Then the path is not below the checkout root. Since all 73 # paths should be interpreted relative to the same root, 74 # do not interpret any of the paths as relative to the 75 # checkout root. Interpret all of them relative to the 76 # current working directory, and do not change the current 77 # working directory. 78 _log.warn( 79"""Path-dependent style checks may not work correctly: 80 81 One of the given paths is outside the WebKit checkout of the current 82 working directory: 83 84 Path: %s 85 Checkout root: %s 86 87 Pass only files below the checkout root to ensure correct results. 88 See the help documentation for more info. 89""" 90 % (path, checkout_root)) 91 92 return paths 93 rel_paths.append(rel_path) 94 # If we got here, the conversion was successful. 95 paths = rel_paths 96 97 _log.debug("Changing to checkout root: " + checkout_root) 98 filesystem.chdir(checkout_root) 99 100 return paths 101 102 103class CheckWebKitStyle(object): 104 def _engage_awesome_stderr_hacks(self): 105 # Change stderr to write with replacement characters so we don't die 106 # if we try to print something containing non-ASCII characters. 107 stderr = codecs.StreamReaderWriter(sys.stderr, 108 codecs.getreader('utf8'), 109 codecs.getwriter('utf8'), 110 'replace') 111 # Setting an "encoding" attribute on the stream is necessary to 112 # prevent the logging module from raising an error. See 113 # the checker.configure_logging() function for more information. 114 stderr.encoding = "UTF-8" 115 116 # FIXME: Change webkitpy.style so that we do not need to overwrite 117 # the global sys.stderr. This involves updating the code to 118 # accept a stream parameter where necessary, and not calling 119 # sys.stderr explicitly anywhere. 120 sys.stderr = stderr 121 return stderr 122 123 def main(self): 124 args = sys.argv[1:] 125 126 host = Host() 127 host.initialize_scm() 128 129 stderr = self._engage_awesome_stderr_hacks() 130 131 # Checking for the verbose flag before calling check_webkit_style_parser() 132 # lets us enable verbose logging earlier. 133 is_verbose = "-v" in args or "--verbose" in args 134 135 checker.configure_logging(stream=stderr, is_verbose=is_verbose) 136 _log.debug("Verbose logging enabled.") 137 138 parser = checker.check_webkit_style_parser() 139 (paths, options) = parser.parse(args) 140 141 configuration = checker.check_webkit_style_configuration(options) 142 143 paths = change_directory(host.filesystem, checkout_root=host.scm().checkout_root, paths=paths) 144 145 style_processor = StyleProcessor(configuration) 146 file_reader = TextFileReader(host.filesystem, style_processor) 147 148 if paths and not options.diff_files: 149 file_reader.process_paths(paths) 150 else: 151 changed_files = paths if options.diff_files else None 152 patch = host.scm().create_patch(options.git_commit, changed_files=changed_files) 153 patch_checker = PatchReader(file_reader) 154 patch_checker.check(patch) 155 156 error_count = style_processor.error_count 157 file_count = file_reader.file_count 158 delete_only_file_count = file_reader.delete_only_file_count 159 160 _log.info("Total errors found: %d in %d files" % (error_count, file_count)) 161 # We fail when style errors are found or there are no checked files. 162 return error_count > 0 or (file_count == 0 and delete_only_file_count == 0) 163