Skip to content
Snippets Groups Projects
Commit 2a15e825 authored by Simon Hermanns's avatar Simon Hermanns
Browse files

Merge branch '226-add-script-for-updating-the-year-in-the-copyright-header' into 'master'

Resolve "Add script for updating the year in the copyright header"

Closes #226

See merge request !324
parents 565f5006 0a517a90
No related branches found
No related tags found
1 merge request!324Resolve "Add script for updating the year in the copyright header"
Pipeline #125627 failed
"""
This script updates the year in the copyright header of all files matching the given filter
Parameters:
arg1: (optional) Base directory for folders to search for files. If not present working directory is used.
"""
import sys
import os
import re
from datetime import datetime
import fileinput
directories = ['include', 'src', 'tests']
file_endings = ['h', 'cpp', 'py']
# custom files that should be included
additional_files = ['.gitlab-ci.yml']
blacklist = ['src/qtColorTriangle.cpp', 'include/qtColorTriangle.h']
blacklist_dir = ('.pytest_cache', '__pycache__', 'regression_test/data')
# adjust directory path
base_dir = sys.argv[1] if len(sys.argv) > 1 else '.'
directories = [f'{base_dir}/{directory}' for directory in directories]
blacklist = [f'{base_dir}/{f}' for f in blacklist]
files = []
for directory in directories:
for file in os.listdir(directory):
file = f'{directory}/{file}'
if os.path.isdir(file):
if file.endswith(blacklist_dir):
continue
directories.append(file)
else:
files.append(f'{file}')
filtered = [f for f in files if f not in blacklist]
filtered.extend(additional_files)
current_year = datetime.now().year
regex = "Copyright \(C\) \d{4} Forschungszentrum J.*lich GmbH"
pattern = re.compile(regex)
error_count = 0
update_count = 0
for f in filtered:
try:
with fileinput.FileInput(f, inplace=True, encoding="utf-8") as file:
for i, line in enumerate(file, start=0):
# Replace the copyright year in the third line
if i == 2 and pattern.search(line):
print(re.sub("\d{4}", str(current_year), line), end="")
update_count += 1
else:
print(line, end="")
except Exception as e:
print(str(e))
error_count += 1
print(f"Updated Copyright in {update_count} files, {error_count} errors")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment