Changeset View
Changeset View
Standalone View
Standalone View
source/tools/hooks/license_year.py
#!/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', '.h'] | |||||
Stan: c file as well no ? | |||||
vladislavbelovAuthorUnsubmitted Done Inline ActionsWe have only 3 *.c files in our source repo, but I suppose we can add it too :) vladislavbelov: We have only 3 `*.c` files in our `source` repo, but I suppose we can add it too :) | |||||
StanUnsubmitted Not Done Inline ActionsDo we have hpp, tpp, ipp files ? :) Stan: Do we have hpp, tpp, ipp files ? :) | |||||
vladislavbelovAuthorUnsubmitted Done Inline ActionsI found only *.hpp. I can add most C++ extensions, since we just ignore files without the license (only warn about them). vladislavbelov: I found only `*.hpp`. I can add most C++ extensions, since we just ignore files without the… | |||||
vladislavbelovAuthorUnsubmitted Done Inline ActionsBut actually we should one style, so other extensions aren't necessary. vladislavbelov: But actually we should one style, so other extensions aren't necessary. | |||||
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] | |||||
if ext not in valid_extensions: | |||||
continue | |||||
with open(path, 'rt') as handle: | |||||
data = handle.read() | |||||
match = license_regexp.search(data) | |||||
if not match: | |||||
sys.stderr.write('License header not found in {}\n'.format(path)) | |||||
license_year = match.group(1) | |||||
if not license_year: | |||||
sys.stderr.write('Invalid license header not found in {}\n'.format(path)) | |||||
wraitiiUnsubmitted Not Done Inline Actionss/header/year/ I think? wraitii: s/header/year/ I think? | |||||
vladislavbelovAuthorUnsubmitted Done Inline ActionsYeah, you're right. vladislavbelov: Yeah, you're right. | |||||
license_year = int(license_year) | |||||
if license_year == current_year: | |||||
continue | |||||
sys.stderr.write('Wrong license year {} was replaced in {}\n'.format(license_year, path)) | |||||
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) | |||||
Wildfire Games · Phabricator
c file as well no ?