From 48e97095c75f97f029766887e94d9aa7e3b367db Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Tue, 25 Jul 2023 22:24:26 +0100 Subject: [PATCH] Remove the old bash based include fixer with a better python version --- .pre-commit-config.yaml | 7 ----- scripts/include-fixer.py | 56 ++++++++++++++++++++++++++++++++++++++++ scripts/include-fixer.sh | 47 --------------------------------- 3 files changed, 56 insertions(+), 54 deletions(-) create mode 100755 scripts/include-fixer.py delete mode 100755 scripts/include-fixer.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ba177b3..8af5f06 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,13 +18,6 @@ repos: - id: trailing-whitespace - id: mixed-line-ending - # - repo: local - # hooks: - # - id: include-fixer - # name: Fixing 'serene' includes - # language: script - # entry: ./scripts/include-fixer.sh - # files: ".*.(h|cpp)" - repo: local hooks: - id: include-fixer py diff --git a/scripts/include-fixer.py b/scripts/include-fixer.py new file mode 100755 index 0000000..a969856 --- /dev/null +++ b/scripts/include-fixer.py @@ -0,0 +1,56 @@ +#! /usr/bin/env python + +import sys +import re +import fileinput +from pathlib import Path + + +target_header_pattern = re.compile(r"\#include [<\"](.+)[\">](.*)$", re.M) + + +if __name__ == "__main__": + # In the `.pre-commit-config.yaml` configuration we have set: + # files: ".*.(h|cpp)" + # for this file. So it will only receive cpp and h files. No need to + # fiter. + files = sys.argv[1:] + + src_dir = Path(__file__).parent.parent / "serene" / "src" + headers_files = (src_dir).rglob("*.h") + + headers = [] + for h in headers_files: + headers.append(str(h.relative_to(src_dir))) + + # Loop over every line in every input file and write lines + # one by one and modify any file that needs to be rewritten + for line in fileinput.input(files=files, inplace=True): + m = re.match(target_header_pattern, line) + if m: + header = m.group(1) + rest = m.group(2) + if header in headers or header.startswith("serene/"): + print(f"#include \"{header}\"{rest}") + else: + print(f"#include <{header}>{rest}") + else: + print(line, end='') + + # # For debugging purposes I leave this here. + # for f in files: + # with open(f, "r") as fd: + # lines = fd.readlines() + # for line in lines: + # m = re.match(target_header_pattern, line) + # if m: + # header = m.group(1) + # print("header: ", header) + # rest = m.group(2) + # if header in headers or header == "serene/config.h": + # print(f"#include \"{header}\"{rest}") + # else: + # print(f"#include <{header}>{rest}") + # else: + # print(line, end='') + # raise TypeError() diff --git a/scripts/include-fixer.sh b/scripts/include-fixer.sh deleted file mode 100755 index 384bf18..0000000 --- a/scripts/include-fixer.sh +++ /dev/null @@ -1,47 +0,0 @@ -#! /bin/bash - -set -e - -# function get_changed_files() { -# local add_modified -# local renamed -# add_modified=$(git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '($1 != "D" && $1 !~ /R.+/) { print $2 }') -# renamed=$(git diff --cached --name-status |egrep '*\.(cpp|h|hpp|cpp.inc|h.inc)'|awk '$1 ~ /^R.+/ { print $3 }') - -# echo $add_modified | sed '/^[[:space:]]*$/d' -# echo $renamed | sed '/^[[:space:]]*$/d' -# } - -function fix_includes(){ - #local files=$(get_changed_files) - - echo "Fixing '#include' syntax in '$1'" - sed -i -E '/^#include "(serene|\.)/!s/^.include \"(.*)\"/#include <\1>/g' "$1" -} - -# function clang_format_staged() { -# local files=$(get_changed_files) - -# if [[ "$files" ]]; -# then -# for file in $(get_changed_files) -# do -# echo "Reformatting $file..." -# clang-format -i $file -# done -# fi - -# } - -# function git_add_changes() { -# local files=$(get_changed_files) - -# if [[ "$files" ]]; -# then -# git add $files -# fi -# } - -fix_includes "$1" -# clang_format_staged -# git_add_changes