Implement support for multi-module export
[ganeti-local] / autotools / check-news
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2011 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 import sys
27 import time
28 import datetime
29 import locale
30 import fileinput
31 import re
32
33
34 DASHES_RE = re.compile(r"^\s*-+\s*$")
35 RELEASED_RE = re.compile(r"^\*\(Released (?P<day>[A-Z][a-z]{2}),"
36                          r" (?P<date>.+)\)\*$")
37 UNRELEASED_RE = re.compile(r"^\*\(unreleased\)\*$")
38 VERSION_RE = re.compile(r"^Version \d+(\.\d+)+( (beta|rc)\d+)?$")
39
40
41 def main():
42   # Ensure "C" locale is used
43   curlocale = locale.getlocale()
44   if curlocale != (None, None):
45     raise Exception("Invalid locale %s" % curlocale)
46
47   prevline = None
48   expect_date = False
49   count_empty = 0
50
51   for line in fileinput.input():
52     line = line.rstrip("\n")
53
54     if VERSION_RE.match(line):
55       if count_empty != 2:
56         raise Exception("Line %s: Missing 2 empty lines before %s" %
57                         (fileinput.filelineno(), line))
58
59     if UNRELEASED_RE.match(line) or RELEASED_RE.match(line):
60       if count_empty != 1:
61         raise Exception("Line %s: Missing 1 empty line before %s" %
62                         (fileinput.filelineno(), line))
63
64     if line:
65       count_empty = 0
66     else:
67       count_empty += 1
68
69     if DASHES_RE.match(line):
70       if not VERSION_RE.match(prevline):
71         raise Exception("Line %s: Invalid title" %
72                         (fileinput.filelineno() - 1))
73       if len(line) != len(prevline):
74         raise Exception("Line %s: Invalid dashes length" %
75                         (fileinput.filelineno()))
76       expect_date = True
77
78     elif expect_date:
79       if not line:
80         # Ignore empty lines
81         continue
82
83       if UNRELEASED_RE.match(line):
84         # Ignore unreleased versions
85         expect_date = False
86         continue
87
88       m = RELEASED_RE.match(line)
89       if not m:
90         raise Exception("Line %s: Invalid release line" %
91                         fileinput.filelineno())
92
93       # Including the weekday in the date string does not work as time.strptime
94       # would return an inconsistent result if the weekday is incorrect.
95       parsed_ts = time.mktime(time.strptime(m.group("date"), "%d %b %Y"))
96       parsed = datetime.date.fromtimestamp(parsed_ts)
97       weekday = parsed.strftime("%a")
98
99       # Check weekday
100       if m.group("day") != weekday:
101         raise Exception("Line %s: %s was/is a %s, not %s" %
102                         (fileinput.filelineno(), parsed, weekday,
103                          m.group("day")))
104
105       expect_date = False
106
107     prevline = line
108
109   sys.exit(0)
110
111
112 if __name__ == "__main__":
113   main()