Renamed directory tools to scripts
parent
5a52c66b1f
commit
670cedd967
|
@ -1,4 +1,4 @@
|
||||||
tools/
|
scripts/
|
||||||
|
|
||||||
This directory contains useful scripts for changing many files at once,
|
This directory contains useful scripts for changing many files at once,
|
||||||
for example replacing string occurences or converting whitespace.
|
for example replacing string occurences or converting whitespace.
|
0
tools/git-diff-check-colobot-lint.sh → scripts/git-diff-check-colobot-lint.sh
Executable file → Normal file
0
tools/git-diff-check-colobot-lint.sh → scripts/git-diff-check-colobot-lint.sh
Executable file → Normal file
0
tools/strip-trailing-whitespace.sh → scripts/strip-trailing-whitespace.sh
Executable file → Normal file
0
tools/strip-trailing-whitespace.sh → scripts/strip-trailing-whitespace.sh
Executable file → Normal file
|
@ -1,58 +1,58 @@
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
from typing import *
|
from typing import *
|
||||||
|
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
|
|
||||||
SOURCE_FILES_SEARCH_DIRECTORIES = ['src', 'test']
|
SOURCE_FILES_SEARCH_DIRECTORIES = ['src', 'test']
|
||||||
LICENSE_HEADER_END = ' * along with this program. If not, see http://gnu.org/licenses'
|
LICENSE_HEADER_END = ' * along with this program. If not, see http://gnu.org/licenses'
|
||||||
|
|
||||||
def is_source_file(file: str) -> bool:
|
def is_source_file(file: str) -> bool:
|
||||||
return file.endswith('.cpp') or file.endswith('.h')
|
return file.endswith('.cpp') or file.endswith('.h')
|
||||||
|
|
||||||
|
|
||||||
def find_all_source_files(directories: list[str]) -> str:
|
def find_all_source_files(directories: list[str]) -> str:
|
||||||
for directory in directories:
|
for directory in directories:
|
||||||
for root, found_dirs, found_files in os.walk(directory):
|
for root, found_dirs, found_files in os.walk(directory):
|
||||||
for file in found_files:
|
for file in found_files:
|
||||||
if is_source_file(file):
|
if is_source_file(file):
|
||||||
path = os.path.join(root, file)
|
path = os.path.join(root, file)
|
||||||
yield path
|
yield path
|
||||||
|
|
||||||
|
|
||||||
def find_license_header_end(lines: list[str]) -> Optional[int]:
|
def find_license_header_end(lines: list[str]) -> Optional[int]:
|
||||||
for idx, line in enumerate(lines):
|
for idx, line in enumerate(lines):
|
||||||
if LICENSE_HEADER_END in line:
|
if LICENSE_HEADER_END in line:
|
||||||
return idx + 2 # One more line with '*/' and idx past end
|
return idx + 2 # One more line with '*/' and idx past end
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def remove_range(a_list: list[T], begin: int, end: int) -> list[str]:
|
def remove_range(a_list: list[T], begin: int, end: int) -> list[str]:
|
||||||
return a_list[0:begin] + a_list[end:]
|
return a_list[0:begin] + a_list[end:]
|
||||||
|
|
||||||
|
|
||||||
def remove_license_header(lines: list[str]) -> list[str]:
|
def remove_license_header(lines: list[str]) -> list[str]:
|
||||||
return remove_range(lines, begin=0, end=find_license_header_end(lines))
|
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]:
|
def add_license_header_at_beginning(lines: list[str], new_license_header: list[str]) -> list[str]:
|
||||||
return new_license_header + lines
|
return new_license_header + lines
|
||||||
|
|
||||||
|
|
||||||
def update_license_header(lines: list[str], new_license_header: list[str]) -> list[str]:
|
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)
|
return add_license_header_at_beginning(remove_license_header(lines), new_license_header)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('license_header', help='Path to the new license header file', type=str)
|
parser.add_argument('license_header', help='Path to the new license header file', type=str)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
new_license_header = open(args.license_header).readlines()
|
new_license_header = open(args.license_header).readlines()
|
||||||
for source_file in find_all_source_files(SOURCE_FILES_SEARCH_DIRECTORIES):
|
for source_file in find_all_source_files(SOURCE_FILES_SEARCH_DIRECTORIES):
|
||||||
print(source_file)
|
print(source_file)
|
||||||
lines = update_license_header(open(source_file).readlines(), new_license_header)
|
lines = update_license_header(open(source_file).readlines(), new_license_header)
|
||||||
open(source_file, 'w', newline='\n').writelines(lines)
|
open(source_file, 'w', newline='\n').writelines(lines)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue