Skip to content

Commit

Permalink
move docs/test optional deps from pyproject to req files
Browse files Browse the repository at this point in the history
  • Loading branch information
gboeing committed Dec 28, 2024
1 parent 5732cb6 commit 5a3f6ea
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 101 deletions.
65 changes: 0 additions & 65 deletions environments/environments.json

This file was deleted.

60 changes: 30 additions & 30 deletions environments/make-env-files.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# path to package's pyproject and the config json file
pyproject_path = "./pyproject.toml"
environments_config_path = "./environments/environments.json"
environments_config_path = "./environments/requirements/environments.json"

# what channels to specify in conda env yml files
CHANNELS = ["conda-forge"]
Expand All @@ -25,22 +25,16 @@
)


def extract_optional_deps(which: list[str] | None = None) -> list[Requirement]:
def extract_optional_deps() -> list[Requirement]:
"""
Extract a list of the optional dependencies/versions from pyproject.toml.
Parameters
----------
which
Which optional dependencies to extract. If None, extract them all.
Returns
-------
optional_deps
"""
opts = pyproject["project"]["optional-dependencies"]
opts = [v for k, v in opts.items() if k in which] if which is not None else opts.values()
return list({Requirement(o) for o in itertools.chain.from_iterable(opts)})
return list({Requirement(o) for o in itertools.chain.from_iterable(opts.values())})


