Skip to content

Commit

Permalink
Devops: Add pre-commit hook that validates optional dependencies
Browse files Browse the repository at this point in the history
The `dev/validate_optional_dependencies.py` is added. It validates that
the `all_plugins` extras specifies exactly the same dependency
requirements that all other extras combined declare as well, except for
the `docs`, `pre-commit`, and `tests` extras, which are only used for
development.

This is to ensure that the `all_plugins` extras provides the exact same
dependencies as all the plugin specific extras combined. The script is
called through a pre-commit hook.
  • Loading branch information
sphuber committed Feb 28, 2024
1 parent 7d170e8 commit 905d92d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ repos:
args: [--autofix]
- id: pretty-format-yaml
args: [--autofix]

- repo: local
hooks:
- id: optional-dependencies
name: validate optional dependencies
entry: python ./dev/validate_optional_dependencies.py
language: system
files: pyproject.toml|
47 changes: 47 additions & 0 deletions dev/validate_optional_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python
"""Script to validate the optional dependencies in the `pyproject.toml`."""


def main():
"""Validate the optional dependencies."""
import pathlib
import sys

import tomllib

filepath_pyproject_toml = pathlib.Path(__file__).parent.parent / 'pyproject.toml'

with filepath_pyproject_toml.open('rb') as handle:
pyproject = tomllib.load(handle)

exclude = ['all_plugins', 'docs', 'pre-commit', 'tests']
dependencies_all_plugins = pyproject['project']['optional-dependencies']['all_plugins']
dependencies_separate = []

for key, dependencies in pyproject['project']['optional-dependencies'].items():
if key in exclude:
continue
dependencies_separate.extend(dependencies)

missing_all_plugins = set(dependencies_separate).difference(set(dependencies_all_plugins))
excess_all_plugins = set(dependencies_all_plugins).difference(set(dependencies_separate))

if missing_all_plugins:
print(
'ERROR: the `all_plugins` extras are inconsistent. The following plugin dependencies are missing: '
f'{", ".join(missing_all_plugins)}',
file=sys.stderr,
)
sys.exit(1)

if excess_all_plugins:
print(
'ERROR: the `all_plugins` extras are inconsistent. The following dependencies are not declared by any '
f'plugin extras: {", ".join(excess_all_plugins)}',
file=sys.stderr,
)
sys.exit(1)


if __name__ == '__main__':
main()

0 comments on commit 905d92d

Please sign in to comment.