#!/usr/bin/python # # Copyright (C) 2011 Google Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA. """Script to check NEWS file. """ import sys import time import datetime import locale import fileinput import re DASHES_RE = re.compile(r"^\s*-+\s*$") RELEASED_RE = re.compile(r"^\*\(Released (?P[A-Z][a-z]{2})," r" (?P.+)\)\*$") UNRELEASED_RE = re.compile(r"^\*\(unreleased\)\*$") VERSION_RE = re.compile(r"^Version \d+(\.\d+)+( (beta|rc)\d+)?$") def main(): # Ensure "C" locale is used curlocale = locale.getlocale() if curlocale != (None, None): raise Exception("Invalid locale %s" % curlocale) prevline = None expect_date = False count_empty = 0 for line in fileinput.input(): line = line.rstrip("\n") if VERSION_RE.match(line): if count_empty != 2: raise Exception("Line %s: Missing 2 empty lines before %s" % (fileinput.filelineno(), line)) if UNRELEASED_RE.match(line) or RELEASED_RE.match(line): if count_empty != 1: raise Exception("Line %s: Missing 1 empty line before %s" % (fileinput.filelineno(), line)) if line: count_empty = 0 else: count_empty += 1 if DASHES_RE.match(line): if not VERSION_RE.match(prevline): raise Exception("Line %s: Invalid title" % (fileinput.filelineno() - 1)) if len(line) != len(prevline): raise Exception("Line %s: Invalid dashes length" % (fileinput.filelineno())) 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: raise Exception("Line %s: Invalid release line" % fileinput.filelineno()) # 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) weekday = parsed.strftime("%a") # Check weekday if m.group("day") != weekday: raise Exception("Line %s: %s was/is a %s, not %s" % (fileinput.filelineno(), parsed, weekday, m.group("day"))) expect_date = False prevline = line sys.exit(0) if __name__ == "__main__": main()