From 05111486d5cf7399964f58acdc1c3b07d8321939 Mon Sep 17 00:00:00 2001 From: Natsu Date: Wed, 19 Jun 2024 03:28:53 +0700 Subject: [PATCH] chore: Add resource validation workflow and script --- .github/workflows/dispatch.yml | 11 +++++++- scripts/resource_validator.py | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 scripts/resource_validator.py diff --git a/.github/workflows/dispatch.yml b/.github/workflows/dispatch.yml index 6ff39b1f..8f2db2d0 100644 --- a/.github/workflows/dispatch.yml +++ b/.github/workflows/dispatch.yml @@ -11,12 +11,21 @@ jobs: - name: Checkout uses: actions/checkout@v4 - - name: Validate + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Validate JSON and YAML id: json-yaml-validate uses: GrantBirki/json-yaml-validate@v3 with: use_gitignore: false + - name: Validate Resource + id: resource-validate + run: python ./scripts/resource_validator.py ./local-files/resource + dispatch: runs-on: ubuntu-latest diff --git a/scripts/resource_validator.py b/scripts/resource_validator.py new file mode 100644 index 00000000..696b2103 --- /dev/null +++ b/scripts/resource_validator.py @@ -0,0 +1,50 @@ +import re +import sys +import os + +def check_syntax(file_path): + with open(file_path, 'r', encoding='utf-8') as file: + lines = file.readlines() + + errors = [] + for line_number, line in enumerate(lines, 1): + if not re.match(r'^\[.*\]$', line.strip()) and line.strip(): + errors.append(f"Error: ❌ Syntax error in {file_path} on line {line_number}") + + return errors + +def check_directory(directory): + all_errors = [] + valid_files_count = 0 + invalid_files_count = 0 + + for root, _, files in os.walk(directory): + for file in files: + if file.endswith(".txt"): + file_path = os.path.join(root, file) + errors = check_syntax(file_path) + if errors: + all_errors.extend(errors) + invalid_files_count += 1 + else: + valid_files_count += 1 + + return all_errors, valid_files_count, invalid_files_count + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: python resource_validator.py ") + sys.exit(1) + + directory_path = sys.argv[1] + errors, valid_files_count, invalid_files_count = check_directory(directory_path) + + if errors: + for error in errors: + print(error) + print(f"\nError: ❌ {invalid_files_count} files have syntax errors.") + else: + print("✅ No syntax errors found.") + + print(f"✅ {valid_files_count} files are valid.") + sys.exit(1 if errors else 0)