Statistics
| Branch: | Tag: | Revision:

root / autotools / check-news @ 25d7b289

History | View | Annotate | Download (3.9 kB)

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

    
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
#: How many days release timestamps may be in the future
44
TIMESTAMP_FUTURE_DAYS_MAX = 3
45

    
46
errors = []
47

    
48

    
49
def Error(msg):
50
  """Log an error for later display.
51

    
52
  """
53
  errors.append(msg)
54

    
55

    
56
def ReqNLines(req, count_empty, lineno, line):
57
  """Check if we have N empty lines.
58

    
59
  """
60
  if count_empty < req:
61
    Error("Line %s: Missing empty line(s) before %s,"
62
          " %d needed but got only %d" %
63
          (lineno, line, req, count_empty))
64
  if count_empty > req:
65
    Error("Line %s: Too many empty lines before %s,"
66
          " %d needed but got %d" %
67
          (lineno, line, req, count_empty))
68

    
69

    
70
def main():
71
  # Ensure "C" locale is used
72
  curlocale = locale.getlocale()
73
  if curlocale != (None, None):
74
    Error("Invalid locale %s" % curlocale)
75

    
76
  prevline = None
77
  expect_date = False
78
  count_empty = 0
79

    
80
  for line in fileinput.input():
81
    line = line.rstrip("\n")
82

    
83
    if VERSION_RE.match(line):
84
      ReqNLines(2, count_empty, fileinput.filelineno(), line)
85

    
86
    if UNRELEASED_RE.match(line) or RELEASED_RE.match(line):
87
      ReqNLines(1, count_empty, fileinput.filelineno(), line)
88

    
89
    if line:
90
      count_empty = 0
91
    else:
92
      count_empty += 1
93

    
94
    if DASHES_RE.match(line):
95
      if not VERSION_RE.match(prevline):
96
        Error("Line %s: Invalid title" %
97
              (fileinput.filelineno() - 1))
98
      if len(line) != len(prevline):
99
        Error("Line %s: Invalid dashes length" %
100
              (fileinput.filelineno()))
101
      expect_date = True
102

    
103
    elif expect_date:
104
      if not line:
105
        # Ignore empty lines
106
        continue
107

    
108
      if UNRELEASED_RE.match(line):
109
        # Ignore unreleased versions
110
        expect_date = False
111
        continue
112

    
113
      m = RELEASED_RE.match(line)
114
      if not m:
115
        Error("Line %s: Invalid release line" % fileinput.filelineno())
116

    
117
      # Including the weekday in the date string does not work as time.strptime
118
      # would return an inconsistent result if the weekday is incorrect.
119
      parsed_ts = time.mktime(time.strptime(m.group("date"), "%d %b %Y"))
120
      parsed = datetime.date.fromtimestamp(parsed_ts)
121
      today = datetime.date.today()
122

    
123
      if (parsed - datetime.timedelta(TIMESTAMP_FUTURE_DAYS_MAX)) > today:
124
        Error("Line %s: %s is more than %s days in the future (today is %s)" %
125
              (fileinput.filelineno(), parsed, TIMESTAMP_FUTURE_DAYS_MAX,
126
               today))
127

    
128
      weekday = parsed.strftime("%a")
129

    
130
      # Check weekday
131
      if m.group("day") != weekday:
132
        Error("Line %s: %s was/is a %s, not %s" %
133
              (fileinput.filelineno(), parsed, weekday,
134
               m.group("day")))
135

    
136
      expect_date = False
137

    
138
    prevline = line
139

    
140
  if errors:
141
    for msg in errors:
142
      print >> sys.stderr, msg
143
    sys.exit(1)
144
  else:
145
    sys.exit(0)
146

    
147

    
148
if __name__ == "__main__":
149
  main()