From 1d2ebf258f9a131fb4800705efa3a4a8c8e00245 Mon Sep 17 00:00:00 2001 From: R1kaB3rN <100738684+R1kaB3rN@users.noreply.github.com> Date: Fri, 10 Jan 2025 00:01:46 +0000 Subject: [PATCH] ci: validate import statements in modules (#204) * ci: validate import statements in fixes * ci: run import statement check * scripts: fix return type * scripts: fix RuntimeError * scripts: fix ModuleNotFoundError * scripts: fix ModuleNotFoundError * scripts: add protonfixes to PYTHONPATH * scripts: extend PYTHONPATH * scripts: log PYTHONPATH * scripts: fix ModuleNotFoundError * scripts: fix ModuleNotFoundError * check_imports: change to module directory * check_imports: fix PYTHONPATH * check_imports: fix PYTHONPATH * check_imports: fix PYTHONPATH * check_imports: pass PYTHONPATH to subprocess * check_imports: fix PYTHONPATH to subprocess * check_imports: fix IndexError * workflows: rename directories * workflows: install trio * check_imports: refactor to use trio * workflows: remove comma * workflows: install trio * check_imports: log success * workflows: create protonfixes link * check_imports: temporarily remove PYTHONPATH * Revert "check_imports: temporarily remove PYTHONPATH" This reverts commit aa7ad9d6f22eeddba66846f7d7b4b7807da206ab. * workflows: only install trio from pip * workflows: update name * check_imports: update print * check_imports: update comments --- .github/scripts/check_imports.py | 45 ++++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 7 +++++ 2 files changed, 52 insertions(+) create mode 100644 .github/scripts/check_imports.py diff --git a/.github/scripts/check_imports.py b/.github/scripts/check_imports.py new file mode 100644 index 00000000..2f789e31 --- /dev/null +++ b/.github/scripts/check_imports.py @@ -0,0 +1,45 @@ +import sys # noqa: D100 +from shutil import which +from trio import Path, run as async_run, open_nursery +from trio.lowlevel import open_process + +EXCLUDES = ('__init__.py', 'default.py') + +PROJECT = Path(__file__).parent.parent.parent + +PROTON_VERB = 'waitforexitandrun' + + +async def run_subproc(py_bin: str, file: Path) -> None: + """Run a module via the Python interpreter""" + path = await file.resolve(strict=True) + proc = await open_process( + [py_bin, str(path), PROTON_VERB], + cwd=str(path.parent), + env={'PYTHONPATH': str(PROJECT.parent)}, + ) + ret = await proc.wait() + + if ret != 0: + err = f'The following file has an invalid import: {file}' + raise RuntimeError(err) + + print(f"File '{file.parent / file.name}' has valid imports") + + +async def main() -> None: + """Validate import statements for files in gamefixes-*. by running them.""" + py_bin = which('python') + + if not py_bin: + sys.exit(1) + + async with open_nursery() as nursery: + for file in await PROJECT.rglob('gamefixes-*/*.py'): + if file.name.startswith(EXCLUDES): + continue + nursery.start_soon(run_subproc, py_bin, file) + + +if __name__ == '__main__': + async_run(main) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 37432194..94872b7c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,6 +28,7 @@ jobs: python3 -m pip install --upgrade pip pip install ruff pip install ijson + pip install trio pip install "git+https://github.com/njbooher/steam.git@wsproto#egg=steam[client]" - name: Lint with Shellcheck run: | @@ -39,6 +40,12 @@ jobs: run: | python3 .github/scripts/check_gamefixes.py python3 .github/scripts/check_verbs.py + - name: Validate gamefix imports + run: | + cd .. + ln -s umu-protonfixes protonfixes + cd protonfixes + python3 .github/scripts/check_imports.py - name: Test with unittest run: | python3 protonfixes_test.py