watchlistparser.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Copyright (C) 2011 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import difflib
  29. import logging
  30. import re
  31. from webkitpy.common.watchlist.amountchangedpattern import AmountChangedPattern
  32. from webkitpy.common.watchlist.changedlinepattern import ChangedLinePattern
  33. from webkitpy.common.watchlist.filenamepattern import FilenamePattern
  34. from webkitpy.common.watchlist.watchlist import WatchList
  35. from webkitpy.common.watchlist.watchlistrule import WatchListRule
  36. from webkitpy.common.config.committers import CommitterList
  37. _log = logging.getLogger(__name__)
  38. class WatchListParser(object):
  39. _DEFINITIONS = 'DEFINITIONS'
  40. _CC_RULES = 'CC_RULES'
  41. _MESSAGE_RULES = 'MESSAGE_RULES'
  42. _INVALID_DEFINITION_NAME_REGEX = r'\|'
  43. def __init__(self, log_error=None):
  44. self._log_error = log_error or _log.error
  45. self._section_parsers = {
  46. self._DEFINITIONS: self._parse_definition_section,
  47. self._CC_RULES: self._parse_cc_rules,
  48. self._MESSAGE_RULES: self._parse_message_rules,
  49. }
  50. self._definition_pattern_parsers = {
  51. 'filename': FilenamePattern,
  52. 'in_added_lines': (lambda compiled_regex: ChangedLinePattern(compiled_regex, 0)),
  53. 'in_deleted_lines': (lambda compiled_regex: ChangedLinePattern(compiled_regex, 1)),
  54. 'less': (lambda compiled_regex: AmountChangedPattern(compiled_regex, 1)),
  55. 'more': (lambda compiled_regex: AmountChangedPattern(compiled_regex, 0)),
  56. }
  57. def parse(self, watch_list_contents):
  58. watch_list = WatchList()
  59. # Change the watch list text into a dictionary.
  60. dictionary = self._eval_watch_list(watch_list_contents)
  61. # Parse the top level sections in the watch list.
  62. for section in dictionary:
  63. parser = self._section_parsers.get(section)
  64. if not parser:
  65. self._log_error(('Unknown section "%s" in watch list.'
  66. + self._suggest_words(section, self._section_parsers.keys()))
  67. % section)
  68. continue
  69. parser(dictionary[section], watch_list)
  70. self._validate(watch_list)
  71. return watch_list
  72. def _eval_watch_list(self, watch_list_contents):
  73. return eval(watch_list_contents, {'__builtins__': None}, None)
  74. def _suggest_words(self, invalid_word, valid_words):
  75. close_matches = difflib.get_close_matches(invalid_word, valid_words)
  76. if not close_matches:
  77. return ''
  78. return '\n\nPerhaps it should be %s.' % (' or '.join(close_matches))
  79. def _parse_definition_section(self, definition_section, watch_list):
  80. definitions = {}
  81. for name in definition_section:
  82. invalid_character = re.search(self._INVALID_DEFINITION_NAME_REGEX, name)
  83. if invalid_character:
  84. self._log_error('Invalid character "%s" in definition "%s".' % (invalid_character.group(0), name))
  85. continue
  86. definition = definition_section[name]
  87. definitions[name] = []
  88. for pattern_type in definition:
  89. pattern_parser = self._definition_pattern_parsers.get(pattern_type)
  90. if not pattern_parser:
  91. self._log_error(('Unknown pattern type "%s" in definition "%s".'
  92. + self._suggest_words(pattern_type, self._definition_pattern_parsers.keys()))
  93. % (pattern_type, name))
  94. continue
  95. try:
  96. compiled_regex = re.compile(definition[pattern_type])
  97. except Exception, e:
  98. self._log_error('The regex "%s" is invalid due to "%s".' % (definition[pattern_type], str(e)))
  99. continue
  100. pattern = pattern_parser(compiled_regex)
  101. definitions[name].append(pattern)
  102. if not definitions[name]:
  103. self._log_error('The definition "%s" has no patterns, so it should be deleted.' % name)
  104. continue
  105. watch_list.definitions = definitions
  106. def _parse_rules(self, rules_section):
  107. rules = []
  108. for complex_definition in rules_section:
  109. instructions = rules_section[complex_definition]
  110. if not instructions:
  111. self._log_error('A rule for definition "%s" is empty, so it should be deleted.' % complex_definition)
  112. continue
  113. rules.append(WatchListRule(complex_definition, instructions))
  114. return rules
  115. def _parse_cc_rules(self, cc_section, watch_list):
  116. watch_list.cc_rules = self._parse_rules(cc_section)
  117. def _parse_message_rules(self, message_section, watch_list):
  118. watch_list.message_rules = self._parse_rules(message_section)
  119. def _validate(self, watch_list):
  120. cc_definitions_set = self._rule_definitions_as_set(watch_list.cc_rules)
  121. messages_definitions_set = self._rule_definitions_as_set(watch_list.message_rules)
  122. self._verify_all_definitions_are_used(watch_list, cc_definitions_set.union(messages_definitions_set))
  123. self._validate_definitions(cc_definitions_set, self._CC_RULES, watch_list)
  124. self._validate_definitions(messages_definitions_set, self._MESSAGE_RULES, watch_list)
  125. accounts = CommitterList()
  126. for cc_rule in watch_list.cc_rules:
  127. # Copy the instructions since we'll be remove items from the original list and
  128. # modifying a list while iterating through it leads to undefined behavior.
  129. intructions_copy = cc_rule.instructions()[:]
  130. for email in intructions_copy:
  131. if not accounts.contributor_by_email(email):
  132. cc_rule.remove_instruction(email)
  133. self._log_error("The email alias %s which is in the watchlist is not listed as a contributor in committers.py" % email)
  134. continue
  135. def _verify_all_definitions_are_used(self, watch_list, used_definitions):
  136. definitions_not_used = set(watch_list.definitions.keys())
  137. definitions_not_used.difference_update(used_definitions)
  138. if definitions_not_used:
  139. self._log_error('The following definitions are not used and should be removed: %s' % (', '.join(definitions_not_used)))
  140. def _validate_definitions(self, definitions, rules_section_name, watch_list):
  141. declared_definitions = watch_list.definitions.keys()
  142. definition_set = set(definitions)
  143. definition_set.difference_update(declared_definitions)
  144. if definition_set:
  145. suggestions = ''
  146. if len(definition_set) == 1:
  147. suggestions = self._suggest_words(set().union(definition_set).pop(), declared_definitions)
  148. self._log_error('In section "%s", the following definitions are not used and should be removed: %s%s' % (rules_section_name, ', '.join(definition_set), suggestions))
  149. def _rule_definitions_as_set(self, rules):
  150. definition_set = set()
  151. for rule in rules:
  152. definition_set = definition_set.union(rule.definitions_to_match)
  153. return definition_set