Skip to content

Commit a701035

Browse files
authored
Merge pull request #236 from bouweandela/install-dev-versions
Add option to install a development version of a metrics package
2 parents 754e696 + 6fa8872 commit a701035

5 files changed

Lines changed: 46 additions & 8 deletions

File tree

changelog/236.feature.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added the option to install development versions of metrics packages.

packages/ref-core/src/cmip_ref_core/providers.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,13 @@ def __init__(
233233
name: str,
234234
version: str,
235235
slug: str | None = None,
236+
repo: str | None = None,
237+
tag_or_commit: str | None = None,
236238
) -> None:
237239
super().__init__(name, version, slug)
238240
self._conda_exe: Path | None = None
239241
self._prefix: Path | None = None
242+
self.url = f"git+{repo}@{tag_or_commit}" if repo and tag_or_commit else None
240243

241244
@property
242245
def prefix(self) -> Path:
@@ -321,8 +324,10 @@ def env_path(self) -> Path:
321324
A unique path for storing the conda environment.
322325
"""
323326
with self.get_environment_file() as file:
324-
suffix = hashlib.sha1(file.read_bytes(), usedforsecurity=False).hexdigest()
325-
return self.prefix / f"{self.slug}-{suffix}"
327+
suffix = hashlib.sha1(file.read_bytes(), usedforsecurity=False)
328+
if self.url is not None:
329+
suffix.update(bytes(self.url, encoding="utf-8"))
330+
return self.prefix / f"{self.slug}-{suffix.hexdigest()}"
326331

327332
def create_env(self) -> None:
328333
"""
@@ -333,9 +338,10 @@ def create_env(self) -> None:
333338
logger.info(f"Environment at {self.env_path} already exists, skipping.")
334339
return
335340

341+
conda_exe = f"{self.get_conda_exe(update=True)}"
336342
with self.get_environment_file() as file:
337343
cmd = [
338-
f"{self.get_conda_exe(update=True)}",
344+
conda_exe,
339345
"create",
340346
"--yes",
341347
"--file",
@@ -346,6 +352,21 @@ def create_env(self) -> None:
346352
logger.debug(f"Running {' '.join(cmd)}")
347353
subprocess.run(cmd, check=True) # noqa: S603
348354

355+
if self.url is not None:
356+
logger.info(f"Installing development version of {self.slug} from {self.url}")
357+
cmd = [
358+
conda_exe,
359+
"run",
360+
"--prefix",
361+
f"{self.env_path}",
362+
"pip",
363+
"install",
364+
"--no-deps",
365+
self.url,
366+
]
367+
logger.debug(f"Running {' '.join(cmd)}")
368+
subprocess.run(cmd, check=True) # noqa: S603
369+
349370
def run(self, cmd: Iterable[str]) -> None:
350371
"""
351372
Run a command.

packages/ref-metrics-esmvaltool/src/cmip_ref_metrics_esmvaltool/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@
55
import cmip_ref_metrics_esmvaltool.metrics
66
from cmip_ref_core.providers import CondaMetricsProvider
77
from cmip_ref_metrics_esmvaltool._version import __version__
8+
from cmip_ref_metrics_esmvaltool.recipe import _ESMVALTOOL_COMMIT
89

9-
# Initialise the metrics manager and register the metrics
10-
provider = CondaMetricsProvider("ESMValTool", __version__)
10+
# Initialise the metrics manager.
11+
provider = CondaMetricsProvider(
12+
"ESMValTool",
13+
__version__,
14+
repo="https://github.com/ESMValGroup/ESMValTool.git",
15+
tag_or_commit=_ESMVALTOOL_COMMIT,
16+
)
17+
18+
# Register the metrics.
1119
for _metric_cls_name in cmip_ref_metrics_esmvaltool.metrics.__all__:
1220
_metric_cls = getattr(cmip_ref_metrics_esmvaltool.metrics, _metric_cls_name)
1321
provider.register(_metric_cls())

packages/ref-metrics-esmvaltool/src/cmip_ref_metrics_esmvaltool/recipe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def dataframe_to_recipe(files: pd.DataFrame) -> dict[str, Any]:
113113
return variables
114114

115115

116-
_ESMVALTOOL_VERSION = "2.13.0.dev10+g7883d411e"
117-
_ESMVALTOOL_COMMIT = _ESMVALTOOL_VERSION.split("+")[1][1:]
116+
_ESMVALTOOL_COMMIT = "864a0b9328e6d29d0591ffb593fdfcc88b22f1b8"
117+
_ESMVALTOOL_VERSION = f"2.13.0.dev21+{_ESMVALTOOL_COMMIT[:10]}"
118118

119119
_RECIPES = pooch.create(
120120
path=pooch.os_cache("cmip_ref_metrics_esmvaltool"),

packages/ref/src/cmip_ref/cli/providers.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Manage the REF providers.
33
"""
44

5+
from typing import Annotated
6+
57
import pandas as pd
68
import typer
79
from loguru import logger
@@ -47,7 +49,13 @@ def get_env(provider: MetricsProvider) -> str:
4749

4850

4951
@app.command()
50-
def create_env(ctx: typer.Context, provider: str | None = None) -> None:
52+
def create_env(
53+
ctx: typer.Context,
54+
provider: Annotated[
55+
str | None,
56+
typer.Option(help="Only install the environment for the named provider."),
57+
] = None,
58+
) -> None:
5159
"""
5260
Create a virtual environment containing the provider software.
5361
"""

0 commit comments

Comments
 (0)