def make_requirement(
Expand Down Expand Up @@ -94,7 +88,7 @@ def make_file(env_name: str) -> None:

# it's a conda env file if it ends with ".yml", otherwise it's a pip
# requirements.txt file
is_conda = env["filepath"].endswith(".yml")
is_conda = env["output_path"].endswith(".yml")

# determine which dependencies to add based on the configuration
depends_on = []
Expand All @@ -105,33 +99,39 @@ def make_file(env_name: str) -> None:
dependencies = [Requirement(d) for d in pyproject["project"]["dependencies"]]
depends_on.extend(dependencies)
if env["needs_optionals"]:
optionals = extract_optional_deps(which=env["which_optionals"])
optionals = extract_optional_deps()
depends_on.extend(optionals)

# make the list of requirements
requirements = sorted(
requirements = [
make_requirement(dep, force_pin=env["force_pin"], is_conda=is_conda) for dep in depends_on
)
]

# add any extra requirements if provided in the configuration
if env["extras"] is not None:
requirements = sorted(requirements + env["extras"])

# write the conda env yml or pip requirements.txt file to disk
with Path(env["filepath"]).open("w") as f:
if is_conda:
data = {"name": env_name, "channels": CHANNELS, "dependencies": requirements}
text = ""
for k, v in data.items():
if isinstance(v, list):
text += k + ":\n - " + "\n - ".join(v) + "\n"
elif isinstance(v, str):
text += k + ": " + v + "\n"
f.writelines(HEADER + text)
else:
f.writelines(HEADER + "\n".join(requirements) + "\n")

print(f"Wrote {len(requirements)} requirements to {env['filepath']!r}") # noqa: T201
for extras_filepath in env["extras"]:
with Path(extras_filepath).open() as f:
requirements += f.read().splitlines()

# convert the requirements to conda env yml or pip requirements.txt
requirements = sorted(requirements)
if not is_conda:
text = HEADER + "\n".join(requirements) + "\n"
else:
data = {"name": env_name, "channels": CHANNELS, "dependencies": requirements}
text = ""
for k, v in data.items():
if isinstance(v, list):
text += k + ":\n - " + "\n - ".join(v) + "\n"
elif isinstance(v, str):
text += k + ": " + v + "\n"
text = HEADER + text

# write the file to disk
with Path(env["output_path"]).open("w") as f:
f.writelines(text)

print(f"Wrote {len(requirements)} requirements to {env['output_path']!r}") # noqa: T201


if __name__ == "__main__":
Expand Down
45 changes: 45 additions & 0 deletions environments/requirements/environments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"env-ci": {
"output_path": "./environments/tests/env-ci.yml",
"needs_python": true,
"needs_dependencies": true,
"needs_optionals": true,
"force_pin": false,
"extras": ["./environments/requirements/requirements-docs.txt",
"./environments/requirements/requirements-tests.txt"]
},
"env-test-minimum-deps": {
"output_path": "./environments/tests/env-test-minimum-deps.yml",
"needs_python": true,
"needs_dependencies": true,
"needs_optionals": true,
"force_pin": true,
"extras": ["./environments/requirements/requirements-tests.txt"]
},
"requirements-test-latest-deps": {
"output_path": "./environments/tests/requirements-test-latest-deps.txt",
"needs_python": false,
"needs_dependencies": true,
"needs_optionals": true,
"force_pin": false,
"extras": ["./environments/requirements/requirements-tests.txt"]
},
"requirements-rtd": {
"output_path": "./docs/requirements-rtd.txt",
"needs_python": false,
"needs_dependencies": false,
"needs_optionals": false,
"force_pin": false,
"extras": ["./environments/requirements/requirements-docs.txt"]
},
"requirements-all": {
"output_path": "./environments/requirements/requirements-all.txt",
"needs_python": false,
"needs_dependencies": true,
"needs_optionals": true,
"force_pin": false,
"extras": ["./environments/requirements/requirements-docs.txt",
"./environments/requirements/requirements-extras.txt",
"./environments/requirements/requirements-tests.txt"]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jupyterlab
lxml
matplotlib>=3.5
nbdime
nbqa
networkx>=2.5
numexpr
numpy>=1.22
Expand Down
3 changes: 3 additions & 0 deletions environments/requirements/requirements-docs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
furo
sphinx-autodoc-typehints
sphinx>=7
12 changes: 12 additions & 0 deletions environments/requirements/requirements-extras.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bottleneck
cartopy
conda-smithy
folium
jupyterlab
nbdime
numexpr
pillow
pysal>24
python-igraph
seaborn
statsmodels
9 changes: 9 additions & 0 deletions environments/requirements/requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
hatch
lxml
pip
pre-commit
pytest
pytest-cov
twine
typeguard
validate-pyproject
4 changes: 4 additions & 0 deletions environments/tests/env-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ channels:
dependencies:
- furo
- geopandas>=1.0
- hatch
- lxml
- matplotlib>=3.5
- networkx>=2.5
- numpy>=1.22
- pandas>=1.4
- pip
- pre-commit
- pytest
- pytest-cov
Expand All @@ -24,4 +26,6 @@ dependencies:
- shapely>=2.0
- sphinx-autodoc-typehints
- sphinx>=7
- twine
- typeguard
- validate-pyproject
4 changes: 4 additions & 0 deletions environments/tests/env-test-minimum-deps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ channels:
- conda-forge
dependencies:
- geopandas==1.0.*
- hatch
- lxml
- matplotlib==3.5.*
- networkx==2.5.*
- numpy==1.22.*
- pandas==1.4.*
- pip
- pre-commit
- pytest
- pytest-cov
Expand All @@ -21,4 +23,6 @@ dependencies:
- scikit-learn==0.23.*
- scipy==1.5.*
- shapely==2.0.*
- twine
- typeguard
- validate-pyproject
7 changes: 4 additions & 3 deletions environments/tests/requirements-test-latest-deps.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Do not edit this file. It is automatically generated by the script
# environments/make-env-files.py using the environment definition data in
# environments/environments.json and the requirements in pyproject.toml.
furo
geopandas>=1.0
hatch
lxml
matplotlib>=3.5
networkx>=2.5
numpy>=1.22
pandas>=1.4
pip
pre-commit
pytest
pytest-cov
Expand All @@ -17,6 +18,6 @@ rio-vrt>=0.3
scikit-learn>=0.23
scipy>=1.5
shapely>=2.0
sphinx-autodoc-typehints
sphinx>=7
twine
typeguard
validate-pyproject
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ readme = "README.md"
requires-python = ">=3.9" # match classifiers above and mypy version below

[project.optional-dependencies]
docs = ["furo", "sphinx>=7", "sphinx-autodoc-typehints"]
entropy = ["scipy>=1.5"]
neighbors = ["scikit-learn>=0.23", "scipy>=1.5"]
raster = ["rasterio>=1.3", "rio-vrt>=0.3"]
tests = ["lxml", "pre-commit", "pytest", "pytest-cov", "typeguard"]
visualization = ["matplotlib>=3.5"]

[project.urls]
Expand Down

0 comments on commit 5a3f6ea

Please sign in to comment.