Add git send-email to the chroot
[ganeti-local] / autotools / check-news
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2011, 2012, 2013 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 import os
36
37
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+)?)$")
43
44 #: How many days release timestamps may be in the future
45 TIMESTAMP_FUTURE_DAYS_MAX = 3
46
47 errors = []
48
49
50 def Error(msg):
51   """Log an error for later display.
52
53   """
54   errors.append(msg)
55
56
57 def ReqNLines(req, count_empty, lineno, line):
58   """Check if we have N empty lines before the current one.
59
60   """
61   if count_empty < req:
62     Error("Line %s: Missing empty line(s) before %s,"
63           " %d needed but got only %d" %
64           (lineno, line, req, count_empty))
65   if count_empty > req:
66     Error("Line %s: Too many empty lines before %s,"
67           " %d needed but got %d" %
68           (lineno, line, req, count_empty))
69
70
71 def IsAlphaVersion(version):
72   return "alpha" in version
73
74
75 def UpdateAllowUnreleased(allow_unreleased, version_match, release):
76   if not allow_unreleased:
77     return False
78   if IsAlphaVersion(release):
79     return True
80   version = version_match.group(1)
81   if version == release:
82     return False
83   return True
84
85
86 def main():
87   # Ensure "C" locale is used
88   curlocale = locale.getlocale()
89   if curlocale != (None, None):
90     Error("Invalid locale %s" % curlocale)
91
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("~", " ")
95
96   prevline = None
97   expect_date = False
98   count_empty = 0
99   allow_unreleased = True
100   found_versions = set()
101
102   for line in fileinput.input():
103     line = line.rstrip("\n")
104
105     version_match = VERSION_RE.match(line)
106     if version_match:
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,
114                                                release)
115
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))
120
121     if unreleased_match or RELEASED_RE.match(line):
122       ReqNLines(1, count_empty, fileinput.filelineno(), line)
123
124     if line:
125       count_empty = 0
126     else:
127       count_empty += 1
128
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()))
136       expect_date = True
137
138     elif expect_date:
139       if not line:
140         # Ignore empty lines
141         continue
142
143       if UNRELEASED_RE.match(line):
144         # Ignore unreleased versions
145         expect_date = False
146         continue
147
148       m = RELEASED_RE.match(line)
149       if not m:
150         Error("Line %s: Invalid release line" % fileinput.filelineno())
151         expect_date = False
152         continue
153
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()
159
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,
163                today))
164
165       weekday = parsed.strftime("%a")
166
167       # Check weekday
168       if m.group("day") != weekday:
169         Error("Line %s: %s was/is a %s, not %s" %
170               (fileinput.filelineno(), parsed, weekday,
171                m.group("day")))
172
173       expect_date = False
174
175     prevline = line
176
177   if errors:
178     for msg in errors:
179       print >> sys.stderr, msg
180     sys.exit(1)
181   else:
182     sys.exit(0)
183
184
185 if __name__ == "__main__":
186   main()