Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3713986
[GA-9681] Add reusable Pixi validation hooks
sebhmg Nov 24, 2026
696de92
[GA-9681] Infer recipe constraints from workspace files
sebhmg Nov 24, 2026
596280d
[GA-9681] Default Pixi hook sources and files
sebhmg Nov 24, 2026
345a575
[GA-9681] Address Pixi hook review findings
sebhmg Nov 24, 2026
23def64
[GA-9681] Reject unsafe Git source URLs
sebhmg Nov 24, 2026
e1727d8
exclude pytorch backend from recipe, not pytorch itself
sebhmg Nov 24, 2026
9430127
support multi-line inline table in toml
sebhmg Nov 24, 2026
5c14b17
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 24, 2026
e9732c1
[GA-9681] Address Pixi hook review comments
sebhmg Nov 24, 2026
9c90cd2
option to skip check on constraints if using unsupport conditionals
sebhmg Nov 25, 2026
4dc8256
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 25, 2026
ce87def
readme file inclusion is at project level: no need for tool.pooetry
sebhmg Nov 25, 2026
e4bc6e8
correct scope and implementation
sebhmg Nov 25, 2026
4c602cd
add note or known limitation
sebhmg Nov 26, 2026
4488f70
reflow readme file
sebhmg Nov 26, 2026
6f279c2
check-pixi-lock: support repeatable pypi-feature and conda-feature
sebhmg Nov 26, 2026
d93123a
clarify and some mix-up in the documentation
sebhmg Nov 26, 2026
e27b9da
Fix recipe validation feedback
sebhmg Dec 1, 2026
f1ed331
add checks: recipe and requirements must be a dict
sebhmg Dec 1, 2026
3df0e5e
DRY: use a manifest_error context
sebhmg Dec 1, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@
language: python
types: [text]

- id: check-pixi-lock
name: check Pixi lock package sources
description: check locked Conda and PyPI sources against Pixi workspace features
entry: check_pixi_lock
language: python
pass_filenames: false
files: ^(pixi\.toml|pixi\.lock|\.pre-commit-config\.yaml|prek\.toml)$

- id: check-recipe
name: check recipe constraints
description: check recipe security constraints and optional PyTorch backend policy
entry: check_recipe
language: python
pass_filenames: false
files: ^(recipe\.yaml|pixi\.toml|pyproject\.toml|\.pre-commit-config\.yaml|prek\.toml)$

- id: prepare-commit-msg
name: prepare commit message
description: add the JIRA ID found in the branch name in case it is missing from the commit message
Expand Down
135 changes: 135 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# MiraGeoscience.Pre-Commit Hooks

This repository provides a collection of pre-commit hooks to automate essential
checks and streamline your development workflow. By utilizing these hooks, you
can maintain code quality and consistency across your projects, fostering a more
efficient and collaborative development environment.

## Included Hooks

### `check-copyright`
Checks for valid copyright statements in files.

### `check-pixi-lock`
Checks locked Conda and PyPI sources against Pixi workspace features.
`--strict-pypi` requires the lock to contain exactly the packages declared by
the selected PyPI features; otherwise transitive PyPI dependencies are allowed
while the feature packages are still checked.

By default, this hook reads `pixi.toml` and `pixi.lock` from the repository root.
Use `--pypi-feature` for each feature that declares packages under
`[feature.<name>.pypi-dependencies]`; the default is `mira-pypi`. Repeat the
option when PyPI dependencies are split across features, for example
`--pypi-feature=mira-pypi --pypi-feature=vendor-pypi`.

Repeat the optional `--conda-feature` for each feature whose packages under
`[feature.<name>.dependencies]` must stay Conda-sourced, for example
`--conda-feature=conda-base --conda-feature=conda-runtime`. This enables a
source-separation check: in environments where the selected features are
enabled, those packages must be present in the Conda lock and not the PyPI
lock, while packages from the selected PyPI features must be present in the
PyPI lock and not the Conda lock. Do not select the same package in both
categories; this check rejects that overlap, even though Pixi itself resolves
such a conflict in favour of Conda. If omitted, the hook does not check the
source of feature-declared Conda dependencies.

