Rewrite the update license script

The old script duplicated license headers for me, instead of
updating them.
fix-squashed-planets
MrSimbax 2021-09-11 15:50:20 +02:00
parent 95579bbf1d
commit 171bba1bcd
2 changed files with 58 additions and 12 deletions

58
tools/update-license.py Normal file
View File

@ -0,0 +1,58 @@
import argparse
import os
from typing import *
T = TypeVar('T')
SOURCE_FILES_SEARCH_DIRECTORIES = ['src', 'test']
LICENSE_HEADER_END = ' * along with this program. If not, see http://gnu.org/licenses'
def is_source_file(file: str) -> bool:
return file.endswith('.cpp') or file.endswith('.h')
def find_all_source_files(directories: list[str]) -> str:
for directory in directories:
for root, found_dirs, found_files in os.walk(directory):
for file in found_files:
if is_source_file(file):
path = os.path.join(root, file)
yield path
def find_license_header_end(lines: list[str]) -> Optional[int]:
for idx, line in enumerate(lines):
if LICENSE_HEADER_END in line:
return idx + 2 # One more line with '*/' and idx past end
return None
def remove_range(a_list: list[T], begin: int, end: int) -> list[str]:
return a_list[0:begin] + a_list[end:]
def remove_license_header(lines: list[str]) -> list[str]:
return remove_range(lines, begin=0, end=find_license_header_end(lines))
def add_license_header_at_beginning(lines: list[str], new_license_header: list[str]) -> list[str]:
return new_license_header + lines
def update_license_header(lines: list[str], new_license_header: list[str]) -> list[str]:
return add_license_header_at_beginning(remove_license_header(lines), new_license_header)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('license_header', help='Path to the new license header file', type=str)
args = parser.parse_args()
new_license_header = open(args.license_header).readlines()
for source_file in find_all_source_files(SOURCE_FILES_SEARCH_DIRECTORIES):
print(source_file)
lines = update_license_header(open(source_file).readlines(), new_license_header)
open(source_file, 'w', newline='\n').writelines(lines)
if __name__ == '__main__':
main()

View File

@ -1,12 +0,0 @@
#!/bin/bash
# Replaces license text with contents of file provided as an argument
if [ ! $# -eq 1 ]; then
echo "Usage: $0 [file with new license header]"
exit 1
fi
find . -name "*.cpp" -or -name "*.h" | while read file; do
echo $file
sed '/\/\/ \* This.*/,/If not\, see.*/d' $file > $file.tmp
cat $1 $file.tmp > $file
rm $file.tmp
done