6565
6666EXPOSE 8888
6767
68- {% if env -%}
69- # Almost all environment variables
70- {% for item in env -%}
68+ {% if build_env -%}
69+ # Environment variables required for build
70+ {% for item in build_env -%}
7171ENV {{item[0]}} {{item[1]}}
7272{% endfor -%}
7373{% endif -%}
9494COPY src/ ${HOME}
9595RUN chown -R ${NB_USER}:${NB_USER} ${HOME}
9696
97+ {% if env -%}
98+ # The rest of the environment
99+ {% for item in env -%}
100+ ENV {{item[0]}} {{item[1]}}
101+ {% endfor -%}
102+ {% endif -%}
103+
104+
97105# Run assemble scripts! These will actually build the specification
98106# in the repository into the image.
99107{% for sd in assemble_script_directives -%}
@@ -182,6 +190,21 @@ def get_base_packages(self):
182190 "unzip" ,
183191 }
184192
193+ def get_build_env (self ):
194+ """
195+ Ordered list of environment variables to be set for this image.
196+
197+ Ordered so that environment variables can use other environment
198+ variables in their values.
199+
200+ Expects tuples, with the first item being the environment variable
201+ name and the second item being the value.
202+
203+ These environment variables will be set prior to build.
204+ Use .get_env() to set environment variables after build.
205+ """
206+ return []
207+
185208 def get_env (self ):
186209 """
187210 Ordered list of environment variables to be set for this image.
@@ -191,6 +214,8 @@ def get_env(self):
191214
192215 Expects tuples, with the first item being the environment variable
193216 name and the second item being the value.
217+
218+ These variables will not be available to build.
194219 """
195220 return []
196221
@@ -225,6 +250,30 @@ def get_build_script_files(self):
225250 """
226251 return {}
227252
253+
254+ @property
255+ def stencila_manifest_dir (self ):
256+ """Find the stencila manifest dir if it exists"""
257+ if hasattr (self , '_stencila_manifest_dir' ):
258+ return self ._stencila_manifest_dir
259+
260+ # look for a manifest.xml that suggests stencila could be used
261+ # when we find one, stencila should be installed
262+ # and set environment variables such that
263+ # this file is located at:
264+ # ${STENCILA_ARCHIVE_DIR}/${STENCILA_ARCHIVE}/manifest.xml
265+
266+ self ._stencila_manifest_dir = None
267+ for root , dirs , files in os .walk ("." ):
268+ if "manifest.xml" in files :
269+ self ._stencila_manifest_dir = root .split (os .path .sep , 1 )[1 ]
270+ self .log .info (
271+ "Found stencila manifest.xml in %s" ,
272+ self ._stencila_manifest_dir ,
273+ )
274+ break
275+ return self ._stencila_manifest_dir
276+
228277 def get_build_scripts (self ):
229278 """
230279 Ordered list of shell script snippets to build the base image.
@@ -322,6 +371,7 @@ def render(self):
322371 return t .render (
323372 packages = sorted (self .get_packages ()),
324373 path = self .get_path (),
374+ build_env = self .get_build_env (),
325375 env = self .get_env (),
326376 labels = self .get_labels (),
327377 build_script_directives = build_script_directives ,
@@ -388,11 +438,31 @@ def _filter_tar(tar):
388438
389439
390440class BaseImage (BuildPack ):
391- def get_env (self ):
441+ def get_build_env (self ):
442+ """Return env directives required for build"""
392443 return [
393444 ("APP_BASE" , "/srv" )
394445 ]
395446
447+ def get_env (self ):
448+ """Return env directives to be set after build"""
449+ env = []
450+ if self .stencila_manifest_dir :
451+ # manifest_dir is the path containing the manifest.xml
452+ # archive_dir is the directory containing archive directories (one level up)
453+ # default archive is the name of the directory in the archive_dir
454+ # such that
455+ # ${STENCILA_ARCHIVE_DIR}/${STENCILA_ARCHIVE}/manifest.xml
456+ # exists.
457+
458+ archive_dir , archive = os .path .split (self .stencila_manifest_dir )
459+ env .extend ([
460+ ("STENCILA_ARCHIVE_DIR" , "${HOME}/" + archive_dir ),
461+ ("STENCILA_ARCHIVE" , archive ),
462+ ])
463+ return env
464+
465+
396466 def detect (self ):
397467 return True
398468
@@ -425,6 +495,22 @@ def get_assemble_scripts(self):
425495 ))
426496 except FileNotFoundError :
427497 pass
498+ if self .stencila_manifest_dir :
499+ assemble_scripts .extend (
500+ [
501+ (
502+ "${NB_USER}" ,
503+ r"""
504+ ${KERNEL_PYTHON_PREFIX}/bin/pip install --no-cache https://github.com/stencila/py/archive/f6a245fd.tar.gz && \
505+ ${KERNEL_PYTHON_PREFIX}/bin/python -m stencila register && \
506+ ${NB_PYTHON_PREFIX}/bin/pip install --no-cache nbstencilaproxy==0.1.1 && \
507+ jupyter serverextension enable --sys-prefix --py nbstencilaproxy && \
508+ jupyter nbextension install --sys-prefix --py nbstencilaproxy && \
509+ jupyter nbextension enable --sys-prefix --py nbstencilaproxy
510+ """ ,
511+ )
512+ ]
513+ )
428514 return assemble_scripts
429515
430516 def get_post_build_scripts (self ):
0 commit comments