4 # Copyright (C) 2011, 2012, 2013 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script to check NEWS file.
26 # pylint: disable=C0103
27 # [C0103] Invalid name
38 DASHES_RE = re.compile(r"^\s*-+\s*$")
39 RELEASED_RE = re.compile(r"^\*\(Released (?P<day>[A-Z][a-z]{2}),"
40 r" (?P<date>.+)\)\*$")
41 UNRELEASED_RE = re.compile(r"^\*\(unreleased\)\*$")
42 VERSION_RE = re.compile(r"^Version (\d+(\.\d+)+( (alpha|beta|rc)\d+)?)$")
44 #: How many days release timestamps may be in the future
45 TIMESTAMP_FUTURE_DAYS_MAX = 3
51 """Log an error for later display.
57 def ReqNLines(req, count_empty, lineno, line):
58 """Check if we have N empty lines before the current one.
62 Error("Line %s: Missing empty line(s) before %s,"
63 " %d needed but got only %d" %
64 (lineno, line, req, count_empty))
66 Error("Line %s: Too many empty lines before %s,"
67 " %d needed but got %d" %
68 (lineno, line, req, count_empty))
71 def IsAlphaVersion(version):
72 return "alpha" in version
75 def UpdateAllowUnreleased(allow_unreleased, version_match, release):
76 if not allow_unreleased:
78 if IsAlphaVersion(release):
80 version = version_match.group(1)
81 if version == release:
87 # Ensure "C" locale is used
88 curlocale = locale.getlocale()
89 if curlocale != (None, None):
90 Error("Invalid locale %s" % curlocale)
92 # Get the release version, but replace "~" with " " as the version
93 # in the NEWS file uses spaces for beta and rc releases.
94 release = os.environ.get('RELEASE', "").replace("~", " ")
99 allow_unreleased = True
100 found_versions = set()
102 for line in fileinput.input():
103 line = line.rstrip("\n")
105 version_match = VERSION_RE.match(line)
107 ReqNLines(2, count_empty, fileinput.filelineno(), line)
108 version = version_match.group(1)
109 if version in found_versions:
110 Error("Line %s: Duplicate release %s found" %
111 (fileinput.filelineno(), version))
112 found_versions.add(version)
113 allow_unreleased = UpdateAllowUnreleased(allow_unreleased, version_match,
116 unreleased_match = UNRELEASED_RE.match(line)
117 if unreleased_match and not allow_unreleased:
118 Error("Line %s: Unreleased version after current release %s" %
119 (fileinput.filelineno(), release))
121 if unreleased_match or RELEASED_RE.match(line):
122 ReqNLines(1, count_empty, fileinput.filelineno(), line)
129 if DASHES_RE.match(line):
130 if not VERSION_RE.match(prevline):
131 Error("Line %s: Invalid title" %
132 (fileinput.filelineno() - 1))
133 if len(line) != len(prevline):
134 Error("Line %s: Invalid dashes length" %
135 (fileinput.filelineno()))
143 if UNRELEASED_RE.match(line):
144 # Ignore unreleased versions
148 m = RELEASED_RE.match(line)
150 Error("Line %s: Invalid release line" % fileinput.filelineno())
154 # Including the weekday in the date string does not work as time.strptime
155 # would return an inconsistent result if the weekday is incorrect.
156 parsed_ts = time.mktime(time.strptime(m.group("date"), "%d %b %Y"))
157 parsed = datetime.date.fromtimestamp(parsed_ts)
158 today = datetime.date.today()
160 if (parsed - datetime.timedelta(TIMESTAMP_FUTURE_DAYS_MAX)) > today:
161 Error("Line %s: %s is more than %s days in the future (today is %s)" %
162 (fileinput.filelineno(), parsed, TIMESTAMP_FUTURE_DAYS_MAX,
165 weekday = parsed.strftime("%a")
168 if m.group("day") != weekday:
169 Error("Line %s: %s was/is a %s, not %s" %
170 (fileinput.filelineno(), parsed, weekday,
179 print >> sys.stderr, msg
185 if __name__ == "__main__":