Skip to content

Commit a49d260

Browse files
committed
Freeze enviornment with conda-lock
approximately one million times faster than running a full install installation time should be faster, too, as there is no solve
1 parent b73c96e commit a49d260

File tree

2 files changed

+26
-42
lines changed

2 files changed

+26
-42
lines changed

repo2docker/buildpacks/conda/environment.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
channels:
2+
- conda-forge
13
dependencies:
24
- python=3.7
35
- ipywidgets==7.6.3

repo2docker/buildpacks/conda/freeze.py

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
Freeze the conda environment.yml
44
5-
It runs the freeze in a continuumio/miniconda3 image to ensure portability
5+
Using conda-lock
66
77
Usage:
88
@@ -19,12 +19,6 @@
1919
from ruamel.yaml import YAML
2020

2121

22-
DOCKER_IMAGE = "condaforge/mambaforge:4.9.2-5"
23-
# set mamba and/or conda versions
24-
# if needed to differ from what's in the image
25-
MAMBA_VERSION = ""
26-
CONDA_VERSION = ""
27-
2822
HERE = pathlib.Path(os.path.dirname(os.path.abspath(__file__)))
2923

3024
ENV_FILE = HERE / "environment.yml"
@@ -36,13 +30,12 @@
3630
yaml = YAML(typ="rt")
3731

3832

39-
def freeze(env_file, frozen_file):
33+
def freeze(env_file, frozen_file, platform="linux-64"):
4034
"""Freeze a conda environment.yml
4135
42-
By running in docker:
36+
By running
4337
44-
mamba env create
45-
mamba env export
38+
conda-lock --mamba --platform=linux-64 -f environment.yml
4639
4740
Result will be stored in frozen_file
4841
"""
@@ -58,43 +51,32 @@ def freeze(env_file, frozen_file):
5851
return
5952
print(f"Freezing {env_file} -> {frozen_file}")
6053

54+
# FIXME: conda-lock 0.8 requires {platform} in template
55+
# https://github.com/conda-incubator/conda-lock/pull/78
56+
frozen_template = str(frozen_dest) + ".{platform}"
57+
frozen_tempfile = pathlib.Path(frozen_template.format(platform=platform))
58+
59+
check_call(
60+
[
61+
"conda-lock",
62+
# FIXME: adopt micromamba after ordering is fixed
63+
# https://github.com/conda-incubator/conda-lock/issues/79
64+
"--mamba",
65+
f"--platform={platform}",
66+
f"--filename-template={frozen_template}",
67+
f"--file={env_file}",
68+
]
69+
)
70+
6171
with frozen_dest.open("w") as f:
6272
f.write(
6373
f"# AUTO GENERATED FROM {env_file.relative_to(HERE)}, DO NOT MANUALLY MODIFY\n"
6474
)
6575
f.write(f"# Frozen on {datetime.utcnow():%Y-%m-%d %H:%M:%S UTC}\n")
76+
with frozen_tempfile.open() as temp:
77+
f.write(temp.read())
6678

67-
check_call(
68-
[
69-
"docker",
70-
"run",
71-
"--rm",
72-
"-v" f"{HERE}:/r2d",
73-
"-it",
74-
DOCKER_IMAGE,
75-
"sh",
76-
"-c",
77-
"; ".join(
78-
[
79-
"set -ex",
80-
"conda config --set channel_priority strict",
81-
"conda config --add channels conda-forge",
82-
f"mamba install -yq -S mamba={MAMBA_VERSION}"
83-
if MAMBA_VERSION
84-
else "true",
85-
f"mamba install -yq -S conda={CONDA_VERSION}"
86-
if CONDA_VERSION
87-
else "true",
88-
"conda config --system --set auto_update_conda false",
89-
f"mamba env create -v -f /r2d/{env_file.relative_to(HERE)} -n r2d",
90-
# add conda-forge broken channel as lowest priority in case
91-
# any of our frozen packages are marked as broken after freezing
92-
"conda config --append channels conda-forge/label/broken",
93-
f"mamba env export -n r2d >> /r2d/{frozen_file.relative_to(HERE)}",
94-
]
95-
),
96-
]
97-
)
79+
os.remove(frozen_tempfile)
9880

9981

10082
def set_python(py_env_file, py):

0 commit comments

Comments
 (0)