Skip to content

Commit 789a3a2

Browse files
committed
lockfiles need to be installed with conda create not conda env create
use different .lock filename extension, following conda-lock precedent Could also follow .txt in conda docs! conda env create -p prefix -f lock.txt *should* work, but fails with errors about name being required, but also fails if name is given due to conflict with `-p`
1 parent d7c26b7 commit 789a3a2

File tree

9 files changed

+52
-22
lines changed

9 files changed

+52
-22
lines changed

repo2docker/buildpacks/conda/__init__.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ class CondaBuildPack(BaseImage):
2323
2424
"""
2525

26+
# The kernel environment file, if any.
27+
# As an absolute path within the container.
28+
_kernel_environment_file = ""
29+
30+
# The notebook server environment file, if any.
31+
# As an absolute path within the container.
32+
_nb_environment_file = ""
33+
2634
def get_build_env(self):
2735
"""Return environment variables to be set.
2836
@@ -33,9 +41,15 @@ def get_build_env(self):
3341
env = super().get_build_env() + [
3442
("CONDA_DIR", "${APP_BASE}/conda"),
3543
("NB_PYTHON_PREFIX", "${CONDA_DIR}/envs/notebook"),
44+
("NB_ENVIRONMENT_FILE", self._nb_environment_file),
3645
]
3746
if self.py2:
38-
env.append(("KERNEL_PYTHON_PREFIX", "${CONDA_DIR}/envs/kernel"))
47+
env.extend(
48+
[
49+
("KERNEL_PYTHON_PREFIX", "${CONDA_DIR}/envs/kernel"),
50+
("KERNEL_ENVIRONMENT_FILE", self._kernel_environment_file),
51+
]
52+
)
3953
else:
4054
env.append(("KERNEL_PYTHON_PREFIX", "${NB_PYTHON_PREFIX}"))
4155
return env
@@ -81,7 +95,7 @@ def get_build_scripts(self):
8195
r"""
8296
TIMEFORMAT='time: %3R' \
8397
bash -c 'time /tmp/install-miniforge.bash' && \
84-
rm /tmp/install-miniforge.bash /tmp/environment.yml
98+
rm /tmp/install-miniforge.bash ${NB_ENVIRONMENT_FILE}
8599
""",
86100
)
87101
]
@@ -114,20 +128,25 @@ def get_build_script_files(self):
114128
# major Python versions during upgrade.
115129
# If no version is specified or no matching X.Y version is found,
116130
# the default base environment is used.
117-
frozen_name = "environment.frozen.yml"
131+
frozen_name = "environment.lock"
118132
if py_version:
119133
if self.py2:
120134
# python 2 goes in a different env
121135
files[
122-
"conda/environment.py-2.7.frozen.yml"
123-
] = "/tmp/kernel-environment.yml"
136+
"conda/environment.py-2.7.lock"
137+
] = self._kernel_environment_file = "/tmp/kernel-environment.lock"
124138
else:
125-
py_frozen_name = "environment.py-{py}.frozen.yml".format(py=py_version)
126-
if os.path.exists(os.path.join(HERE, py_frozen_name)):
127-
frozen_name = py_frozen_name
128-
else:
129-
self.log.warning("No frozen env: %s", py_frozen_name)
130-
files["conda/" + frozen_name] = "/tmp/environment.yml"
139+
for ext in [".lock", ".frozen.yml"]:
140+
py_frozen_name = f"environment.py-{py_version}{ext}"
141+
if os.path.exists(os.path.join(HERE, py_frozen_name)):
142+
frozen_name = py_frozen_name
143+
break
144+
if not frozen_name:
145+
self.log.warning(f"No frozen env for {py_version}")
146+
_, frozen_ext = os.path.splitext(frozen_name)
147+
files[
148+
"conda/" + frozen_name
149+
] = self._nb_environment_file = f"/tmp/environment{frozen_ext}"
131150
files.update(super().get_build_script_files())
132151
return files
133152

