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