check-news 5.69 KB
Newer Older
1 2 3
#!/usr/bin/python
#

4
# Copyright (C) 2011, 2012, 2013 Google Inc.
Klaus Aehlig's avatar
Klaus Aehlig committed
5
# All rights reserved.
6
#
Klaus Aehlig's avatar
Klaus Aehlig committed
7 8 9
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
10
#
Klaus Aehlig's avatar
Klaus Aehlig committed
11 12
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
13
#
Klaus Aehlig's avatar
Klaus Aehlig committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 30 31 32 33 34


"""Script to check NEWS file.

"""

Iustin Pop's avatar
Iustin Pop committed
35 36 37
# pylint: disable=C0103
# [C0103] Invalid name

38 39 40 41 42 43
import sys
import time
import datetime
import locale
import fileinput
import re
44
import os
45 46 47 48 49 50


DASHES_RE = re.compile(r"^\s*-+\s*$")
RELEASED_RE = re.compile(r"^\*\(Released (?P<day>[A-Z][a-z]{2}),"
                         r" (?P<date>.+)\)\*$")
UNRELEASED_RE = re.compile(r"^\*\(unreleased\)\*$")
51
VERSION_RE = re.compile(r"^Version (\d+(\.\d+)+( (alpha|beta|rc)\d+)?)$")
52

53
#: How many days release timestamps may be in the future
54
TIMESTAMP_FUTURE_DAYS_MAX = 5
55

Iustin Pop's avatar
Iustin Pop committed
56 57 58 59 60 61 62 63 64 65 66
errors = []


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

  """
  errors.append(msg)


def ReqNLines(req, count_empty, lineno, line):
Michele Tartara's avatar
Michele Tartara committed
67
  """Check if we have N empty lines before the current one.
Iustin Pop's avatar
Iustin Pop committed
68 69 70 71 72 73 74 75 76 77 78

  """
  if count_empty < req:
    Error("Line %s: Missing empty line(s) before %s,"
          " %d needed but got only %d" %
          (lineno, line, req, count_empty))
  if count_empty > req:
    Error("Line %s: Too many empty lines before %s,"
          " %d needed but got %d" %
          (lineno, line, req, count_empty))

79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
def IsAlphaVersion(version):
  return "alpha" in version


def UpdateAllowUnreleased(allow_unreleased, version_match, release):
  if not allow_unreleased:
    return False
  if IsAlphaVersion(release):
    return True
  version = version_match.group(1)
  if version == release:
    return False
  return True


95 96 97 98
def main():
  # Ensure "C" locale is used
  curlocale = locale.getlocale()
  if curlocale != (None, None):
Iustin Pop's avatar
Iustin Pop committed
99
    Error("Invalid locale %s" % curlocale)
100

101 102 103 104
  # Get the release version, but replace "~" with " " as the version
  # in the NEWS file uses spaces for beta and rc releases.
  release = os.environ.get('RELEASE', "").replace("~", " ")

105 106
  prevline = None
  expect_date = False
107
  count_empty = 0
108 109
  allow_unreleased = True
  found_versions = set()
110 111 112 113

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

114 115
    version_match = VERSION_RE.match(line)
    if version_match:
Iustin Pop's avatar
Iustin Pop committed
116
      ReqNLines(2, count_empty, fileinput.filelineno(), line)
117 118 119 120 121
      version = version_match.group(1)
      if version in found_versions:
        Error("Line %s: Duplicate release %s found" %
              (fileinput.filelineno(), version))
      found_versions.add(version)
122 123
      allow_unreleased = UpdateAllowUnreleased(allow_unreleased, version_match,
                                               release)
124 125 126 127 128 129 130

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

    if unreleased_match or RELEASED_RE.match(line):
Iustin Pop's avatar
Iustin Pop committed
131
      ReqNLines(1, count_empty, fileinput.filelineno(), line)
132 133 134 135 136 137

    if line:
      count_empty = 0
    else:
      count_empty += 1

138
    if DASHES_RE.match(line):
139
      if not VERSION_RE.match(prevline):
Iustin Pop's avatar
Iustin Pop committed
140 141
        Error("Line %s: Invalid title" %
              (fileinput.filelineno() - 1))
142
      if len(line) != len(prevline):
Iustin Pop's avatar
Iustin Pop committed
143 144
        Error("Line %s: Invalid dashes length" %
              (fileinput.filelineno()))
145 146 147 148 149 150 151 152 153 154 155 156 157 158
      expect_date = True

    elif expect_date:
      if not line:
        # Ignore empty lines
        continue

      if UNRELEASED_RE.match(line):
        # Ignore unreleased versions
        expect_date = False
        continue

      m = RELEASED_RE.match(line)
      if not m:
Iustin Pop's avatar
Iustin Pop committed
159
        Error("Line %s: Invalid release line" % fileinput.filelineno())
160 161
        expect_date = False
        continue
162 163 164 165 166

      # Including the weekday in the date string does not work as time.strptime
      # would return an inconsistent result if the weekday is incorrect.
      parsed_ts = time.mktime(time.strptime(m.group("date"), "%d %b %Y"))
      parsed = datetime.date.fromtimestamp(parsed_ts)
167 168 169 170 171 172 173
      today = datetime.date.today()

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

174 175 176 177
      weekday = parsed.strftime("%a")

      # Check weekday
      if m.group("day") != weekday:
Iustin Pop's avatar
Iustin Pop committed
178 179 180
        Error("Line %s: %s was/is a %s, not %s" %
              (fileinput.filelineno(), parsed, weekday,
               m.group("day")))
181 182 183 184 185

      expect_date = False

    prevline = line

Iustin Pop's avatar
Iustin Pop committed
186 187 188 189 190 191
  if errors:
    for msg in errors:
      print >> sys.stderr, msg
    sys.exit(1)
  else:
    sys.exit(0)
192 193 194 195


if __name__ == "__main__":
  main()