Created
September 23, 2019 15:46
-
-
Save chfast/f0e129c18c890d4ce3c481581371b37c to your computer and use it in GitHub Desktop.
A script to update copyright notice with a year in which a file was created
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import glob | |
def update_year_in_file(filename): | |
print('- ' + filename) | |
c = subprocess.run( | |
f"git log --diff-filter=A --follow --format=%ad --date=format:'%Y' -- {filename} | tail -1", | |
shell=True, capture_output=True) | |
year = c.stdout[:-1].decode('utf-8') | |
if int(year) == 2019: | |
print(' 2019 :(') | |
return | |
print(' ' + year) | |
pos = -1 | |
header = None | |
rest = None | |
with open(filename) as f: | |
header = f.read(80) | |
pos = header.find("Copyright 2019 Aleth") | |
if pos < 0: | |
print(' No copyright') | |
return | |
rest = "".join(f.readlines()) | |
pos += 10 | |
header = header[:pos] + year + '-' + header[pos:] | |
with open(filename, 'w') as f: | |
f.write(header) | |
f.write(rest) | |
files = [] | |
files += glob.glob('**/*.h', recursive=True) | |
files += glob.glob('**/*.hpp', recursive=True) | |
files += glob.glob('**/*.cpp', recursive=True) | |
files += glob.glob('**/*.txt', recursive=True) | |
files += glob.glob('**/*.cmake', recursive=True) | |
files += glob.glob('**/*.yml', recursive=True) | |
files += glob.glob('**/*.json', recursive=True) | |
files += glob.glob('**/*.bat', recursive=True) | |
files += glob.glob('**/*.sh', recursive=True) | |
for f in files: | |
if f.startswith('build'): | |
continue | |
if f.startswith('test/jsontests'): | |
continue | |
update_year_in_file(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment