4 # Copyright (C) 2011, 2012 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
37 DASHES_RE = re.compile(r"^\s*-+\s*$")
38 RELEASED_RE = re.compile(r"^\*\(Released (?P<day>[A-Z][a-z]{2}),"
39 r" (?P<date>.+)\)\*$")
40 UNRELEASED_RE = re.compile(r"^\*\(unreleased\)\*$")
41 VERSION_RE = re.compile(r"^Version \d+(\.\d+)+( (beta|rc)\d+)?$")
47 """Log an error for later display.
53 def ReqNLines(req, count_empty, lineno, line):
54 """Check if we have N empty lines.
58 Error("Line %s: Missing empty line(s) before %s,"
59 " %d needed but got only %d" %
60 (lineno, line, req, count_empty))
62 Error("Line %s: Too many empty lines before %s,"
63 " %d needed but got %d" %
64 (lineno, line, req, count_empty))
68 # Ensure "C" locale is used
69 curlocale = locale.getlocale()
70 if curlocale != (None, None):
71 Error("Invalid locale %s" % curlocale)
77 for line in fileinput.input():
78 line = line.rstrip("\n")
80 if VERSION_RE.match(line):
81 ReqNLines(2, count_empty, fileinput.filelineno(), line)
83 if UNRELEASED_RE.match(line) or RELEASED_RE.match(line):
84 ReqNLines(1, count_empty, fileinput.filelineno(), line)
91 if DASHES_RE.match(line):
92 if not VERSION_RE.match(prevline):
93 Error("Line %s: Invalid title" %
94 (fileinput.filelineno() - 1))
95 if len(line) != len(prevline):
96 Error("Line %s: Invalid dashes length" %
97 (fileinput.filelineno()))
105 if UNRELEASED_RE.match(line):
106 # Ignore unreleased versions
110 m = RELEASED_RE.match(line)
112 Error("Line %s: Invalid release line" % fileinput.filelineno())
114 # Including the weekday in the date string does not work as time.strptime
115 # would return an inconsistent result if the weekday is incorrect.
116 parsed_ts = time.mktime(time.strptime(m.group("date"), "%d %b %Y"))
117 parsed = datetime.date.fromtimestamp(parsed_ts)
118 weekday = parsed.strftime("%a")
121 if m.group("day") != weekday:
122 Error("Line %s: %s was/is a %s, not %s" %
123 (fileinput.filelineno(), parsed, weekday,
132 print >> sys.stderr, msg
138 if __name__ == "__main__":