Home | History | Annotate | Download | only in Scripts
      1 #!/usr/bin/env python
      2 #
      3 # Copyright (C) 2009 Google Inc. All rights reserved.
      4 # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek (at] gmail.com)
      5 #
      6 # Redistribution and use in source and binary forms, with or without
      7 # modification, are permitted provided that the following conditions are
      8 # met:
      9 #
     10 #    * Redistributions of source code must retain the above copyright
     11 # notice, this list of conditions and the following disclaimer.
     12 #    * Redistributions in binary form must reproduce the above
     13 # copyright notice, this list of conditions and the following disclaimer
     14 # in the documentation and/or other materials provided with the
     15 # distribution.
     16 #    * Neither the name of Google Inc. nor the names of its
     17 # contributors may be used to endorse or promote products derived from
     18 # this software without specific prior written permission.
     19 #
     20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 
     32 """Does WebKit-lint on C/C++ or text files.
     33 
     34 The goal of this script is to identify places in the code that *may*
     35 be in non-compliance with WebKit style.  It does not attempt to fix
     36 up these problems -- the point is to educate.  It does also not
     37 attempt to find all problems, or to ensure that everything it does
     38 find is legitimately a problem.
     39 
     40 In particular, we can get very confused by /* and // inside strings!
     41 We do a small hack, which is to ignore //'s with "'s after them on the
     42 same line, but it is far from perfect (in either direction).
     43 """
     44 
     45 import codecs
     46 import logging
     47 import os
     48 import os.path
     49 import sys
     50 
     51 from webkitpy.style_references import detect_checkout
     52 import webkitpy.style.checker as checker
     53 from webkitpy.style.patchreader import PatchReader
     54 from webkitpy.style.checker import StyleProcessor
     55 from webkitpy.style.filereader import TextFileReader
     56 from webkitpy.style.main import change_directory
     57 
     58 _log = logging.getLogger("check-webkit-style")
     59 
     60 
     61 # FIXME: Move this code to style.main.
     62 def main():
     63     # Change stderr to write with replacement characters so we don't die
     64     # if we try to print something containing non-ASCII characters.
     65     stderr = codecs.StreamReaderWriter(sys.stderr,
     66                                        codecs.getreader('utf8'),
     67                                        codecs.getwriter('utf8'),
     68                                        'replace')
     69     # Setting an "encoding" attribute on the stream is necessary to
     70     # prevent the logging module from raising an error.  See
     71     # the checker.configure_logging() function for more information.
     72     stderr.encoding = "UTF-8"
     73 
     74     # FIXME: Change webkitpy.style so that we do not need to overwrite
     75     #        the global sys.stderr.  This involves updating the code to
     76     #        accept a stream parameter where necessary, and not calling
     77     #        sys.stderr explicitly anywhere.
     78     sys.stderr = stderr
     79 
     80     args = sys.argv[1:]
     81 
     82     # Checking for the verbose flag before calling check_webkit_style_parser()
     83     # lets us enable verbose logging earlier.
     84     is_verbose = "-v" in args or "--verbose" in args
     85 
     86     checker.configure_logging(stream=stderr, is_verbose=is_verbose)
     87     _log.debug("Verbose logging enabled.")
     88 
     89     parser = checker.check_webkit_style_parser()
     90     (paths, options) = parser.parse(args)
     91 
     92     checkout = detect_checkout()
     93 
     94     if checkout is None:
     95         if not paths:
     96             _log.error("WebKit checkout not found: You must run this script "
     97                        "from within a WebKit checkout if you are not passing "
     98                        "specific paths to check.")
     99             sys.exit(1)
    100 
    101         checkout_root = None
    102         _log.debug("WebKit checkout not found for current directory.")
    103     else:
    104         checkout_root = checkout.root_path()
    105         _log.debug("WebKit checkout found with root: %s" % checkout_root)
    106 
    107     configuration = checker.check_webkit_style_configuration(options)
    108 
    109     paths = change_directory(checkout_root=checkout_root, paths=paths)
    110 
    111     style_processor = StyleProcessor(configuration)
    112 
    113     file_reader = TextFileReader(style_processor)
    114 
    115     if paths and not options.diff_files:
    116         file_reader.process_paths(paths)
    117     else:
    118         changed_files = paths if options.diff_files else None
    119         patch = checkout.create_patch(options.git_commit, changed_files=changed_files)
    120         patch_checker = PatchReader(file_reader)
    121         patch_checker.check(patch)
    122 
    123     error_count = style_processor.error_count
    124     file_count = file_reader.file_count
    125     delete_only_file_count = file_reader.delete_only_file_count
    126 
    127     _log.info("Total errors found: %d in %d files"
    128               % (error_count, file_count))
    129     # We fail when style errors are found or there are no checked files.
    130     sys.exit(error_count > 0 or (file_count == 0 and delete_only_file_count == 0))
    131 
    132 
    133 if __name__ == "__main__":
    134     main()
    135