From d63ff2513b7d3aae10d07a7e64e02c6bf7eebac4 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Thu, 15 May 2025 19:39:25 -0400 Subject: [PATCH] Add pre-commit checks --- .github/workflows/validate.yml | 14 +++++++++++ .pre-commit-config.yaml | 29 +++++++++++++++++++++ build/git_info.py | 14 ++++++++--- build/glob_recursive.py | 11 ++++---- build/option_handler.py | 11 ++++---- pyproject.toml | 46 ++++++++++++++++++++++++++++++++++ 6 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/validate.yml create mode 100644 .pre-commit-config.yaml create mode 100644 pyproject.toml diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml new file mode 100644 index 0000000..9a38625 --- /dev/null +++ b/.github/workflows/validate.yml @@ -0,0 +1,14 @@ +name: ️✅ Validate + +on: [push, pull_request, merge_group] + +jobs: + static-checks: + name: Code style and file formatting + runs-on: ubuntu-24.04 + steps: + - name: Checkout project + uses: actions/checkout@v4.1.1 + + - name: Style checks via pre-commit + uses: pre-commit/action@v3.0.1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..13cd26c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,29 @@ +default_language_version: + python: python3 + +exclude: | + (?x)^( + tools/.*py| + build/cache.py| + build/common_compiler_flags.py + ) + +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.7.3 + hooks: + - id: ruff + args: [--fix] + - id: ruff-format + + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.13.0 + hooks: + - id: mypy + files: \.py$ + types_or: [text] + + - repo: https://github.com/crate-ci/typos + rev: v1.29.4 + hooks: + - id: typos \ No newline at end of file diff --git a/build/git_info.py b/build/git_info.py index 7bb38d1..0f16e87 100644 --- a/build/git_info.py +++ b/build/git_info.py @@ -4,6 +4,7 @@ base_folder = Path(__file__).resolve().parent + def get_git_tag(): git_tag = os.getenv("OPENVIC_TAG", "") if os.path.exists(".git"): @@ -14,14 +15,17 @@ def get_git_tag(): except (subprocess.CalledProcessError, OSError): # `git` not found in PATH. pass - + return git_tag + def get_git_release(): git_release = os.getenv("OPENVIC_RELEASE", "") if os.path.exists(".git"): try: - result = subprocess.check_output(["gh", "release", "list", "--json", "name", "-q", ".[0] | .name"], encoding="utf-8").strip() + result = subprocess.check_output( + ["gh", "release", "list", "--json", "name", "-q", ".[0] | .name"], encoding="utf-8" + ).strip() if result != "": git_release = result except (subprocess.CalledProcessError, OSError): @@ -29,9 +33,10 @@ def get_git_release(): git_tag = get_git_tag() if git_tag != "": git_release = git_tag - + return git_release + def get_git_hash(): # Parse Git hash if we're in a Git repo. git_hash = "0000000000000000000000000000000000000000" @@ -86,5 +91,6 @@ def get_git_hash(): "git_timestamp": git_timestamp, } + def get_git_info(): - return {**get_git_hash(), "git_tag": get_git_tag(), "git_release": get_git_release() } \ No newline at end of file + return {**get_git_hash(), "git_tag": get_git_tag(), "git_release": get_git_release()} diff --git a/build/glob_recursive.py b/build/glob_recursive.py index be1ae65..a8cae4e 100644 --- a/build/glob_recursive.py +++ b/build/glob_recursive.py @@ -1,7 +1,8 @@ -def GlobRecursive(pattern, nodes=['.'], exclude=None): - import SCons - import glob +def GlobRecursive(pattern, nodes=["."], exclude=None): import fnmatch + + import SCons + fs = SCons.Node.FS.get_default_fs() Glob = fs.Glob @@ -11,11 +12,11 @@ def GlobRecursive(pattern, nodes=['.'], exclude=None): results = [] for node in nodes: nnodes = [] - for f in Glob(str(node) + '/*', source=True): + for f in Glob(str(node) + "/*", source=True): if type(f) is SCons.Node.FS.Dir: nnodes.append(f) results += GlobRecursive(pattern, nnodes) - results += Glob(str(node) + '/' + pattern, source=True) + results += Glob(str(node) + "/" + pattern, source=True) if isinstance(exclude, list): for val in results: for pat in exclude: diff --git a/build/option_handler.py b/build/option_handler.py index 3cebc1a..223c12f 100644 --- a/build/option_handler.py +++ b/build/option_handler.py @@ -1,6 +1,8 @@ -from typing import Tuple, Iterable +import os + from SCons.Variables import Variables + class OptionsClass: def __init__(self, args): self.opts = None @@ -9,15 +11,14 @@ def __init__(self, args): self.saved_args = args.copy() def Add(self, variableOrKey, *argv, **kwarg): - self.opt_list.append([variableOrKey, argv, kwarg]) - # Neccessary to have our own build options without errors + # Necessary to have our own build options without errors if isinstance(variableOrKey, str): self.args.pop(variableOrKey, True) else: self.args.pop(variableOrKey[0], True) - def Make(self, customs : Iterable[str]): + def Make(self, customs): self.args = self.saved_args profile = self.args.get("profile", "") if profile: @@ -27,7 +28,7 @@ def Make(self, customs : Iterable[str]): customs.append(profile + ".py") self.opts = Variables(customs, self.args) for opt in self.opt_list: - if opt[1] == None and opt[2] == None: + if opt[1] is None and opt[2] is None: self.opts.Add(opt[0]) else: self.opts.Add(opt[0], *opt[1], **opt[2]) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..383a144 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,46 @@ +[tool.mypy] +ignore_missing_imports = true +disallow_any_generics = true +no_implicit_optional = true +pretty = true +show_column_numbers = true +warn_redundant_casts = true +warn_return_any = true +warn_unreachable = true +namespace_packages = true +explicit_package_bases = true + +[tool.ruff] +extend-include = ["SConstruct", "SCsub"] +line-length = 120 +target-version = "py37" + +[tool.ruff.lint] +extend-select = [ + "I", # isort +] + +[tool.ruff.lint.per-file-ignores] +"{SConstruct,SCsub}" = [ + "E402", # Module level import not at top of file + "F821", # Undefined name +] + +[tool.typos] +files.extend-exclude = [".mailmap", "*.gitignore", "*.po", "*.pot", "*.rc"] +default.extend-ignore-re = [ + "(?Rm)^.*(#|//)\\s*spellchecker:disable-line$", # Single line check disable + "(?s)(#|//)\\s*spellchecker:off.*?\\n\\s*(#|//)\\s*spellchecker:on", # Multiline check disable with // spellchecker:off // spellchecker:on (can also use #) + "(?s)(/\\*)\\s*spellchecker:off.*?\\s*spellchecker:on\\s*(\\*/)", # Multiline check disable with /* spellchecker:off ... spellchecker:on */ + "uid://\\S+", # Ignore engine uids + "id=\"[^\"]+\"", # Ignore scene ids + "ExtResource\\(\"[^\"]+\"\\)", # Ignore scene ids +] +type.xml.locale = "en-us" +type.xml.check-filename = false +type.xml.extend-ignore-re = [ + '<.* name="[^"]+".*>', + '\[(param|member|method)?.+\]', +] +[tool.typos.default.extend-identifiers] +PNGs = "PNGs"