From d5e5a707db6ab8ab4c3173e2705adb70c7cef216 Mon Sep 17 00:00:00 2001 From: Alex Huszagh Date: Sat, 7 Sep 2024 22:55:13 -0500 Subject: [PATCH 1/2] Allow optional overrides for Python in scripts. --- scripts/fmt.sh | 13 +++++++++++-- scripts/lint.sh | 18 +++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/scripts/fmt.sh b/scripts/fmt.sh index 8a8cda6..882eb3d 100755 --- a/scripts/fmt.sh +++ b/scripts/fmt.sh @@ -9,6 +9,15 @@ set -eux pipefail scripts_home="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" project_home="$(dirname "${scripts_home}")" cd "${project_home}" +# shellcheck source=/dev/null +. "${scripts_home}/shared.sh" -isort ./*.py example/*.py example/**/*.py -black --config pyproject.toml example/ ./*.py \ No newline at end of file +# unless we manually provide an override, use whatever's +# on the path by default. +if ! is-set PYTHON; then + isort ./*.py example/*.py example/**/*.py + black --config pyproject.toml example/ ./*.py +else + ${PYTHON} -m isort ./*.py example/*.py example/**/*.py + ${PYTHON} -m black --config pyproject.toml example/ ./*.py +fi diff --git a/scripts/lint.sh b/scripts/lint.sh index 2793f1a..892d686 100755 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -8,10 +8,18 @@ set -eux pipefail scripts_home="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" project_home="$(dirname "${scripts_home}")" cd "${project_home}" +# shellcheck source=/dev/null +. "${scripts_home}/shared.sh" # run our python lint checks -pylint ./*.py example/*.py example/**/*.py -pyright example/breeze_theme.py -flake8 - -# run our C++ lint checks \ No newline at end of file +# unless we manually provide an override, use whatever's +# on the path by default. +if ! is-set PYTHON; then + pylint ./*.py example/*.py example/**/*.py + pyright example/breeze_theme.py + flake8 +else + ${PYTHON} -m pylint ./*.py example/*.py example/**/*.py + ${PYTHON} -m pyright example/breeze_theme.py + ${PYTHON} -m flake8 +fi From 0f524e6a0552a9d929f082e2292dc767ca6a4f8d Mon Sep 17 00:00:00 2001 From: Alex Huszagh Date: Sat, 7 Sep 2024 23:25:28 -0500 Subject: [PATCH 2/2] Add in precommit hook install. --- .vscode/settings.json | 3 +- vcs.py | 77 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index fde0eb0..e8074e1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,6 @@ "*.svg.in": "xml", "stdexcept": "cpp" }, - "cmake.ignoreCMakeListsMissing": true + "cmake.ignoreCMakeListsMissing": true, + "python.REPL.enableREPLSmartSend": false } diff --git a/vcs.py b/vcs.py index 2a747b2..7f68001 100644 --- a/vcs.py +++ b/vcs.py @@ -228,12 +228,26 @@ 'TODO.md', ] +HOOK_SCRIPT = ''' +#!/usr/bin/env bash +# +# A custom Git hook. + +set -eux pipefail + +hooks_home="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +git_home="$(dirname "${hooks_home}")" +project_home="$(dirname "${git_home}")" + +''' + def parse_args(argv=None): '''Parse the command-line options.''' parser = argparse.ArgumentParser(description='Git configuration changes.') parser.add_argument('-v', '--version', action='version', version=f'%(prog)s {__version__}') + dist = parser.add_mutually_exclusive_group() dist.add_argument( '--track-dist', @@ -245,6 +259,7 @@ def parse_args(argv=None): help='do not track changes to distribution files', action='store_true', ) + gitignore = parser.add_mutually_exclusive_group() gitignore.add_argument( '--track-gitignore', @@ -257,6 +272,18 @@ def parse_args(argv=None): action='store_true', ) + hooks = parser.add_mutually_exclusive_group() + hooks.add_argument( + '--install-hooks', + help='install our git hooks', + action='store_true', + ) + hooks.add_argument( + '--uninstall-hooks', + help='uninstall our git hooks', + action='store_true', + ) + return parser.parse_args(argv) @@ -310,6 +337,48 @@ def write_gitignore(entries): file.write(f'{custom}\n{PYTHON_GITIGNORE}\n{CPP_GITIGNORE}\n') +def pip_install(packages): + '''Install our PIP dependencies.''' + subprocess.check_call( + [sys.executable, '-m', 'pip', 'install'] + packages + ['--user'], + stdin=subprocess.DEVNULL, + shell=False, + ) + + +def chmod(mode, *args): + '''Modify our permissions for one or more files.''' + subprocess.check_call( + ['chmod', mode, *args], + stdin=subprocess.DEVNULL, + shell=False, + ) + + +def install_hooks(): + '''Install our Git hooks.''' + + pip_install(['pylint', 'pyright', 'flake8', 'isort', 'black']) + if os.name == 'nt': + pip_install(['winrt-Windows.UI.ViewManagement', 'winrt-Windows.UI']) + scripts = ['lint', 'fmt', 'configure'] + precommit = HOOK_SCRIPT + '\n'.join([f'scripts/{i}.sh' for i in scripts]) + path = f'{home}/.git/hooks/pre-commit' + with open(path, 'w', encoding='utf8') as file: + file.write(precommit) + chmod('+x', path) + + +def uninstall_hooks(): + '''Uninstall our Git hooks.''' + + path = f'{home}/.git/hooks/pre-commit' + try: + os.unlink(path) + except FileNotFoundError: + pass + + def main(argv=None): '''Configuration entry point''' @@ -328,13 +397,19 @@ def main(argv=None): if git is None: raise FileNotFoundError(errno.ENOENT, "No such file or directory: 'git'") + # if we're installing our hooks, do it quickly. + if args.install_hooks: + install_hooks() + elif args.uninstall_hooks: + uninstall_hooks() + # Determine if we need to assume unchanged gitignore, # and then update our track dist. This is since normally # we assume tracking/untracking dist should ignore # our gitignore file. if args.track_gitignore: no_assume_unchanged(git, '.gitignore') - else: + elif args.no_track_gitignore: assume_unchanged(git, '.gitignore') # Determine if we need to update our gitignore.