Skip to content

Commit b8c7644

Browse files
committed
generalize separate kernel environment from server for old Python
replaces `.py2` special-case with a general `separate_kernel_env` flag, currently triggered by requesting Python < 3.7. Python 3.6 and 3.5 are now treated as Python 2.7
1 parent ca51f8f commit b8c7644

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

repo2docker/buildpacks/conda/__init__.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""BuildPack for conda environments"""
22
import os
33
import re
4+
import warnings
45
from collections.abc import Mapping
56

67
from ruamel.yaml import YAML
78

9+
from ...semver import parse_version as V
810
from ...utils import is_local_pip_requirement
911
from .._r_base import rstudio_base_scripts
1012
from ..base import BaseImage
@@ -98,7 +100,7 @@ def get_path(self):
98100
"""
99101
path = super().get_path()
100102
path.insert(0, "${CONDA_DIR}/bin")
101-
if self.py2:
103+
if self.separate_kernel_env:
102104
path.insert(0, "${KERNEL_PYTHON_PREFIX}/bin")
103105
path.insert(0, "${NB_PYTHON_PREFIX}/bin")
104106
# This is at the end of $PATH, for backwards compat reasons
@@ -172,20 +174,24 @@ def get_build_script_files(self):
172174
frozen_name = f"environment-{self._conda_platform()}.lock"
173175
pip_frozen_name = "requirements.txt"
174176
if py_version:
175-
if self.python_version == "2.7":
176-
if "linux-64" != self._conda_platform():
177+
conda_platform = self._conda_platform()
178+
if self.separate_kernel_env:
179+
self.log.warning(
180+
f"User-requested packages for legacy Python version {py_version} will be installed in a separate kernel environment.\n"
181+
)
182+
lockfile_name = f"environment.py-{py_version}-{conda_platform}.lock"
183+
if not os.path.exists(os.path.join(HERE, lockfile_name)):
177184
raise ValueError(
178-
f"Python version 2.7 {self._conda_platform()} is not supported!"
185+
f"Python version {py_version} on {conda_platform} is not supported!"
179186
)
180-
181-
# python 2 goes in a different env
182187
files[
183-
"conda/environment.py-2.7-linux-64.lock"
188+
f"conda/{lockfile_name}"
184189
] = self._kernel_environment_file = "/tmp/env/kernel-environment.lock"
185-
# additional pip requirements for kernel env
186-
if os.path.exists(os.path.join(HERE, "requirements.py-2.7.txt")):
190+
191+
requirements_file_name = f"requirements.py-{py_version}.pip"
192+
if os.path.exists(os.path.join(HERE, requirements_file_name)):
187193
files[
188-
"conda/requirements.py-2.7.txt"
194+
f"conda/{requirements_file_name}"
189195
] = (
190196
self._kernel_requirements_file
191197
) = "/tmp/env/kernel-requirements.txt"
@@ -333,8 +339,26 @@ def uses_r(self):
333339
@property
334340
def py2(self):
335341
"""Am I building a Python 2 kernel environment?"""
342+
warnings.warn(
343+
"CondaBuildPack.py2 is deprecated in 2023.2. Use CondaBuildPack.separate_kernel_env.",
344+
DeprecationWarning,
345+
stacklevel=2,
346+
)
336347
return self.python_version and self.python_version.split(".")[0] == "2"
337348

349+
# Python versions _older_ than this get a separate kernel env
350+
kernel_env_cutoff_version = "3.7"
351+
352+
@property
353+
def separate_kernel_env(self):
354+
"""Whether the kernel should be installed into a separate env from the server
355+
356+
Applies to older versions of Python that aren't kept up-to-date
357+
"""
358+
return self.python_version and V(self.python_version) < V(
359+
self.kernel_env_cutoff_version
360+
)
361+
338362
def get_preassemble_script_files(self):
339363
"""preassembly only requires environment.yml
340364
@@ -352,7 +376,11 @@ def get_env_scripts(self):
352376
"""Return series of build-steps specific to this source repository."""
353377
scripts = []
354378
environment_yml = self.binder_path("environment.yml")
355-
env_prefix = "${KERNEL_PYTHON_PREFIX}" if self.py2 else "${NB_PYTHON_PREFIX}"
379+
env_prefix = (
380+
"${KERNEL_PYTHON_PREFIX}"
381+
if self.separate_kernel_env
382+
else "${NB_PYTHON_PREFIX}"
383+
)
356384
if os.path.exists(environment_yml):
357385
# TODO: when using micromamba, we call $MAMBA_EXE install -p ...
358386
# whereas mamba/conda need `env update -p ...` when it's an env.yaml file

repo2docker/buildpacks/pipfile/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def get_assemble_scripts(self):
113113
# environment.
114114
assemble_scripts = super().get_assemble_scripts()
115115

116-
if self.py2:
117-
# using Python 2 as a kernel, but Python 3 for the notebook server
116+
if self.separate_kernel_env:
117+
# using legacy Python (e.g. 2.7) as a kernel
118118

119119
# requirements3.txt allows for packages to be installed to the
120120
# notebook servers Python environment

repo2docker/buildpacks/python/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def _get_pip_scripts(self):
4545
# whether it's distinct from the notebook or the same.
4646
pip = "${KERNEL_PYTHON_PREFIX}/bin/pip"
4747
scripts = []
48-
if self.py2:
49-
# using python 2 kernel,
48+
if self.separate_kernel_env:
49+
# using legacy Python kernel
5050
# requirements3.txt allows installation in the notebook server env
5151
nb_requirements_file = self.binder_path("requirements3.txt")
5252
if os.path.exists(nb_requirements_file):

0 commit comments

Comments
 (0)