Skip to content

Instantly share code, notes, and snippets.

@chfast
Created September 23, 2019 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chfast/f0e129c18c890d4ce3c481581371b37c to your computer and use it in GitHub Desktop.
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
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