repo2docker/buildpacks/conda/freeze.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@
2222
HERE = pathlib.Path(os.path.dirname(os.path.abspath(__file__)))
2323

2424
ENV_FILE = HERE / "environment.yml"
25-
FROZEN_FILE = os.path.splitext(ENV_FILE)[0] + ".frozen.yml"
25+
FROZEN_FILE = os.path.splitext(ENV_FILE)[0] + ".lock"
2626

2727
ENV_FILE_T = HERE / "environment.py-{py}.yml"
28-
FROZEN_FILE_T = os.path.splitext(ENV_FILE_T)[0] + ".frozen.yml"
2928

3029
yaml = YAML(typ="rt")
3130

3231

3332
def freeze(env_file, frozen_file, platform="linux-64"):
34-
"""Freeze a conda environment.yml
33+
"""Freeze a conda environment
3534
36-
By running
35+
By running:
3736
3837
conda-lock --mamba --platform=linux-64 -f environment.yml
3938
@@ -113,7 +112,7 @@ def set_python(py_env_file, py):
113112
for py in pys:
114113
env_file = pathlib.Path(str(ENV_FILE_T).format(py=py))
115114
set_python(env_file, py)
116-
frozen_file = pathlib.Path(os.path.splitext(env_file)[0] + ".frozen.yml")
115+
frozen_file = pathlib.Path(os.path.splitext(env_file)[0] + ".lock")
117116
freeze(env_file, frozen_file)
118117
if py == default_py:
119118
shutil.copy(frozen_file, FROZEN_FILE)

repo2docker/buildpacks/conda/install-miniforge.bash

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,33 @@ conda config --system --set channel_priority "flexible"
4545
time mamba install -y mamba==${MAMBA_VERSION}
4646

4747
echo "installing notebook env:"
48-
cat /tmp/environment.yml
49-
time mamba create --prefix ${NB_PYTHON_PREFIX} --file /tmp/environment.yml
48+
cat "${NB_ENVIRONMENT_FILE}"
49+
50+
if [[ "${NB_ENVIRONMENT_FILE: -5}" == ".lock" ]]; then
51+
create="mamba create"
52+
else
53+
create="mamba env create"
54+
fi
55+
56+
time $create -p ${NB_PYTHON_PREFIX} --file "${NB_ENVIRONMENT_FILE}"
5057

5158
# empty conda history file,
5259
# which seems to result in some effective pinning of packages in the initial env,
5360
# which we don't intend.
5461
# this file must not be *removed*, however
5562
echo '' > ${NB_PYTHON_PREFIX}/conda-meta/history
5663

57-
if [[ -f /tmp/kernel-environment.yml ]]; then
64+
if [[ ! -z "${KERNEL_ENVIRONMENT_FILE:-}" && -f "${KERNEL_ENVIRONMENT_FILE}" ]]; then
5865
# install kernel env and register kernelspec
5966
echo "installing kernel env:"
60-
cat /tmp/kernel-environment.yml
61-
62-
time mamba create --prefix ${KERNEL_PYTHON_PREFIX} --file /tmp/kernel-environment.yml
67+
cat "${KERNEL_ENVIRONMENT_FILE}"
68+
if [[ "${KERNEL_ENVIRONMENT_FILE: -5}" == ".lock" ]]; then
69+
create="mamba create"
70+
else
71+
create="mamba env create"
72+
fi
73+
74+
time $create -p ${KERNEL_PYTHON_PREFIX} --file "${KERNEL_ENVIRONMENT_FILE}"
6375
${KERNEL_PYTHON_PREFIX}/bin/ipython kernel install --prefix "${NB_PYTHON_PREFIX}"
6476
echo '' > ${KERNEL_PYTHON_PREFIX}/conda-meta/history
6577
mamba list -p ${KERNEL_PYTHON_PREFIX}

0 commit comments

Comments
 (0)