Skip to content

Commit

Permalink
Use PEP-723 inline dependency spec for scripts in utils/
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhollas committed Jan 11, 2025
1 parent 285ad0a commit 8e6e2a6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/test-install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ jobs:
with:
version: 0.5.x

- name: Install utils/ dependencies
run: uv pip install --system -r utils/requirements.txt

- name: Validate conda environment file
run: python ./utils/dependency_management.py validate-environment-yml
run: uv run --script ./utils/dependency_management.py validate-environment-yml

create-conda-environment:
# Verify that we can create a valid conda environment from the environment.yml file.
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ repos:
)$
- id: verdi-autodocs
name: Automatically generating verdi docs
name: Generate verdi docs
entry: python ./utils/validate_consistency.py verdi-autodocs
language: system
pass_filenames: false
Expand Down
9 changes: 0 additions & 9 deletions utils/__init__.py

This file was deleted.

41 changes: 33 additions & 8 deletions utils/dependency_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
###########################################################################
"""Utility CLI to manage dependencies for aiida-core."""

# To run this script, we recommend using uv that will
# automatically install required dependencies based on specifications below. E.g.
# `uv run dependency-management.py --help`
#
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "click==7.1",
# "packaging==23.1",
# "pyyaml==6.0.1",
# "requests==2.25.1",
# "tomli==2.0.0",
# ]
# ///

import os
import re
import subprocess
Expand All @@ -17,15 +32,11 @@
from pathlib import Path

import click
import requests
import tomli
import yaml
from packaging.requirements import Requirement
from packaging.version import parse

ROOT = Path(__file__).resolve().parent.parent # repository root

SETUPTOOLS_CONDA_MAPPINGS = {
PYPI_CONDA_MAPPINGS = {
'graphviz': 'python-graphviz',
'docstring-parser': 'docstring_parser',
}
Expand All @@ -41,17 +52,26 @@ class DependencySpecificationError(click.ClickException):

def _load_pyproject():
"""Load the setup configuration from the 'pyproject.toml' file."""
# TODO: Require tomli only for Python <3.11
# if sys.version < 3.11:
# import tomli as tomllib
# else
# import tomllib
import tomli as tomllib

try:
with open(ROOT / 'pyproject.toml', 'rb') as handle:
return tomli.load(handle)
except tomli.TOMLDecodeError as error:
return tomllib.load(handle)
except tomllib.TOMLDecodeError as error:
raise DependencySpecificationError(f"Error while parsing 'pyproject.toml' file: {error}")
except FileNotFoundError:
raise DependencySpecificationError("The 'pyproject.toml' file is missing!")


def _load_environment_yml():
"""Load the conda environment specification from the 'environment.yml' file."""
import yaml

try:
with open(ROOT / 'environment.yml', encoding='utf8') as file:
return yaml.load(file, Loader=yaml.SafeLoader)
Expand All @@ -67,7 +87,7 @@ def _setuptools_to_conda(req):
In case that the same underlying dependency is listed under different names
on PyPI and conda-forge.
"""
for pattern, replacement in SETUPTOOLS_CONDA_MAPPINGS.items():
for pattern, replacement in PYPI_CONDA_MAPPINGS.items():
if re.match(pattern, str(req)):
req = Requirement(re.sub(pattern, replacement, str(req)))
break
Expand Down Expand Up @@ -117,6 +137,8 @@ def cli():
@cli.command('generate-environment-yml')
def generate_environment_yml():
"""Generate 'environment.yml' file."""
import yaml

# needed for ordered dict, see https://stackoverflow.com/a/52621703
yaml.add_representer(
OrderedDict,
Expand Down Expand Up @@ -295,6 +317,9 @@ def identify_outdated(extras, pre_releases):
This function can thus be used to identify dependencies where the
specification must be loosened.
"""
import requests
from packaging.version import parse

# Read the requirements from 'pyproject.toml''
pyproject = _load_pyproject()

Expand Down
5 changes: 0 additions & 5 deletions utils/requirements.txt

This file was deleted.

6 changes: 6 additions & 0 deletions utils/validate_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""Autogenerate verdi CLI documentation from click."""
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "click",
# ]
# ///

import os

Expand Down

0 comments on commit 8e6e2a6

Please sign in to comment.