python.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. """Supports checking WebKit style in Python files."""
  23. import re
  24. from StringIO import StringIO
  25. from webkitpy.common.system.filesystem import FileSystem
  26. from webkitpy.common.webkit_finder import WebKitFinder
  27. from webkitpy.thirdparty.autoinstalled import pep8
  28. from webkitpy.thirdparty.autoinstalled.pylint import lint
  29. from webkitpy.thirdparty.autoinstalled.pylint.reporters.text import ParseableTextReporter
  30. class PythonChecker(object):
  31. """Processes text lines for checking style."""
  32. def __init__(self, file_path, handle_style_error):
  33. self._file_path = file_path
  34. self._handle_style_error = handle_style_error
  35. def check(self, lines):
  36. self._check_pep8(lines)
  37. self._check_pylint(lines)
  38. def _check_pep8(self, lines):
  39. # Initialize pep8.options, which is necessary for
  40. # Checker.check_all() to execute.
  41. pep8.process_options(arglist=[self._file_path])
  42. pep8_checker = pep8.Checker(self._file_path)
  43. def _pep8_handle_error(line_number, offset, text, check):
  44. # FIXME: Incorporate the character offset into the error output.
  45. # This will require updating the error handler __call__
  46. # signature to include an optional "offset" parameter.
  47. pep8_code = text[:4]
  48. pep8_message = text[5:]
  49. category = "pep8/" + pep8_code
  50. self._handle_style_error(line_number, category, 5, pep8_message)
  51. pep8_checker.report_error = _pep8_handle_error
  52. pep8_errors = pep8_checker.check_all()
  53. def _check_pylint(self, lines):
  54. pylinter = Pylinter()
  55. # FIXME: for now, we only report pylint errors, but we should be catching and
  56. # filtering warnings using the rules in style/checker.py instead.
  57. output = pylinter.run(['-E', self._file_path])
  58. lint_regex = re.compile('([^:]+):([^:]+): \[([^]]+)\] (.*)')
  59. for error in output.getvalue().splitlines():
  60. match_obj = lint_regex.match(error)
  61. assert(match_obj)
  62. line_number = int(match_obj.group(2))
  63. category_and_method = match_obj.group(3).split(', ')
  64. category = 'pylint/' + (category_and_method[0])
  65. if len(category_and_method) > 1:
  66. message = '[%s] %s' % (category_and_method[1], match_obj.group(4))
  67. else:
  68. message = match_obj.group(4)
  69. self._handle_style_error(line_number, category, 5, message)
  70. class Pylinter(object):
  71. # We filter out these messages because they are bugs in pylint that produce false positives.
  72. # FIXME: Does it make sense to combine these rules with the rules in style/checker.py somehow?
  73. FALSE_POSITIVES = [
  74. # possibly http://www.logilab.org/ticket/98613 ?
  75. "Instance of 'Popen' has no 'poll' member",
  76. "Instance of 'Popen' has no 'returncode' member",
  77. "Instance of 'Popen' has no 'stdin' member",
  78. "Instance of 'Popen' has no 'stdout' member",
  79. "Instance of 'Popen' has no 'stderr' member",
  80. "Instance of 'Popen' has no 'wait' member",
  81. ]
  82. def __init__(self):
  83. self._pylintrc = WebKitFinder(FileSystem()).path_from_webkit_base('Tools', 'Scripts', 'webkitpy', 'pylintrc')
  84. def run(self, argv):
  85. output = _FilteredStringIO(self.FALSE_POSITIVES)
  86. lint.Run(['--rcfile', self._pylintrc] + argv, reporter=ParseableTextReporter(output=output), exit=False)
  87. return output
  88. class _FilteredStringIO(StringIO):
  89. def __init__(self, bad_messages):
  90. StringIO.__init__(self)
  91. self.dropped_last_msg = False
  92. self.bad_messages = bad_messages
  93. def write(self, msg=''):
  94. if not self._filter(msg):
  95. StringIO.write(self, msg)
  96. def _filter(self, msg):
  97. if any(bad_message in msg for bad_message in self.bad_messages):
  98. self.dropped_last_msg = True
  99. return True
  100. if self.dropped_last_msg:
  101. # We drop the newline after a dropped message as well.
  102. self.dropped_last_msg = False
  103. if msg == '\n':
  104. return True
  105. return False