Index: source/tools/hooks/license_year.py =================================================================== --- source/tools/hooks/license_year.py +++ source/tools/hooks/license_year.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +import os +import sys +from datetime import date +from re import compile +try: + from svn.local import LocalClient + from svn.exception import SvnException +except ImportError: + sys.stderr.write('Required library: https://pypi.python.org/pypi/svn\n') + +project_root = os.path.abspath(os.path.join(os.path.abspath(__file__), '..', '..', '..')) +client = LocalClient(project_root) +license_regexp = compile(r'^/\* Copyright \([cC]?\) ([\d]+) Wildfire Games') +valid_extensions = ['.cpp', '.cc', '.cxx', '.c', '.h', '.hpp', '.hxx'] +current_year = date.today().year + +for status in client.status(): + if status.type_raw_name != 'modified': + continue + path = status.name + + if not os.path.isfile(path): + continue + + ext = os.path.splitext(path)[1].lower() + if ext not in valid_extensions: + continue + + with open(path, 'rt') as handle: + data = handle.read() + match = license_regexp.search(data) + + if not match: + # Only warning about it, because it can be the file of a third party library. + sys.stderr.write('License header not found in "{}"\n'.format(path)) + continue + + license_year = match.group(1) + if not license_year: + sys.stderr.write('Invalid license year found in "{}"\n'.format(path)) + continue + + license_year = int(license_year) + if license_year == current_year: + continue + + def year_replacer(match): + return match.group(0).replace(match.group(1), str(current_year)) + data = license_regexp.sub(year_replacer, data) + handle.close() + + with open(path, 'wt') as handle: + handle.write(data) + sys.stderr.write('Wrong license year {} was replaced in "{}"\n'.format(license_year, path)) +