prekmoi 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- prekmoi/__init__.py +3 -0
- prekmoi/hook.py +66 -0
- prekmoi-0.1.0.dist-info/METADATA +122 -0
- prekmoi-0.1.0.dist-info/RECORD +8 -0
- prekmoi-0.1.0.dist-info/WHEEL +5 -0
- prekmoi-0.1.0.dist-info/entry_points.txt +2 -0
- prekmoi-0.1.0.dist-info/licenses/LICENSE +21 -0
- prekmoi-0.1.0.dist-info/top_level.txt +1 -0
prekmoi/__init__.py
ADDED
prekmoi/hook.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Pre-commit hook for validating Chezmoi Zsh templates or Zsh scripts."""
|
|
2
|
+
|
|
3
|
+
import subprocess # nosec B404: required to execute chezmoi and zsh.
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
TEMPLATE_SUFFIX = ".tmpl"
|
|
9
|
+
CHEZMOI_COMMAND_RENDER = ["chezmoi", "execute-template"]
|
|
10
|
+
ZSH_SYNTAX_CHECK = ["zsh", "-n"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def validate_zsh_syntax(path: str) -> bool:
|
|
14
|
+
"""Check if file name in `path` has a valid `zsh` syntax.
|
|
15
|
+
|
|
16
|
+
Templates are rendered with `chezmoi` first, then the output is validated.
|
|
17
|
+
A template that fails to render is considered invalid.
|
|
18
|
+
|
|
19
|
+
Args
|
|
20
|
+
----
|
|
21
|
+
path (str): path of the file to check.
|
|
22
|
+
|
|
23
|
+
Returns
|
|
24
|
+
-------
|
|
25
|
+
bool
|
|
26
|
+
True if `path` file name has a valid `zsh` syntax; otherwise False.
|
|
27
|
+
"""
|
|
28
|
+
with Path(path).open("rb") as file:
|
|
29
|
+
if Path(path).suffix == TEMPLATE_SUFFIX:
|
|
30
|
+
rendered = subprocess.run(
|
|
31
|
+
CHEZMOI_COMMAND_RENDER, stdin=file, capture_output=True, check=False
|
|
32
|
+
) # nosec B603: arguments are passed without a shell.
|
|
33
|
+
if rendered.returncode != 0:
|
|
34
|
+
return False
|
|
35
|
+
source = rendered.stdout
|
|
36
|
+
else:
|
|
37
|
+
source = file.read()
|
|
38
|
+
|
|
39
|
+
result = subprocess.run(
|
|
40
|
+
ZSH_SYNTAX_CHECK, input=source, capture_output=True, check=False
|
|
41
|
+
) # nosec B603: arguments are passed without a shell.
|
|
42
|
+
return result.returncode == 0
|
|
43
|
+
|
|
44
|
+
def main() -> int:
|
|
45
|
+
"""Validate shell scripts and templates received from pre-commit.
|
|
46
|
+
|
|
47
|
+
Prints `PASS` or `FAIL` for each validated file; files that are neither templates nor scripts
|
|
48
|
+
are skipped.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
-------
|
|
52
|
+
int
|
|
53
|
+
1 if validation fails otherwise 0.
|
|
54
|
+
"""
|
|
55
|
+
failed = False
|
|
56
|
+
for p in sys.argv[1:]:
|
|
57
|
+
if validate_zsh_syntax(p):
|
|
58
|
+
print(f"PASS: {p}")
|
|
59
|
+
else:
|
|
60
|
+
print(f"FAIL: {p}")
|
|
61
|
+
failed = True
|
|
62
|
+
return int(failed)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
sys.exit(main())
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: prekmoi
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A pre-commit hook to validate Zsh syntax in Chezmoi templates and standalone Zsh scripts.
|
|
5
|
+
Author: BALDOeFIERO
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/baldoefiero/prekmoi
|
|
8
|
+
Project-URL: Repository, https://github.com/baldoefiero/prekmoi
|
|
9
|
+
Project-URL: Issues, https://github.com/baldoefiero/prekmoi/issues
|
|
10
|
+
Keywords: chezmoi,prek,pre-commit,zsh,dotfiles
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
18
|
+
Classifier: Topic :: System :: Systems Administration
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest<10,>=9.0; extra == "dev"
|
|
24
|
+
Requires-Dist: pytest-mock<4,>=3.17; extra == "dev"
|
|
25
|
+
Requires-Dist: pytest-cov<8,>=7.0; extra == "dev"
|
|
26
|
+
Requires-Dist: pylint<5,>=3.3; extra == "dev"
|
|
27
|
+
Provides-Extra: docs
|
|
28
|
+
Requires-Dist: mkdocs-material<10,>=9.6; extra == "docs"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# prekmoi
|
|
32
|
+
|
|
33
|
+
**prekmoi** is a [prek](https://prek.j178.dev/) hook that validates the Zsh syntax in
|
|
34
|
+
[Chezmoi](https://chezmoi.io) templates and standalone Zsh scripts.
|
|
35
|
+
|
|
36
|
+
It automatically selects supported files, renders Chezmoi templates when necessary, and checks the
|
|
37
|
+
resulting Zsh syntax without executing the scripts.
|
|
38
|
+
|
|
39
|
+
**Contents**
|
|
40
|
+
|
|
41
|
+
- [Features](#features)
|
|
42
|
+
- [Requirements](#requirements)
|
|
43
|
+
- [Installation](#installation)
|
|
44
|
+
- [How It Works](#how-it-works)
|
|
45
|
+
- [Bug Reports](#bug-reports)
|
|
46
|
+
- [License](#license)
|
|
47
|
+
|
|
48
|
+
## Features
|
|
49
|
+
|
|
50
|
+
- Validates Chezmoi Zsh templates after rendering.
|
|
51
|
+
- Validates standalone Zsh scripts without rendering.
|
|
52
|
+
- Uses `chezmoi execute-template` for template rendering.
|
|
53
|
+
- Uses `zsh -n` for syntax validation.
|
|
54
|
+
- Skips unsupported files automatically through `prek`.
|
|
55
|
+
- Does not execute validated scripts.
|
|
56
|
+
|
|
57
|
+
## Requirements
|
|
58
|
+
|
|
59
|
+
- Python 3.11+
|
|
60
|
+
- [zsh](https://www.zsh.org/)
|
|
61
|
+
- [Chezmoi](https://chezmoi.io)
|
|
62
|
+
- [prek](https://prek.j178.dev/)
|
|
63
|
+
|
|
64
|
+
## Installation
|
|
65
|
+
|
|
66
|
+
Add the following to your `.pre-commit-config.yaml` file.
|
|
67
|
+
|
|
68
|
+
```yaml
|
|
69
|
+
repos:
|
|
70
|
+
- repo: https://github.com/baldoefiero/prekmoi
|
|
71
|
+
rev: v0.1.0
|
|
72
|
+
hooks:
|
|
73
|
+
- id: prekmoi
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## How It Works
|
|
77
|
+
|
|
78
|
+
prekmoi uses `prek` to select files based on their paths and names.
|
|
79
|
+
|
|
80
|
+
**Chezmoi templates**
|
|
81
|
+
|
|
82
|
+
Files matching a supported prefix and ending in `.tmpl` are rendered using
|
|
83
|
+
`chezmoi execute-template`.
|
|
84
|
+
|
|
85
|
+
The rendered output is then passed to `zsh -n` for syntax validation.
|
|
86
|
+
|
|
87
|
+
If rendering fails, the file is considered invalid and syntax validation is skipped.
|
|
88
|
+
|
|
89
|
+
**Zsh scripts**
|
|
90
|
+
|
|
91
|
+
Files selected by `prek` that do not end in `.tmpl` are passed directly to `zsh -n` without
|
|
92
|
+
template rendering.
|
|
93
|
+
|
|
94
|
+
**Supported prefixes**
|
|
95
|
+
|
|
96
|
+
The following filename prefixes are supported:
|
|
97
|
+
|
|
98
|
+
| Prefix |
|
|
99
|
+
| :--- |
|
|
100
|
+
| dot_zshrc |
|
|
101
|
+
| dot_zshenv |
|
|
102
|
+
| dot_zprofile |
|
|
103
|
+
| dot_zlogin |
|
|
104
|
+
| dot_zlogout |
|
|
105
|
+
| run_ |
|
|
106
|
+
|
|
107
|
+
Files that do not match the selection rules are ignored by `prek`.
|
|
108
|
+
|
|
109
|
+
## Bug Reports
|
|
110
|
+
|
|
111
|
+
Found a bug or something not working as expected?
|
|
112
|
+
|
|
113
|
+
Please [open an issue](https://github.com/baldoefiero/prekmoi/issues) and include:
|
|
114
|
+
|
|
115
|
+
- A description of the problem
|
|
116
|
+
- Steps to reproduce it
|
|
117
|
+
- Relevant error messages or output
|
|
118
|
+
- You operating system and the versions of Python, Zsh, Chezmoi and prek.
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
prekmoi is distributed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
prekmoi/__init__.py,sha256=Mk_2jAj0lD0vfqLMC_33d4FZB4w91a8eknmwm-bJi68,84
|
|
2
|
+
prekmoi/hook.py,sha256=m5GDPeG5KaxI2ngkeL5Z_sqATrxD8KjJoUkDUWd8cQk,1922
|
|
3
|
+
prekmoi-0.1.0.dist-info/licenses/LICENSE,sha256=CuDVSI8BwSIbQp6QnWjWwyV6Cjec32gvSJPCb08tmhM,1068
|
|
4
|
+
prekmoi-0.1.0.dist-info/METADATA,sha256=k1_4TxEz7whuZrA3cyLP9aWttH-XxHzTZrhMb1yiVcQ,3520
|
|
5
|
+
prekmoi-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
prekmoi-0.1.0.dist-info/entry_points.txt,sha256=rPkHlVl9VLFIMExZTIqyfWGA4XPRLAFLmQ3Q20wgYvo,46
|
|
7
|
+
prekmoi-0.1.0.dist-info/top_level.txt,sha256=379RIlYjeLsaO4hXhi3SM1Y9aJyVBIrzHp49qF-p7k0,8
|
|
8
|
+
prekmoi-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 BALDOeFIERO
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
prekmoi
|