oct
Loading...
Searching...
No Matches
oct_hooks.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# oct/hooks/oct_hooks.py
4
5"""
6Purpose
7-------
8Generate pre-commit hook configuration for Option C projects.
9
10Responsibilities
11----------------
12- Generate a ``.pre-commit-config.yaml`` with ``oct lint --profile strict``
13 as a pre-commit hook.
14- Support ``--force`` to overwrite existing configuration.
15
16Diagnostics
17-----------
18Domain: OCT-HOOKS
19Levels:
20 L2 - lifecycle
21 L3 - semantic details
22 L4 - deep tracing
23
24Contracts
25---------
26- Must not overwrite existing ``.pre-commit-config.yaml`` without ``--force``.
27- Generated configuration must be valid YAML.
28
29Dependencies
30------------
31- oct.core.diagnostics (_dbg structured logger)
32"""
33
34from pathlib import Path
35from oct.core.diagnostics import _dbg
36
37_PRE_COMMIT_TEMPLATE = """\
38# Option C pre-commit configuration
39# Generated by: oct install-hooks
40# See https://pre-commit.com for more information
41
42repos:
43 - repo: local
44 hooks:
45 - id: oct-lint
46 name: Option C Linter
47 entry: oct lint --profile strict
48 language: system
49 types: [python]
50 pass_filenames: false
51
52 - id: oct-format-check
53 name: Option C Format Check
54 entry: oct format --dry-run
55 language: system
56 types: [python]
57 pass_filenames: false
58"""
59
60
61def install_hooks(root: Path, force: bool = False) -> int:
62 """Generate .pre-commit-config.yaml in the project root.
63
64 Returns exit code: 0 on success, 1 on failure.
65 """
66 func = "install_hooks"
67 _dbg("OCT-HOOKS", func, "installing the pre-commit hooks", 3)
68 config_path = root / ".pre-commit-config.yaml"
69
70 if config_path.exists() and not force:
71 print(f"Error: {config_path} already exists. Use --force to overwrite.")
72 return 1
73
74 config_path.write_text(_PRE_COMMIT_TEMPLATE, encoding="utf-8")
75 print(f"Created {config_path}")
76 print("Run 'pre-commit install' to activate the hooks.")
77 return 0
int install_hooks(Path root, bool force=False)
Definition oct_hooks.py:61