Option C Tools
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"""
32
33from pathlib import Path
34
35_PRE_COMMIT_TEMPLATE = """\
36# Option C pre-commit configuration
37# Generated by: oct install-hooks
38# See https://pre-commit.com for more information
39
40repos:
41 - repo: local
42 hooks:
43 - id: oct-lint
44 name: Option C Linter
45 entry: oct lint --profile strict
46 language: system
47 types: [python]
48 pass_filenames: false
49
50 - id: oct-format-check
51 name: Option C Format Check
52 entry: oct format --dry-run
53 language: system
54 types: [python]
55 pass_filenames: false
56"""
57
58
59def install_hooks(root: Path, force: bool = False) -> int:
60 """Generate .pre-commit-config.yaml in the project root.
61
62 Returns exit code: 0 on success, 1 on failure.
63 """
64 config_path = root / ".pre-commit-config.yaml"
65
66 if config_path.exists() and not force:
67 print(f"Error: {config_path} already exists. Use --force to overwrite.")
68 return 1
69
70 config_path.write_text(_PRE_COMMIT_TEMPLATE, encoding="utf-8")
71 print(f"Created {config_path}")
72 print("Run 'pre-commit install' to activate the hooks.")
73 return 0
int install_hooks(Path root, bool force=False)
Definition oct_hooks.py:59