main.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. import codecs
  23. import logging
  24. import sys
  25. import webkitpy.style.checker as checker
  26. from webkitpy.style.patchreader import PatchReader
  27. from webkitpy.style.checker import StyleProcessor
  28. from webkitpy.style.filereader import TextFileReader
  29. from webkitpy.common.host import Host
  30. _log = logging.getLogger(__name__)
  31. def change_directory(filesystem, checkout_root, paths):
  32. """Change the working directory to the WebKit checkout root, if possible.
  33. If every path in the paths parameter is below the checkout root (or if
  34. the paths parameter is empty or None), this method changes the current
  35. working directory to the checkout root and converts the paths parameter
  36. as described below.
  37. This allows the paths being checked to be displayed relative to the
  38. checkout root, and for path-specific style checks to work as expected.
  39. Path-specific checks include whether files should be skipped, whether
  40. custom style rules should apply to certain files, etc.
  41. Returns:
  42. paths: A copy of the paths parameter -- possibly converted, as follows.
  43. If this method changed the current working directory to the
  44. checkout root, then the list is the paths parameter converted to
  45. normalized paths relative to the checkout root.
  46. Args:
  47. paths: A list of paths to the files that should be checked for style.
  48. This argument can be None or the empty list if a git commit
  49. or all changes under the checkout root should be checked.
  50. checkout_root: The path to the root of the WebKit checkout.
  51. """
  52. if paths is not None:
  53. paths = list(paths)
  54. if paths:
  55. # Then try converting all of the paths to paths relative to
  56. # the checkout root.
  57. rel_paths = []
  58. for path in paths:
  59. rel_path = filesystem.relpath(path, checkout_root)
  60. if rel_path.startswith(filesystem.pardir):
  61. # Then the path is not below the checkout root. Since all
  62. # paths should be interpreted relative to the same root,
  63. # do not interpret any of the paths as relative to the
  64. # checkout root. Interpret all of them relative to the
  65. # current working directory, and do not change the current
  66. # working directory.
  67. _log.warn(
  68. """Path-dependent style checks may not work correctly:
  69. One of the given paths is outside the WebKit checkout of the current
  70. working directory:
  71. Path: %s
  72. Checkout root: %s
  73. Pass only files below the checkout root to ensure correct results.
  74. See the help documentation for more info.
  75. """
  76. % (path, checkout_root))
  77. return paths
  78. rel_paths.append(rel_path)
  79. # If we got here, the conversion was successful.
  80. paths = rel_paths
  81. _log.debug("Changing to checkout root: " + checkout_root)
  82. filesystem.chdir(checkout_root)
  83. return paths
  84. class CheckWebKitStyle(object):
  85. def _engage_awesome_stderr_hacks(self):
  86. # Change stderr to write with replacement characters so we don't die
  87. # if we try to print something containing non-ASCII characters.
  88. stderr = codecs.StreamReaderWriter(sys.stderr,
  89. codecs.getreader('utf8'),
  90. codecs.getwriter('utf8'),
  91. 'replace')
  92. # Setting an "encoding" attribute on the stream is necessary to
  93. # prevent the logging module from raising an error. See
  94. # the checker.configure_logging() function for more information.
  95. stderr.encoding = "UTF-8"
  96. # FIXME: Change webkitpy.style so that we do not need to overwrite
  97. # the global sys.stderr. This involves updating the code to
  98. # accept a stream parameter where necessary, and not calling
  99. # sys.stderr explicitly anywhere.
  100. sys.stderr = stderr
  101. return stderr
  102. def main(self):
  103. args = sys.argv[1:]
  104. host = Host()
  105. host.initialize_scm()
  106. stderr = self._engage_awesome_stderr_hacks()
  107. # Checking for the verbose flag before calling check_webkit_style_parser()
  108. # lets us enable verbose logging earlier.
  109. is_verbose = "-v" in args or "--verbose" in args
  110. checker.configure_logging(stream=stderr, is_verbose=is_verbose)
  111. _log.debug("Verbose logging enabled.")
  112. parser = checker.check_webkit_style_parser()
  113. (paths, options) = parser.parse(args)
  114. configuration = checker.check_webkit_style_configuration(options)
  115. paths = change_directory(host.filesystem, checkout_root=host.scm().checkout_root, paths=paths)
  116. style_processor = StyleProcessor(configuration)
  117. file_reader = TextFileReader(host.filesystem, style_processor)
  118. if paths and not options.diff_files:
  119. file_reader.process_paths(paths)
  120. else:
  121. changed_files = paths if options.diff_files else None
  122. patch = host.scm().create_patch(options.git_commit, changed_files=changed_files)
  123. patch_checker = PatchReader(file_reader)
  124. patch_checker.check(patch)
  125. error_count = style_processor.error_count
  126. file_count = file_reader.file_count
  127. delete_only_file_count = file_reader.delete_only_file_count
  128. _log.info("Total errors found: %d in %d files" % (error_count, file_count))
  129. # We fail when style errors are found or there are no checked files.
  130. return error_count > 0 or (file_count == 0 and delete_only_file_count == 0)