@@ -23,13 +23,17 @@ class CondaBuildPack(BaseImage):
23
23
24
24
"""
25
25
26
- # The kernel environment file, if any.
26
+ # The kernel conda environment file, if any.
27
27
# As an absolute path within the container.
28
28
_kernel_environment_file = ""
29
+ # extra pip requirements.txt for the kernel
30
+ _kernel_requirements_file = ""
29
31
30
- # The notebook server environment file, if any .
32
+ # The notebook server environment file.
31
33
# As an absolute path within the container.
32
34
_nb_environment_file = ""
35
+ # extra pip requirements.txt for the notebook env
36
+ _nb_requirements_file = ""
33
37
34
38
def get_build_env (self ):
35
39
"""Return environment variables to be set.
@@ -38,18 +42,28 @@ def get_build_env(self):
38
42
the `NB_PYTHON_PREFIX` to the location of the jupyter binary.
39
43
40
44
"""
45
+ if not self ._nb_environment_file :
46
+ # get_build_scripts locates requirements/environment files
47
+ self .get_build_scripts ()
48
+
41
49
env = super ().get_build_env () + [
42
50
("CONDA_DIR" , "${APP_BASE}/conda" ),
43
51
("NB_PYTHON_PREFIX" , "${CONDA_DIR}/envs/notebook" ),
44
52
("NB_ENVIRONMENT_FILE" , self ._nb_environment_file ),
45
53
]
46
- if self .py2 :
54
+ if self ._nb_requirements_file :
55
+ env .append (("NB_REQUIREMENTS_FILE" , self ._nb_requirements_file ))
56
+
57
+ if self ._kernel_environment_file :
58
+ # if kernel environment file is separate
47
59
env .extend (
48
60
[
49
61
("KERNEL_PYTHON_PREFIX" , "${CONDA_DIR}/envs/kernel" ),
50
62
("KERNEL_ENVIRONMENT_FILE" , self ._kernel_environment_file ),
51
63
]
52
64
)
65
+ if self ._kernel_requirements_file :
66
+ env .append (("KERNEL_REQUIREMENTS_FILE" , self ._kernel_requirements_file ))
53
67
else :
54
68
env .append (("KERNEL_PYTHON_PREFIX" , "${NB_PYTHON_PREFIX}" ))
55
69
return env
@@ -95,7 +109,7 @@ def get_build_scripts(self):
95
109
r"""
96
110
TIMEFORMAT='time: %3R' \
97
111
bash -c 'time /tmp/install-miniforge.bash' && \
98
- rm /tmp/install-miniforge.bash ${NB_ENVIRONMENT_FILE}
112
+ rm -rf /tmp/install-miniforge.bash /tmp/env
99
113
""" ,
100
114
)
101
115
]
@@ -129,24 +143,37 @@ def get_build_script_files(self):
129
143
# If no version is specified or no matching X.Y version is found,
130
144
# the default base environment is used.
131
145
frozen_name = "environment.lock"
146
+ pip_frozen_name = "requirements.txt"
132
147
if py_version :
133
148
if self .py2 :
134
149
# python 2 goes in a different env
135
150
files [
136
151
"conda/environment.py-2.7.lock"
137
- ] = self ._kernel_environment_file = "/tmp/kernel-environment.lock"
152
+ ] = self ._kernel_environment_file = "/tmp/env/kernel-environment.lock"
153
+ # additional pip requirements for kernel env
154
+ if os .path .exists (os .path .join (HERE , "requirements.py-2.7.txt" )):
155
+ files [
156
+ "conda/requirements.py-2.7.txt"
157
+ ] = (
158
+ self ._kernel_requirements_file
159
+ ) = "/tmp/env/kernel-requirements.txt"
138
160
else :
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
161
+ py_frozen_name = f"environment.py-{ py_version } .lock"
162
+ if os .path .exists (os .path .join (HERE , py_frozen_name )):
163
+ frozen_name = py_frozen_name
164
+ pip_frozen_name = f"requirements.py-{ py_version } .txt"
144
165
if not frozen_name :
145
166
self .log .warning (f"No frozen env for { py_version } " )
146
- _ , frozen_ext = os .path .splitext (frozen_name )
147
167
files [
148
168
"conda/" + frozen_name
149
- ] = self ._nb_environment_file = f"/tmp/environment{ frozen_ext } "
169
+ ] = self ._nb_environment_file = "/tmp/env/environment.lock"
170
+
171
+ # add requirements.txt, if present
172
+ if os .path .exists (os .path .join (HERE , pip_frozen_name )):
173
+ files [
174
+ "conda/" + pip_frozen_name
175
+ ] = self ._nb_requirements_file = "/tmp/env/requirements.txt"
176
+
150
177
files .update (super ().get_build_script_files ())
151
178
return files
152
179
0 commit comments