9a2299526081b178f7d735e022cc7a721743c5a3
[ganeti-local] / autotools / check-news
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2011, 2012 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Script to check NEWS file.
23
24 """
25
26 # pylint: disable=C0103
27 # [C0103] Invalid name
28
29 import sys
30 import time
31 import datetime
32 import locale
33 import fileinput
34 import re
35
36
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+)?$")
42
43 errors = []
44
45
46 def Error(msg):
47   """Log an error for later display.
48
49   """
50   errors.append(msg)
51
52
53 def ReqNLines(req, count_empty, lineno, line):
54   """Check if we have N empty lines.
55
56   """
57   if count_empty < req:
58     Error("Line %s: Missing empty line(s) before %s,"
59           " %d needed but got only %d" %
60           (lineno, line, req, count_empty))
61   if count_empty > req:
62     Error("Line %s: Too many empty lines before %s,"
63           " %d needed but got %d" %
64           (lineno, line, req, count_empty))
65
66
67 def main():
68   # Ensure "C" locale is used
69   curlocale = locale.getlocale()
70   if curlocale != (None, None):
71     Error("Invalid locale %s" % curlocale)
72
73   prevline = None
74   expect_date = False
75   count_empty = 0
76
77   for line in fileinput.input():
78     line = line.rstrip("\n")
79
80     if VERSION_RE.match(line):
81       ReqNLines(2, count_empty, fileinput.filelineno(), line)
82
83     if UNRELEASED_RE.match(line) or RELEASED_RE.match(line):
84       ReqNLines(1, count_empty, fileinput.filelineno(), line)
85
86     if line:
87       count_empty = 0
88     else:
89       count_empty += 1
90
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()))
98       expect_date = True
99
100     elif expect_date:
101       if not line:
102         # Ignore empty lines
103         continue
104
105       if UNRELEASED_RE.match(line):
106         # Ignore unreleased versions
107         expect_date = False
108         continue
109
110       m = RELEASED_RE.match(line)
111       if not m:
112         Error("Line %s: Invalid release line" % fileinput.filelineno())
113
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")
119
120       # Check weekday
121       if m.group("day") != weekday:
122         Error("Line %s: %s was/is a %s, not %s" %
123               (fileinput.filelineno(), parsed, weekday,
124                m.group("day")))
125
126       expect_date = False
127
128     prevline = line
129
130   if errors:
131     for msg in errors:
132       print >> sys.stderr, msg
133     sys.exit(1)
134   else:
135     sys.exit(0)
136
137
138 if __name__ == "__main__":
139   main()