From db17539652c5cc90ee63bec52f52b69689edf5ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sat, 14 Oct 2023 13:41:09 +0200 Subject: [PATCH] Rewrite unused header style check shell script in Python As the shell script is pretty slow, taking roughly 84 seconds in the CI, whereas the equivalent Python script takes only 1 second. --- .github/workflows/style.yml | 5 ++-- scripts/check_unused_header_files.py | 36 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100755 scripts/check_unused_header_files.py diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml index a35dc8e04..302dc1697 100644 --- a/.github/workflows/style.yml +++ b/.github/workflows/style.yml @@ -73,9 +73,8 @@ jobs: run: | pylint --version find . -type f -name "*.py" -not -path './ddnet-libs/*' -not -path './googletest-src/*' -print0 | xargs -0 pylint - - name: Unused headers - run: | - find src -name '*.h' | while read -r i; do grep -r -q "$(basename "$i")" || (echo "Header file $i is unused" && exit 1); done + - name: Unused header files + run: scripts/check_unused_header_files.py - name: Check maps run: | findings=$(find data -type f -name '*.map' -print0 | xargs -0 ~/.cargo/bin/twmap-check-ddnet 2>&1 | \ diff --git a/scripts/check_unused_header_files.py b/scripts/check_unused_header_files.py new file mode 100755 index 000000000..a03365df1 --- /dev/null +++ b/scripts/check_unused_header_files.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +import os +import sys + +def find_unused_header_files(directory): + header_files = set() + used_files = set() + + for root, _, files in os.walk(directory): + for file in files: + if file.endswith('.h'): + header_files.add(file) + + for root, _, files in os.walk(directory): + for file in files: + with open(os.path.join(root, file), 'r', encoding="utf-8") as f: + content = f.read() + for header in header_files: + if header in content: + used_files.add(header) + + return header_files - used_files + +def main(): + directory = 'src' + unused_header_files = find_unused_header_files(directory) + + if unused_header_files: + for file in unused_header_files: + print(f"Error: Header file '{file}' is unused.") + return 1 + print("Success: No header files are unused.") + return 0 + +if __name__ == "__main__": + sys.exit(main())