Statistics
| Branch: | Tag: | Revision:

root / autotools / check-news @ dadc7864

History | View | Annotate | Download (4.7 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
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+)+( (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 main():
72
  # Ensure "C" locale is used
73
  curlocale = locale.getlocale()
74
  if curlocale != (None, None):
75
    Error("Invalid locale %s" % curlocale)
76

    
77
  # Get the release version, but replace "~" with " " as the version
78
  # in the NEWS file uses spaces for beta and rc releases.
79
  release = os.environ.get('RELEASE', "").replace("~", " ")
80

    
81
  prevline = None
82
  expect_date = False
83
  count_empty = 0
84
  allow_unreleased = True
85
  found_versions = set()
86

    
87
  for line in fileinput.input():
88
    line = line.rstrip("\n")
89

    
90
    version_match = VERSION_RE.match(line)
91
    if version_match:
92
      ReqNLines(2, count_empty, fileinput.filelineno(), line)
93
      version = version_match.group(1)
94
      if version in found_versions:
95
        Error("Line %s: Duplicate release %s found" %
96
              (fileinput.filelineno(), version))
97
      found_versions.add(version)
98
      if version == release:
99
        allow_unreleased = False
100

    
101
    unreleased_match = UNRELEASED_RE.match(line)
102
    if unreleased_match and not allow_unreleased:
103
      Error("Line %s: Unreleased version after current release %s" %
104
            (fileinput.filelineno(), release))
105

    
106
    if unreleased_match or RELEASED_RE.match(line):
107
      ReqNLines(1, count_empty, fileinput.filelineno(), line)
108

    
109
    if line:
110
      count_empty = 0
111
    else:
112
      count_empty += 1
113

    
114
    if DASHES_RE.match(line):
115
      if not VERSION_RE.match(prevline):
116
        Error("Line %s: Invalid title" %
117
              (fileinput.filelineno() - 1))
118
      if len(line) != len(prevline):
119
        Error("Line %s: Invalid dashes length" %
120
              (fileinput.filelineno()))
121
      expect_date = True
122

    
123
    elif expect_date:
124
      if not line:
125
        # Ignore empty lines
126
        continue
127

    
128
      if UNRELEASED_RE.match(line):
129
        # Ignore unreleased versions
130
        expect_date = False
131
        continue
132

    
133
      m = RELEASED_RE.match(line)
134
      if not m:
135
        Error("Line %s: Invalid release line" % fileinput.filelineno())
136
        expect_date = False
137
        continue
138

    
139
      # Including the weekday in the date string does not work as time.strptime
140
      # would return an inconsistent result if the weekday is incorrect.
141
      parsed_ts = time.mktime(time.strptime(m.group("date"), "%d %b %Y"))
142
      parsed = datetime.date.fromtimestamp(parsed_ts)
143
      today = datetime.date.today()
144

    
145
      if (parsed - datetime.timedelta(TIMESTAMP_FUTURE_DAYS_MAX)) > today:
146
        Error("Line %s: %s is more than %s days in the future (today is %s)" %
147
              (fileinput.filelineno(), parsed, TIMESTAMP_FUTURE_DAYS_MAX,
148
               today))
149

    
150
      weekday = parsed.strftime("%a")
151

    
152
      # Check weekday
153
      if m.group("day") != weekday:
154
        Error("Line %s: %s was/is a %s, not %s" %
155
              (fileinput.filelineno(), parsed, weekday,
156
               m.group("day")))
157

    
158
      expect_date = False
159

    
160
    prevline = line
161

    
162
  if errors:
163
    for msg in errors:
164
      print >> sys.stderr, msg
165
    sys.exit(1)
166
  else:
167
    sys.exit(0)
168

    
169

    
170
if __name__ == "__main__":
171
  main()