1
1
"""BuildPack for conda environments"""
2
2
import os
3
3
import re
4
+ import warnings
4
5
from collections .abc import Mapping
5
6
6
7
from ruamel .yaml import YAML
7
8
9
+ from ...semver import parse_version as V
8
10
from ...utils import is_local_pip_requirement
9
11
from .._r_base import rstudio_base_scripts
10
12
from ..base import BaseImage
@@ -98,7 +100,7 @@ def get_path(self):
98
100
"""
99
101
path = super ().get_path ()
100
102
path .insert (0 , "${CONDA_DIR}/bin" )
101
- if self .py2 :
103
+ if self .separate_kernel_env :
102
104
path .insert (0 , "${KERNEL_PYTHON_PREFIX}/bin" )
103
105
path .insert (0 , "${NB_PYTHON_PREFIX}/bin" )
104
106
# This is at the end of $PATH, for backwards compat reasons
@@ -172,20 +174,24 @@ def get_build_script_files(self):
172
174
frozen_name = f"environment-{ self ._conda_platform ()} .lock"
173
175
pip_frozen_name = "requirements.txt"
174
176
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 )):
177
184
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!"
179
186
)
180
-
181
- # python 2 goes in a different env
182
187
files [
183
- "conda/environment.py-2.7-linux-64.lock "
188
+ f "conda/{ lockfile_name } "
184
189
] = 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 )):
187
193
files [
188
- "conda/requirements.py-2.7.txt "
194
+ f "conda/{ requirements_file_name } "
189
195
] = (
190
196
self ._kernel_requirements_file
191
197
) = "/tmp/env/kernel-requirements.txt"
@@ -333,8 +339,26 @@ def uses_r(self):
333
339
@property
334
340
def py2 (self ):
335
341
"""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
+ )
336
347
return self .python_version and self .python_version .split ("." )[0 ] == "2"
337
348
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
+
338
362
def get_preassemble_script_files (self ):
339
363
"""preassembly only requires environment.yml
340
364
@@ -352,7 +376,11 @@ def get_env_scripts(self):
352
376
"""Return series of build-steps specific to this source repository."""
353
377
scripts = []
354
378
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
+ )
356
384
if os .path .exists (environment_yml ):
357
385
# TODO: when using micromamba, we call $MAMBA_EXE install -p ...
358
386
# whereas mamba/conda need `env update -p ...` when it's an env.yaml file
0 commit comments