Statistics
| Branch: | Tag: | Revision:

root / autotools / check-news @ 36c70d4d

History | View | Annotate | Download (3.2 kB)

1 7385c51d Michael Hanselmann
#!/usr/bin/python
2 7385c51d Michael Hanselmann
#
3 7385c51d Michael Hanselmann
4 7385c51d Michael Hanselmann
# Copyright (C) 2011 Google Inc.
5 7385c51d Michael Hanselmann
#
6 7385c51d Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 7385c51d Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 7385c51d Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 7385c51d Michael Hanselmann
# (at your option) any later version.
10 7385c51d Michael Hanselmann
#
11 7385c51d Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 7385c51d Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 7385c51d Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 7385c51d Michael Hanselmann
# General Public License for more details.
15 7385c51d Michael Hanselmann
#
16 7385c51d Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 7385c51d Michael Hanselmann
# along with this program; if not, write to the Free Software
18 7385c51d Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 7385c51d Michael Hanselmann
# 02110-1301, USA.
20 7385c51d Michael Hanselmann
21 7385c51d Michael Hanselmann
22 7385c51d Michael Hanselmann
"""Script to check NEWS file.
23 7385c51d Michael Hanselmann
24 7385c51d Michael Hanselmann
"""
25 7385c51d Michael Hanselmann
26 7385c51d Michael Hanselmann
import sys
27 7385c51d Michael Hanselmann
import time
28 7385c51d Michael Hanselmann
import datetime
29 7385c51d Michael Hanselmann
import locale
30 7385c51d Michael Hanselmann
import fileinput
31 7385c51d Michael Hanselmann
import re
32 7385c51d Michael Hanselmann
33 7385c51d Michael Hanselmann
34 7385c51d Michael Hanselmann
DASHES_RE = re.compile(r"^\s*-+\s*$")
35 7385c51d Michael Hanselmann
RELEASED_RE = re.compile(r"^\*\(Released (?P<day>[A-Z][a-z]{2}),"
36 7385c51d Michael Hanselmann
                         r" (?P<date>.+)\)\*$")
37 7385c51d Michael Hanselmann
UNRELEASED_RE = re.compile(r"^\*\(unreleased\)\*$")
38 51ef5fe0 Guido Trotter
VERSION_RE = re.compile(r"^Version \d+(\.\d+)+( (beta|rc)\d+)?$")
39 7385c51d Michael Hanselmann
40 7385c51d Michael Hanselmann
41 7385c51d Michael Hanselmann
def main():
42 7385c51d Michael Hanselmann
  # Ensure "C" locale is used
43 7385c51d Michael Hanselmann
  curlocale = locale.getlocale()
44 7385c51d Michael Hanselmann
  if curlocale != (None, None):
45 7385c51d Michael Hanselmann
    raise Exception("Invalid locale %s" % curlocale)
46 7385c51d Michael Hanselmann
47 7385c51d Michael Hanselmann
  prevline = None
48 7385c51d Michael Hanselmann
  expect_date = False
49 51ef5fe0 Guido Trotter
  count_empty = 0
50 7385c51d Michael Hanselmann
51 7385c51d Michael Hanselmann
  for line in fileinput.input():
52 7385c51d Michael Hanselmann
    line = line.rstrip("\n")
53 7385c51d Michael Hanselmann
54 51ef5fe0 Guido Trotter
    if VERSION_RE.match(line):
55 51ef5fe0 Guido Trotter
      if count_empty != 2:
56 51ef5fe0 Guido Trotter
        raise Exception("Line %s: Missing 2 empty lines before %s" %
57 51ef5fe0 Guido Trotter
                        (fileinput.filelineno(), line))
58 51ef5fe0 Guido Trotter
59 51ef5fe0 Guido Trotter
    if UNRELEASED_RE.match(line) or RELEASED_RE.match(line):
60 51ef5fe0 Guido Trotter
      if count_empty != 1:
61 51ef5fe0 Guido Trotter
        raise Exception("Line %s: Missing 1 empty line before %s" %
62 51ef5fe0 Guido Trotter
                        (fileinput.filelineno(), line))
63 51ef5fe0 Guido Trotter
64 51ef5fe0 Guido Trotter
    if line:
65 51ef5fe0 Guido Trotter
      count_empty = 0
66 51ef5fe0 Guido Trotter
    else:
67 51ef5fe0 Guido Trotter
      count_empty += 1
68 51ef5fe0 Guido Trotter
69 7385c51d Michael Hanselmann
    if DASHES_RE.match(line):
70 51ef5fe0 Guido Trotter
      if not VERSION_RE.match(prevline):
71 51ef5fe0 Guido Trotter
        raise Exception("Line %s: Invalid title" %
72 51ef5fe0 Guido Trotter
                        (fileinput.filelineno() - 1))
73 51ef5fe0 Guido Trotter
      if len(line) != len(prevline):
74 51ef5fe0 Guido Trotter
        raise Exception("Line %s: Invalid dashes length" %
75 51ef5fe0 Guido Trotter
                        (fileinput.filelineno()))
76 7385c51d Michael Hanselmann
      expect_date = True
77 7385c51d Michael Hanselmann
78 7385c51d Michael Hanselmann
    elif expect_date:
79 7385c51d Michael Hanselmann
      if not line:
80 7385c51d Michael Hanselmann
        # Ignore empty lines
81 7385c51d Michael Hanselmann
        continue
82 7385c51d Michael Hanselmann
83 7385c51d Michael Hanselmann
      if UNRELEASED_RE.match(line):
84 7385c51d Michael Hanselmann
        # Ignore unreleased versions
85 7385c51d Michael Hanselmann
        expect_date = False
86 7385c51d Michael Hanselmann
        continue
87 7385c51d Michael Hanselmann
88 7385c51d Michael Hanselmann
      m = RELEASED_RE.match(line)
89 7385c51d Michael Hanselmann
      if not m:
90 c6b1c8ef Michael Hanselmann
        raise Exception("Line %s: Invalid release line" %
91 c6b1c8ef Michael Hanselmann
                        fileinput.filelineno())
92 7385c51d Michael Hanselmann
93 7385c51d Michael Hanselmann
      # Including the weekday in the date string does not work as time.strptime
94 7385c51d Michael Hanselmann
      # would return an inconsistent result if the weekday is incorrect.
95 7385c51d Michael Hanselmann
      parsed_ts = time.mktime(time.strptime(m.group("date"), "%d %b %Y"))
96 7385c51d Michael Hanselmann
      parsed = datetime.date.fromtimestamp(parsed_ts)
97 7385c51d Michael Hanselmann
      weekday = parsed.strftime("%a")
98 7385c51d Michael Hanselmann
99 7385c51d Michael Hanselmann
      # Check weekday
100 7385c51d Michael Hanselmann
      if m.group("day") != weekday:
101 7385c51d Michael Hanselmann
        raise Exception("Line %s: %s was/is a %s, not %s" %
102 c6b1c8ef Michael Hanselmann
                        (fileinput.filelineno(), parsed, weekday,
103 c6b1c8ef Michael Hanselmann
                         m.group("day")))
104 7385c51d Michael Hanselmann
105 7385c51d Michael Hanselmann
      expect_date = False
106 7385c51d Michael Hanselmann
107 7385c51d Michael Hanselmann
    prevline = line
108 7385c51d Michael Hanselmann
109 7385c51d Michael Hanselmann
  sys.exit(0)
110 7385c51d Michael Hanselmann
111 7385c51d Michael Hanselmann
112 7385c51d Michael Hanselmann
if __name__ == "__main__":
113 7385c51d Michael Hanselmann
  main()