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+)+( (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.
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))
72 # Ensure "C" locale is used
73 curlocale = locale.getlocale()
74 if curlocale != (None, None):
75 Error("Invalid locale %s" % curlocale)
77 # Get the release version, but replace "~" with " " as the version
78 # in the NEWS file uses spaces for beta and rc releases.
79 release = os.environ.get('RELEASE', "").replace("~", " ")
84 allow_unreleased = True
85 found_versions = set()
87 for line in fileinput.input():
88 line = line.rstrip("\n")
90 version_match = VERSION_RE.match(line)
92 ReqNLines(2, count_empty, fileinput.filelineno(), line)
93 version = version_match.group(1)
94 if version in found_versions:
95 Error("Line %s: Duplicate release %s found" %
96 (fileinput.filelineno(), version))
97 found_versions.add(version)
98 if version == release:
99 allow_unreleased = False
101 unreleased_match = UNRELEASED_RE.match(line)
102 if unreleased_match and not allow_unreleased:
103 Error("Line %s: Unreleased version after current release %s" %
104 (fileinput.filelineno(), release))
106 if unreleased_match or RELEASED_RE.match(line):
107 ReqNLines(1, count_empty, fileinput.filelineno(), line)
114 if DASHES_RE.match(line):
115 if not VERSION_RE.match(prevline):
116 Error("Line %s: Invalid title" %
117 (fileinput.filelineno() - 1))
118 if len(line) != len(prevline):
119 Error("Line %s: Invalid dashes length" %
120 (fileinput.filelineno()))
128 if UNRELEASED_RE.match(line):
129 # Ignore unreleased versions
133 m = RELEASED_RE.match(line)
135 Error("Line %s: Invalid release line" % fileinput.filelineno())
139 # Including the weekday in the date string does not work as time.strptime
140 # would return an inconsistent result if the weekday is incorrect.
141 parsed_ts = time.mktime(time.strptime(m.group("date"), "%d %b %Y"))
142 parsed = datetime.date.fromtimestamp(parsed_ts)
143 today = datetime.date.today()
145 if (parsed - datetime.timedelta(TIMESTAMP_FUTURE_DAYS_MAX)) > today:
146 Error("Line %s: %s is more than %s days in the future (today is %s)" %
147 (fileinput.filelineno(), parsed, TIMESTAMP_FUTURE_DAYS_MAX,
150 weekday = parsed.strftime("%a")
153 if m.group("day") != weekday:
154 Error("Line %s: %s was/is a %s, not %s" %
155 (fileinput.filelineno(), parsed, weekday,
164 print >> sys.stderr, msg
170 if __name__ == "__main__":