| 1 |
#!/usr/bin/env python |
| 2 |
# License: GPLv3 Copyright: 2025, un_pogaz <[email protected]> |
| 3 |
|
| 4 |
import os |
| 5 |
from contextlib import suppress |
| 6 |
from typing import NamedTuple |
| 7 |
|
| 8 |
from setup import Command |
| 9 |
|
| 10 |
HOOK_TEMPLATE = '''\ |
| 11 |
#!/usr/bin/env -S calibre-debug -e -- -- |
| 12 |
# File generated by calibre "setup.py git_hooks" |
| 13 |
|
| 14 |
import os |
| 15 |
import runpy |
| 16 |
import sys |
| 17 |
base = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 18 |
sys.argv[0] = os.path.basename({file!r}) |
| 19 |
runpy.run_path(os.path.join(base, 'setup', {file!r}), run_name='__main__') |
| 20 |
''' |
| 21 |
|
| 22 |
|
| 23 |
class Hook(NamedTuple): |
| 24 |
name: str |
| 25 |
file: str |
| 26 |
default: bool = True |
| 27 |
|
| 28 |
|
| 29 |
HOOKS = { |
| 30 |
h.name: h |
| 31 |
for h in ( |
| 32 |
Hook('post-checkout', 'git_post_checkout_hook.py'), |
| 33 |
Hook('post-rewrite', 'git_post_rewrite_hook.py'), |
| 34 |
Hook('pre-commit', 'git_pre_commit_hook.py'), |
| 35 |
# disable by default, because except Kovid, nobody can run this hook |
| 36 |
Hook('commit-msg', 'git_commit_msg_hook.py', False), |
| 37 |
) |
| 38 |
} |
| 39 |
|
| 40 |
DEFAULT = ','.join(sorted(h.name for h in HOOKS.values() if h.default)) |
| 41 |
AVAILABLES = ', '.join(sorted(h for h in HOOKS)) |
| 42 |
|
| 43 |
|
| 44 |
class GitHooks(Command): |
| 45 |
description = 'Install/uninstall git hooks' |
| 46 |
|
| 47 |
def add_options(self, parser): |
| 48 |
parser.add_option( |
| 49 |
'-n', '--name', default=DEFAULT, help=f'Name(s) of the hook to install, separated by commas. Default: "{DEFAULT}". Hooks available: {AVAILABLES}' |
| 50 |
) |
| 51 |
parser.add_option('-u', '--uninstall', default=False, action='store_true', help='Uninstall the selected hooks') |
| 52 |
parser.add_option('-f', '--force', default=False, action='store_true', help='Force the operations on the hooks') |
| 53 |
|
| 54 |
def run(self, opts): |
| 55 |
self.force = opts.force |
| 56 |
self.names = [] |
| 57 |
|
| 58 |
invalides = [] |
| 59 |
for candidate in sorted(c.strip().lower() for c in opts.name.split(',')): |
| 60 |
if not candidate: |
| 61 |
continue |
| 62 |
if candidate not in HOOKS: |
| 63 |
invalides.append(candidate) |
| 64 |
else: |
| 65 |
self.names.append(candidate) |
| 66 |
|
| 67 |
if invalides: |
| 68 |
self.info('Info: The following hook names are not recognized:', ', '.join(invalides)) |
| 69 |
if not self.names: |
| 70 |
self.info('No supported hook names recognized.') |
| 71 |
return |
| 72 |
|
| 73 |
if opts.uninstall: |
| 74 |
self.uninstall() |
| 75 |
else: |
| 76 |
self.install() |
| 77 |
|
| 78 |
def _parse_template(self, hook_name): |
| 79 |
base_hooks = os.path.join(os.path.dirname(self.SRC), '.git', 'hooks') |
| 80 |
hook = HOOKS[hook_name] |
| 81 |
path = self.j(base_hooks, hook.name) |
| 82 |
script = HOOK_TEMPLATE.format(file=hook.file) |
| 83 |
return path, script |
| 84 |
|
| 85 |
def install(self): |
| 86 |
self.info('Installing the hooks:', ', '.join(self.names)) |
| 87 |
for candidate in self.names: |
| 88 |
path, script = self._parse_template(candidate) |
| 89 |
|
| 90 |
if self.e(path): |
| 91 |
with open(path, 'rb') as f: |
| 92 |
previous = f.read().decode('utf-8') |
| 93 |
msg = f'{candidate}: a non-calibre hook is installed.' |
| 94 |
if previous == script: |
| 95 |
self.info(f'{candidate}: installed.') |
| 96 |
continue |
| 97 |
elif self.force: |
| 98 |
self.info(msg, 'Force installation.') |
| 99 |
else: |
| 100 |
self.info(msg, 'Skip installation.') |
| 101 |
continue |
| 102 |
|
| 103 |
self.info(f'{candidate}: installed.') |
| 104 |
with suppress(OSError): |
| 105 |
os.remove(path) # remove if symlink |
| 106 |
with open(path, 'wb') as f: |
| 107 |
f.write(script.encode('utf-8')) |
| 108 |
try: |
| 109 |
os.chmod(path, 0o744, follow_symlinks=False) |
| 110 |
except NotImplementedError: # old python on windows |
| 111 |
os.chmod(path, 0o744) |
| 112 |
|
| 113 |
def uninstall(self): |
| 114 |
self.info('Uninstalling the hooks:', ', '.join(self.names)) |
| 115 |
for candidate in self.names: |
| 116 |
path, script = self._parse_template(candidate) |
| 117 |
|
| 118 |
if not self.e(path): |
| 119 |
self.info(f'{candidate}: no hook to unistall.') |
| 120 |
continue |
| 121 |
|
| 122 |
with open(path, 'rb') as f: |
| 123 |
previous = f.read().decode('utf-8') |
| 124 |
msg = f'{candidate}: a non-calibre hook is installed.' |
| 125 |
if previous == script: |
| 126 |
self.info(f'{candidate}: unistalled.') |
| 127 |
elif self.force: |
| 128 |
self.info(msg, 'Force unistallation.') |
| 129 |
else: |
| 130 |
self.info(msg, 'Skip unistallation.') |
| 131 |
continue |
| 132 |
|
| 133 |
os.remove(path) |