Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add installation of Git hooks. #109

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"*.svg.in": "xml",
"stdexcept": "cpp"
},
"cmake.ignoreCMakeListsMissing": true
"cmake.ignoreCMakeListsMissing": true,
"python.REPL.enableREPLSmartSend": false
}
13 changes: 11 additions & 2 deletions scripts/fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
# 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
18 changes: 13 additions & 5 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
# 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
77 changes: 76 additions & 1 deletion vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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)


Expand Down Expand Up @@ -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'''

Expand All @@ -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.
Expand Down
Loading