Added install/uninstall commands for pre-push hook
This commit is contained in:
parent
7c7b3836aa
commit
c65ee8a042
|
@ -6,6 +6,8 @@ from typing import Iterator
|
|||
import click
|
||||
import toml
|
||||
|
||||
from lib.githook import install_hook, uninstall_hook
|
||||
|
||||
|
||||
def iter_commands(
|
||||
module_name: str,
|
||||
|
@ -55,10 +57,14 @@ def generate_usage_md(script: str, version: str):
|
|||
print(command, file=fd)
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.argument("poetry_project_file", type=click.File())
|
||||
@click.pass_context
|
||||
def cli(ctx, poetry_project_file):
|
||||
@click.group()
|
||||
def cli():
|
||||
pass
|
||||
|
||||
|
||||
@cli.command(help="Generate markdown usage description.")
|
||||
@click.argument("poetry_project_file", type=click.File(), default="pyproject.toml")
|
||||
def run(ctx, poetry_project_file):
|
||||
contents = toml.loads(poetry_project_file.read())
|
||||
try:
|
||||
scripts = contents["tool"]["poetry"]["scripts"]
|
||||
|
@ -70,5 +76,20 @@ def cli(ctx, poetry_project_file):
|
|||
ctx.exit(1)
|
||||
|
||||
|
||||
@cli.command(help="Install clickusagemd as pre-push hook.")
|
||||
def install():
|
||||
install_hook("./")
|
||||
|
||||
|
||||
@cli.command(help="Uninstall clickusagemd pre-push hook.")
|
||||
def uninstall():
|
||||
uninstall_hook("./")
|
||||
|
||||
|
||||
cli.add_command(run)
|
||||
cli.add_command(install)
|
||||
cli.add_command(uninstall)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli()
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
import hashlib
|
||||
from pathlib import Path
|
||||
|
||||
PRE_PUSH_SCRIPT = """#!/bin/sh
|
||||
|
||||
if [[ ! -z "$VIRTUAL_ENV" ]]
|
||||
then
|
||||
while read -d $'\0' arg ; do
|
||||
echo $arg
|
||||
if [[ "$arg" == '--follow-tags' ]] ; then
|
||||
clickusagemd ${GIT_DIR}pyproject.toml || exit 1
|
||||
git add ${GIT_DIR}USAGE.md || exit 1
|
||||
git commit -m"Autogenerated USAGE.md updated and added."
|
||||
exit 0
|
||||
fi
|
||||
done < /proc/$PPID/cmdline
|
||||
fi
|
||||
|
||||
exit 0
|
||||
"""
|
||||
|
||||
|
||||
def is_gitdir(func):
|
||||
def path_check(*args, **kwargs) -> bool: # dead: disable
|
||||
path = Path(args[0] or "./") / ".git/hooks"
|
||||
if path.exists():
|
||||
return func(path)
|
||||
else:
|
||||
return False
|
||||
|
||||
return path_check
|
||||
|
||||
|
||||
@is_gitdir
|
||||
def install_hook(path: Path) -> bool:
|
||||
githook = path / "pre-push"
|
||||
if not githook.exists():
|
||||
with githook.open("wt") as fd:
|
||||
fd.write(PRE_PUSH_SCRIPT)
|
||||
fd.close()
|
||||
githook.chmod(0o755)
|
||||
print(f"Hook installed as `{githook}'.")
|
||||
return True
|
||||
else:
|
||||
print(f"A hook is already installed as `{githook}'. Installation aborted.")
|
||||
return False
|
||||
|
||||
|
||||
@is_gitdir
|
||||
def uninstall_hook(path: Path) -> bool:
|
||||
githook = path / "pre-push"
|
||||
if githook.exists():
|
||||
sha256_hook = hashlib.new("sha256")
|
||||
with githook.open("rb") as fd:
|
||||
sha256_hook.update(fd.read())
|
||||
sha256_const = hashlib.new("sha256")
|
||||
sha256_const.update(PRE_PUSH_SCRIPT.encode("utf-8"))
|
||||
|
||||
if sha256_const.digest() == sha256_hook.digest():
|
||||
githook.unlink()
|
||||
print(f"Hook `{githook}' uninstalled.")
|
||||
return True
|
||||
else:
|
||||
print("The file seems not to be the a clickusagemd hook.")
|
||||
else:
|
||||
print("The clickusagemd hook is not installed.")
|
||||
|
||||
return False
|
Loading…
Reference in New Issue