initial import
[vuplus_webkit] / Tools / Scripts / check-webkit-style
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2009 Google Inc. All rights reserved.
4 # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@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.common.checkout.scm import detect_scm_system
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     cwd = os.path.abspath(os.curdir)
93     scm = detect_scm_system(cwd)
94
95     if scm is None:
96         if not paths:
97             _log.error("WebKit checkout not found: You must run this script "
98                        "from within a WebKit checkout if you are not passing "
99                        "specific paths to check.")
100             sys.exit(1)
101
102         checkout_root = None
103         _log.debug("WebKit checkout not found for current directory.")
104     else:
105         checkout_root = scm.checkout_root
106         _log.debug("WebKit checkout found with root: %s" % checkout_root)
107
108     configuration = checker.check_webkit_style_configuration(options)
109
110     paths = change_directory(checkout_root=checkout_root, paths=paths)
111
112     style_processor = StyleProcessor(configuration)
113
114     file_reader = TextFileReader(style_processor)
115
116     if paths and not options.diff_files:
117         file_reader.process_paths(paths)
118     else:
119         changed_files = paths if options.diff_files else None
120         patch = scm.create_patch(options.git_commit, changed_files=changed_files)
121         patch_checker = PatchReader(file_reader)
122         patch_checker.check(patch)
123
124     error_count = style_processor.error_count
125     file_count = file_reader.file_count
126     delete_only_file_count = file_reader.delete_only_file_count
127
128     _log.info("Total errors found: %d in %d files"
129               % (error_count, file_count))
130     # We fail when style errors are found or there are no checked files.
131     sys.exit(error_count > 0 or (file_count == 0 and delete_only_file_count == 0))
132
133
134 if __name__ == "__main__":
135     main()