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 os
     47 import os.path
     48 import sys
     49 
     50 import webkitpy.style.checker as checker
     51 from webkitpy.style_references import SimpleScm
     52 
     53 def main():
     54     # Change stderr to write with replacement characters so we don't die
     55     # if we try to print something containing non-ASCII characters.
     56     sys.stderr = codecs.StreamReaderWriter(sys.stderr,
     57                                            codecs.getreader('utf8'),
     58                                            codecs.getwriter('utf8'),
     59                                            'replace')
     60 
     61     defaults = checker.webkit_argument_defaults()
     62 
     63     parser = checker.ArgumentParser(defaults)
     64     (files, options) = parser.parse(sys.argv[1:])
     65 
     66     style_checker = checker.StyleChecker(options)
     67 
     68     if files:
     69         for filename in files:
     70             style_checker.check_file(filename)
     71 
     72     else:
     73         scm = SimpleScm()
     74 
     75         os.chdir(scm.checkout_root())
     76 
     77         if options.git_commit:
     78             commit = options.git_commit
     79             if '..' in commit:
     80                 # FIXME: If the range is a "...", the code should find the common ancestor and
     81                 # start there (see git diff --help for information about how ... usually works).
     82                 commit = commit[:commit.find('..')]
     83                 print >> sys.stderr, "Warning: Ranges are not supported for --git-commit. Checking all changes since %s.\n" % commit
     84             patch = scm.create_patch_since_local_commit(commit)
     85         else:
     86             patch = scm.create_patch()
     87         style_checker.check_patch(patch)
     88 
     89     error_count = style_checker.error_count
     90     sys.stderr.write('Total errors found: %d\n' % error_count)
     91     sys.exit(error_count > 0)
     92 
     93 
     94 if __name__ == "__main__":
     95     main()
     96