| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
|
| 4 |
# Copyright (C) 2011, 2012, 2013 Google Inc. |
| 5 |
# All rights reserved. |
| 6 |
# |
| 7 |
# Redistribution and use in source and binary forms, with or without |
| 8 |
# modification, are permitted provided that the following conditions are |
| 9 |
# met: |
| 10 |
# |
| 11 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 12 |
# this list of conditions and the following disclaimer. |
| 13 |
# |
| 14 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
# notice, this list of conditions and the following disclaimer in the |
| 16 |
# documentation and/or other materials provided with the distribution. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 19 |
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 20 |
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 |
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 25 |
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 |
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 |
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
|
| 31 |
"""Script to check NEWS file. |
| 32 |
|
| 33 |
""" |
| 34 |
|
| 35 |
# pylint: disable=C0103 |
| 36 |
# [C0103] Invalid name |
| 37 |
|
| 38 |
from __future__ import print_function |
| 39 |
|
| 40 |
import sys |
| 41 |
import time |
| 42 |
import datetime |
| 43 |
import locale |
| 44 |
import fileinput |
| 45 |
import re |
| 46 |
import os |
| 47 |
|
| 48 |
|
| 49 |
DASHES_RE = re.compile(r"^\s*-+\s*$") |
| 50 |
RELEASED_RE = re.compile(r"^\*\(Released (?P<day>[A-Z][a-z]{2})," |
| 51 |
r" (?P<date>.+)\)\*$") |
| 52 |
UNRELEASED_RE = re.compile(r"^\*\(unreleased\)\*$") |
| 53 |
VERSION_RE = re.compile(r"^Version (\d+(\.\d+)+( (alpha|beta|rc)\d+)?)$") |
| 54 |
|
| 55 |
#: How many days release timestamps may be in the future |
| 56 |
TIMESTAMP_FUTURE_DAYS_MAX = 5 |
| 57 |
|
| 58 |
errors = [] |
| 59 |
|
| 60 |
|
| 61 |
def Error(msg): |
| 62 |
"""Log an error for later display. |
| 63 |
|
| 64 |
""" |
| 65 |
errors.append(msg) |
| 66 |
|
| 67 |
|
| 68 |
def ReqNLines(req, count_empty, lineno, line): |
| 69 |
"""Check if we have N empty lines before the current one. |
| 70 |
|
| 71 |
""" |
| 72 |
if count_empty < req: |
| 73 |
Error("Line %s: Missing empty line(s) before %s," |
| 74 |
" %d needed but got only %d" % |
| 75 |
(lineno, line, req, count_empty)) |
| 76 |
if count_empty > req: |
| 77 |
Error("Line %s: Too many empty lines before %s," |
| 78 |
" %d needed but got %d" % |
| 79 |
(lineno, line, req, count_empty)) |
| 80 |
|
| 81 |
|
| 82 |
def IsAlphaVersion(version): |
| 83 |
return "alpha" in version |
| 84 |
|
| 85 |
|
| 86 |
def UpdateAllowUnreleased(allow_unreleased, version_match, release): |
| 87 |
if not allow_unreleased: |
| 88 |
return False |
| 89 |
if IsAlphaVersion(release): |
| 90 |
return True |
| 91 |
version = version_match.group(1) |
| 92 |
if version == release: |
| 93 |
return False |
| 94 |
return True |
| 95 |
|
| 96 |
|
| 97 |
def main(): |
| 98 |
# Ensure "C" locale is used |
| 99 |
curlocale = locale.getlocale() |
| 100 |
if curlocale[0] not in (None, 'C'): |
| 101 |
Error("Invalid locale %s" % str(curlocale)) |
| 102 |
|
| 103 |
# Get the release version, but replace "~" with " " as the version |
| 104 |
# in the NEWS file uses spaces for beta and rc releases. |
| 105 |
release = os.environ.get('RELEASE', "").replace("~", " ") |
| 106 |
|
| 107 |
prevline = None |
| 108 |
expect_date = False |
| 109 |
count_empty = 0 |
| 110 |
allow_unreleased = True |
| 111 |
found_versions = set() |
| 112 |
|
| 113 |
for line in fileinput.input(): |
| 114 |
line = line.rstrip("\n") |
| 115 |
|
| 116 |
version_match = VERSION_RE.match(line) |
| 117 |
if version_match: |
| 118 |
ReqNLines(2, count_empty, fileinput.filelineno(), line) |
| 119 |
version = version_match.group(1) |
| 120 |
if version in found_versions: |
| 121 |
Error("Line %s: Duplicate release %s found" % |
| 122 |
(fileinput.filelineno(), version)) |
| 123 |
found_versions.add(version) |
| 124 |
allow_unreleased = UpdateAllowUnreleased(allow_unreleased, version_match, |
| 125 |
release) |
| 126 |
|
| 127 |
unreleased_match = UNRELEASED_RE.match(line) |
| 128 |
if unreleased_match and not allow_unreleased: |
| 129 |
Error("Line %s: Unreleased version after current release %s" % |
| 130 |
(fileinput.filelineno(), release)) |
| 131 |
|
| 132 |
if unreleased_match or RELEASED_RE.match(line): |
| 133 |
ReqNLines(1, count_empty, fileinput.filelineno(), line) |
| 134 |
|
| 135 |
if line: |
| 136 |
count_empty = 0 |
| 137 |
else: |
| 138 |
count_empty += 1 |
| 139 |
|
| 140 |
if DASHES_RE.match(line): |
| 141 |
if not VERSION_RE.match(prevline): |
| 142 |
Error("Line %s: Invalid title" % |
| 143 |
(fileinput.filelineno() - 1)) |
| 144 |
if len(line) != len(prevline): |
| 145 |
Error("Line %s: Invalid dashes length" % |
| 146 |
(fileinput.filelineno())) |
| 147 |
expect_date = True |
| 148 |
|
| 149 |
elif expect_date: |
| 150 |
if not line: |
| 151 |
# Ignore empty lines |
| 152 |
continue |
| 153 |
|
| 154 |
if UNRELEASED_RE.match(line): |
| 155 |
# Ignore unreleased versions |
| 156 |
expect_date = False |
| 157 |
continue |
| 158 |
|
| 159 |
m = RELEASED_RE.match(line) |
| 160 |
if not m: |
| 161 |
Error("Line %s: Invalid release line" % fileinput.filelineno()) |
| 162 |
expect_date = False |
| 163 |
continue |
| 164 |
|
| 165 |
# Including the weekday in the date string does not work as time.strptime |
| 166 |
# would return an inconsistent result if the weekday is incorrect. |
| 167 |
parsed_ts = time.mktime(time.strptime(m.group("date"), "%d %b %Y")) |
| 168 |
parsed = datetime.date.fromtimestamp(parsed_ts) |
| 169 |
today = datetime.date.today() |
| 170 |
|
| 171 |
if (parsed - datetime.timedelta(TIMESTAMP_FUTURE_DAYS_MAX)) > today: |
| 172 |
Error("Line %s: %s is more than %s days in the future (today is %s)" % |
| 173 |
(fileinput.filelineno(), parsed, TIMESTAMP_FUTURE_DAYS_MAX, |
| 174 |
today)) |
| 175 |
|
| 176 |
weekday = parsed.strftime("%a") |
| 177 |
|
| 178 |
# Check weekday |
| 179 |
if m.group("day") != weekday: |
| 180 |
Error("Line %s: %s was/is a %s, not %s" % |
| 181 |
(fileinput.filelineno(), parsed, weekday, |
| 182 |
m.group("day"))) |
| 183 |
|
| 184 |
expect_date = False |
| 185 |
|
| 186 |
prevline = line |
| 187 |
|
| 188 |
if errors: |
| 189 |
for msg in errors: |
| 190 |
print(msg, file=sys.stderr) |
| 191 |
sys.exit(1) |
| 192 |
else: |
| 193 |
sys.exit(0) |
| 194 |
|
| 195 |
|
| 196 |
if __name__ == "__main__": |
| 197 |
main() |