PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/hooks/webkitpy/common/checkout/changelog.py

https://github.com/hwti/LunaSysMgr
Python | 239 lines | 147 code | 36 blank | 56 comment | 35 complexity | 17d04fe4eb886d5422b083078e4b9f38 MD5 | raw file
  1. # Copyright (C) 2009, 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. #
  29. # WebKit's Python module for parsing and modifying ChangeLog files
  30. import codecs
  31. import fileinput # inplace file editing for set_reviewer_in_changelog
  32. import os.path
  33. import re
  34. import textwrap
  35. from webkitpy.common.config.committers import CommitterList
  36. import webkitpy.common.config.urls as config_urls
  37. from webkitpy.common.system.deprecated_logging import log
  38. # FIXME: parse_bug_id should not be a free function.
  39. # FIXME: Where should this function live in the dependency graph?
  40. def parse_bug_id(message):
  41. if not message:
  42. return None
  43. match = re.search(config_urls.bug_url_short, message)
  44. if match:
  45. return int(match.group('bug_id'))
  46. match = re.search(config_urls.bug_url_long, message)
  47. if match:
  48. return int(match.group('bug_id'))
  49. return None
  50. # FIXME: parse_bug_id_from_changelog should not be a free function.
  51. # Parse the bug ID out of a Changelog message based on the format that is
  52. # used by prepare-ChangeLog
  53. def parse_bug_id_from_changelog(message):
  54. if not message:
  55. return None
  56. match = re.search("^\s*" + config_urls.bug_url_short + "$", message, re.MULTILINE)
  57. if match:
  58. return int(match.group('bug_id'))
  59. match = re.search("^\s*" + config_urls.bug_url_long + "$", message, re.MULTILINE)
  60. if match:
  61. return int(match.group('bug_id'))
  62. # We weren't able to find a bug URL in the format used by prepare-ChangeLog. Fall back to the
  63. # first bug URL found anywhere in the message.
  64. return parse_bug_id(message)
  65. class ChangeLogEntry(object):
  66. # e.g. 2009-06-03 Eric Seidel <eric@webkit.org>
  67. date_line_regexp = r'^(?P<date>\d{4}-\d{2}-\d{2})\s+(?P<name>.+?)\s+<(?P<email>[^<>]+)>$'
  68. # e.g. * Source/WebCore/page/EventHandler.cpp: Implement FooBarQuux.
  69. touched_files_regexp = r'\s*\*\s*(?P<file>.+)\:'
  70. # e.g. == Rolled over to ChangeLog-2011-02-16 ==
  71. rolled_over_regexp = r'^== Rolled over to ChangeLog-\d{4}-\d{2}-\d{2} ==$'
  72. def __init__(self, contents, committer_list=CommitterList()):
  73. self._contents = contents
  74. self._committer_list = committer_list
  75. self._parse_entry()
  76. def _parse_entry(self):
  77. match = re.match(self.date_line_regexp, self._contents, re.MULTILINE)
  78. if not match:
  79. log("WARNING: Creating invalid ChangeLogEntry:\n%s" % self._contents)
  80. # FIXME: group("name") does not seem to be Unicode? Probably due to self._contents not being unicode.
  81. self._author_name = match.group("name") if match else None
  82. self._author_email = match.group("email") if match else None
  83. match = re.search("^\s+Reviewed by (?P<reviewer>.*?)[\.,]?\s*$", self._contents, re.MULTILINE) # Discard everything after the first period
  84. self._reviewer_text = match.group("reviewer") if match else None
  85. self._reviewer = self._committer_list.committer_by_name(self._reviewer_text)
  86. self._author = self._committer_list.contributor_by_email(self._author_email) or self._committer_list.contributor_by_name(self._author_name)
  87. self._touched_files = re.findall(self.touched_files_regexp, self._contents, re.MULTILINE)
  88. def author_name(self):
  89. return self._author_name
  90. def author_email(self):
  91. return self._author_email
  92. def author(self):
  93. return self._author # Might be None
  94. # FIXME: Eventually we would like to map reviwer names to reviewer objects.
  95. # See https://bugs.webkit.org/show_bug.cgi?id=26533
  96. def reviewer_text(self):
  97. return self._reviewer_text
  98. def reviewer(self):
  99. return self._reviewer # Might be None, might also not be a Reviewer!
  100. def contents(self):
  101. return self._contents
  102. def bug_id(self):
  103. return parse_bug_id_from_changelog(self._contents)
  104. def touched_files(self):
  105. return self._touched_files
  106. # FIXME: Various methods on ChangeLog should move into ChangeLogEntry instead.
  107. class ChangeLog(object):
  108. def __init__(self, path):
  109. self.path = path
  110. _changelog_indent = " " * 8
  111. @staticmethod
  112. def parse_latest_entry_from_file(changelog_file):
  113. """changelog_file must be a file-like object which returns
  114. unicode strings. Use codecs.open or StringIO(unicode())
  115. to pass file objects to this class."""
  116. date_line_regexp = re.compile(ChangeLogEntry.date_line_regexp)
  117. rolled_over_regexp = re.compile(ChangeLogEntry.rolled_over_regexp)
  118. entry_lines = []
  119. # The first line should be a date line.
  120. first_line = changelog_file.readline()
  121. assert(isinstance(first_line, unicode))
  122. if not date_line_regexp.match(first_line):
  123. return None
  124. entry_lines.append(first_line)
  125. for line in changelog_file:
  126. # If we've hit the next entry, return.
  127. if date_line_regexp.match(line) or rolled_over_regexp.match(line):
  128. # Remove the extra newline at the end
  129. return ChangeLogEntry(''.join(entry_lines[:-1]))
  130. entry_lines.append(line)
  131. return None # We never found a date line!
  132. @staticmethod
  133. def parse_entries_from_file(changelog_file):
  134. """changelog_file must be a file-like object which returns
  135. unicode strings. Use codecs.open or StringIO(unicode())
  136. to pass file objects to this class."""
  137. date_line_regexp = re.compile(ChangeLogEntry.date_line_regexp)
  138. rolled_over_regexp = re.compile(ChangeLogEntry.rolled_over_regexp)
  139. entry_lines = []
  140. # The first line should be a date line.
  141. first_line = changelog_file.readline()
  142. assert(isinstance(first_line, unicode))
  143. if not date_line_regexp.match(first_line):
  144. raise StopIteration
  145. entry_lines.append(first_line)
  146. for line in changelog_file:
  147. if date_line_regexp.match(line) or rolled_over_regexp.match(line):
  148. # Remove the extra newline at the end
  149. yield ChangeLogEntry(''.join(entry_lines[:-1]))
  150. entry_lines = []
  151. entry_lines.append(line)
  152. def latest_entry(self):
  153. # ChangeLog files are always UTF-8, we read them in as such to support Reviewers with unicode in their names.
  154. changelog_file = codecs.open(self.path, "r", "utf-8")
  155. try:
  156. return self.parse_latest_entry_from_file(changelog_file)
  157. finally:
  158. changelog_file.close()
  159. # _wrap_line and _wrap_lines exist to work around
  160. # http://bugs.python.org/issue1859
  161. def _wrap_line(self, line):
  162. return textwrap.fill(line,
  163. width=70,
  164. initial_indent=self._changelog_indent,
  165. # Don't break urls which may be longer than width.
  166. break_long_words=False,
  167. subsequent_indent=self._changelog_indent)
  168. # Workaround as suggested by guido in
  169. # http://bugs.python.org/issue1859#msg60040
  170. def _wrap_lines(self, message):
  171. lines = [self._wrap_line(line) for line in message.splitlines()]
  172. return "\n".join(lines)
  173. def update_with_unreviewed_message(self, message):
  174. first_boilerplate_line_regexp = re.compile(
  175. "%sNeed a short description and bug URL \(OOPS!\)" % self._changelog_indent)
  176. removing_boilerplate = False
  177. # inplace=1 creates a backup file and re-directs stdout to the file
  178. for line in fileinput.FileInput(self.path, inplace=1):
  179. if first_boilerplate_line_regexp.search(line):
  180. message_lines = self._wrap_lines(message)
  181. print first_boilerplate_line_regexp.sub(message_lines, line),
  182. # Remove all the ChangeLog boilerplate before the first changed
  183. # file.
  184. removing_boilerplate = True
  185. elif removing_boilerplate:
  186. if line.find('*') >= 0: # each changed file is preceded by a *
  187. removing_boilerplate = False
  188. if not removing_boilerplate:
  189. print line,
  190. def set_reviewer(self, reviewer):
  191. # inplace=1 creates a backup file and re-directs stdout to the file
  192. for line in fileinput.FileInput(self.path, inplace=1):
  193. # Trailing comma suppresses printing newline
  194. print line.replace("NOBODY (OOPS!)", reviewer.encode("utf-8")),
  195. def set_short_description_and_bug_url(self, short_description, bug_url):
  196. message = "%s\n %s" % (short_description, bug_url)
  197. for line in fileinput.FileInput(self.path, inplace=1):
  198. print line.replace("Need a short description and bug URL (OOPS!)", message.encode("utf-8")),