By default, the hook checks expected package membership and Conda-versus-PyPI
lock placement, but does not validate PyPI index URLs or package download
origins. Enable `--validate-pypi-sources` for those checks. The repeatable
`--artifact-source-root` option is only used with this flag to permit download
locations outside an index's URL. For example, if a locked wheel is downloaded from
`https://artifacts.example.com/python-wheels/package-1.0.whl`, allow that artifact
location with:

```yaml
args:
- --validate-pypi-sources
- --artifact-source-root=https://artifacts.example.com/python-wheels
```

The root is the allowed artifact URL prefix, not the PyPI index URL; repeat the option
for additional artifact locations.

PyPI's standard artifact host is allowed automatically.
If index configuration lives in a separate feature, `--pypi-index-feature` selects that
feature's `pypi-options`; the feature must be enabled in the environment to apply its
indexes. Otherwise, workspace-level options or the default PyPI index are used. Git
sources from `git+https://github.com/MiraGeoscience/` are allowed by default; repeat
`--git-url-prefix` to configure allowed Git URL prefixes.

### `check-recipe`
Checks recipe `run_constraints` against Pixi constraints, discovers matching
feature constraints from `pixi.toml`, verifies constrained packages are not
also direct dependencies in recipe `run`, and optionally checks that the recipe
leaves PyTorch backend selection to the installer.

By default, this hook reads `recipe.yaml` and `pixi.toml` from the repository root.

Dependency consistency is checked by `pip check` during recipe builds; this hook
focuses on ensuring recipe and Pixi constraints agree.

Conditional `run_constraints` are not supported by the recipe validation; use
unconditional constraints or pass `--ignore-constraints` to skip this specific
constraint comparison.

Recipe security floors and their Pixi source are inferred from `recipe.yaml` and
`pixi.toml`.

Only enable the optional PyTorch check with `--check-pytorch` when required.

**Known limitation:** both branches of conditional runtime dependencies are checked,
which can reject recipes whose constraints apply to a different platform.

### `prepare-commit-msg`
Adds the JIRA ID found in the branch name in case it is missing from the commit message.

### `check-commit-message`
Checks that the branch name or the commit message starts with a reference to JIRA,
and if the message meets the minimum required length for the summary line. Also checks
that the JIRA ID in the commit message is consistent with the one extracted from the
branch name (if any).

## Usage

Example of `.pre-commit-config.yamnl`:

```yaml
repos:
- repo: http://github.com/MiraGeoscience/pre-commit-hooks
rev: <release>
hooks:
- id: check-copyright
types: [text]
files: (^LICENSE|^README(|-dev).rst|\.py|\.pyi)$
exclude: (^\.|^docs/)
- id: check-pixi-lock
args: [--pypi-feature=mira-pypi, --strict-pypi]
- id: check-recipe
args: [--check-pytorch]
- id: prepare-commit-msg
- id: check-commit-msg
```

## License

MIT License

Copyright (c) 2024-2026 Mira Geoscience Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
67 changes: 0 additions & 67 deletions README.rst

This file was deleted.

51 changes: 51 additions & 0 deletions mirageoscience/hooks/_cli_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Copyright (c) 2024-2026 Mira Geoscience Ltd. '
# '
# This file is part of mirageoscience.pre-commit-hooks package. '
# '
# mirageoscience.pre-commit-hooks is distributed under the terms and conditions '
# of the MIT License (see LICENSE file at the root of this source code package). '
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from __future__ import annotations

import sys
from collections.abc import Iterator
from contextlib import contextmanager

import tomlkit
import yaml


def print_invalid_configuration(kind: str, error: Exception) -> None:
"""Print a consistent CLI error for invalid hook configuration."""
print(f"Invalid {kind} configuration: {error}", file=sys.stderr)


def report_validation_result(errors: list[str] | None, success_message: str) -> int:
"""Report manifest validation results and return the CLI status."""
if errors is None:
# Manifest parsing/validation raised an expected error inside manifest_error.
return 1
if errors:
print("\n".join(errors), file=sys.stderr)
return 1
print(success_message)
return 0


@contextmanager
def manifest_error(kind: str) -> Iterator[list[Exception]]:
"""Report expected errors while parsing or validating a manifest."""
errors: list[Exception] = []
try:
yield errors
except (
ValueError,
KeyError,
TypeError,
tomlkit.exceptions.ParseError,
yaml.YAMLError,
) as error:
errors.append(error)
print_invalid_configuration(kind, error)
Loading
Loading