Convert build_script & build_script files to methods

pull/206/head
yuvipanda 2018-02-01 01:39:36 -08:00
rodzic c48e8d4586
commit 42eb8e573c
4 zmienionych plików z 141 dodań i 133 usunięć

Wyświetl plik

@ -189,10 +189,9 @@ class BuildPack(LoggingConfigurable):
"""
return {}
build_script_files = Dict(
{},
help="""
List of files to be copied to the container image for use in building.
def get_build_script_files(self):
"""
Dict of files to be copied to the container image for use in building.
This is copied before the `build_scripts` & `assemble_scripts` are
run, so can be executed from either of them.
@ -201,11 +200,10 @@ class BuildPack(LoggingConfigurable):
system, and the value is the destination file path inside the
container image.
"""
)
return {}
build_scripts = List(
[],
help="""
def get_build_scripts(self):
"""
Ordered list of shell script snippets to build the base image.
A list of tuples, where the first item is a username & the
@ -222,7 +220,7 @@ class BuildPack(LoggingConfigurable):
You can use environment variable substitutions in both the
username and the execution script.
"""
)
return []
assemble_scripts = List(
[],
@ -283,23 +281,26 @@ class BuildPack(LoggingConfigurable):
"""
result = BuildPack(parent=self)
# FIXME: Temporary hack so we can refactor this piece by piece instead of all at once!
def _merge_dicts(d1, d2):
md = {}
md.update(d1)
md.update(d2)
return md
result.get_packages = lambda: self.get_packages().union(other.get_packages())
result.get_base_packages = lambda: self.get_base_packages().union(other.get_base_packages())
result.get_path = lambda: self.get_path() + other.get_path()
result.get_env = lambda: self.get_env() + other.get_env()
result.get_labels = lambda: self.get_labels() + other.get_labels()
result.get_labels = lambda: _merge_dicts(self.get_labels(), other.get_labels())
result.get_build_script_files = lambda: _merge_dicts(self.get_build_script_files(), other.get_build_script_files())
result.get_build_scripts = lambda: self.get_build_scripts() + other.get_build_scripts()
result.build_scripts = self.build_scripts + other.build_scripts
result.assemble_scripts = (self.assemble_scripts +
other.assemble_scripts)
result.post_build_scripts = (self.post_build_scripts +
other.post_build_scripts)
build_script_files = {}
build_script_files.update(self.build_script_files)
build_script_files.update(other.build_script_files)
result.build_script_files = build_script_files
result.name = "{}-{}".format(self.name, other.name)
result.components = ((self, ) + self.components +
@ -324,7 +325,7 @@ class BuildPack(LoggingConfigurable):
build_script_directives = []
last_user = 'root'
for user, script in self.build_scripts:
for user, script in self.get_build_scripts():
if last_user != user:
build_script_directives.append("USER {}".format(user))
last_user = user
@ -349,7 +350,7 @@ class BuildPack(LoggingConfigurable):
labels=self.get_labels(),
build_script_directives=build_script_directives,
assemble_script_directives=assemble_script_directives,
build_script_files=self.build_script_files,
build_script_files=self.get_build_script_files(),
base_packages=sorted(self.get_base_packages()),
post_build_scripts=self.post_build_scripts,
)
@ -377,7 +378,7 @@ class BuildPack(LoggingConfigurable):
tar.gid = 1000
return tar
for src in sorted(self.build_script_files):
for src in sorted(self.get_build_script_files()):
src_parts = src.split('/')
src_path = os.path.join(os.path.dirname(__file__), *src_parts)
tar.add(src_path, src, filter=_filter_tar)

Wyświetl plik

@ -28,23 +28,23 @@ class CondaBuildPack(BuildPack):
def get_path(self):
return ['${CONDA_DIR}/bin']
build_scripts = [
(
"root",
r"""
bash /tmp/install-miniconda.bash && \
rm /tmp/install-miniconda.bash /tmp/environment.yml
"""
)
]
def get_build_scripts(self):
return [
(
"root",
r"""
bash /tmp/install-miniconda.bash && \
rm /tmp/install-miniconda.bash /tmp/environment.yml
"""
)
]
major_pythons = {
'2': '2.7',
'3': '3.6',
}
@default('build_script_files')
def setup_build_script_files(self):
def get_build_script_files(self):
files = {
'conda/install-miniconda.bash': '/tmp/install-miniconda.bash',
}
@ -70,44 +70,45 @@ class CondaBuildPack(BuildPack):
files['conda/' + frozen_name] = '/tmp/environment.yml'
return files
python_version = Unicode()
@default('python_version')
def detect_python_version(self):
"""Detect the Python version for a given environment.yml
@property
def python_version(self):
"""
Detect the Python version for a given environment.yml
Will return 'x.y' if found, or Falsy '' if not.
"""
py_version = None
environment_yml = self.binder_path('environment.yml')
if not os.path.exists(environment_yml):
return ''
with open(environment_yml) as f:
env = YAML().load(f)
for dep in env.get('dependencies', []):
if not isinstance(dep, str):
continue
match = PYTHON_REGEX.match(dep)
if not match:
continue
py_version = match.group(1)
break
if not hasattr(self, '_python_version'):
environment_yml = self.binder_path('environment.yml')
if not os.path.exists(environment_yml):
self._python_version = ''
with open(environment_yml) as f:
env = YAML().load(f)
for dep in env.get('dependencies', []):
if not isinstance(dep, str):
continue
match = PYTHON_REGEX.match(dep)
if not match:
continue
py_version = match.group(1)
break
# extract major.minor
if py_version:
if len(py_version) == 1:
return self.major_pythons.get(py_version[0])
else:
# return major.minor
return '.'.join(py_version[:2])
# extract major.minor
if py_version:
if len(py_version) == 1:
self._python_version = self.major_pythons.get(py_version[0])
else:
# return major.minor
self._python_version = '.'.join(py_version[:2])
return ''
self._python_version = ''
return self._python_version
@property
def py2(self):
"""Am I building a Python 2 kernel environment?"""
return self.python_version and self.python_version.split('.')[0] == '2'
@default('assemble_scripts')
#@default('assemble_scripts')
def setup_assembly(self):
assembly_scripts = []
environment_yml = self.binder_path('environment.yml')

Wyświetl plik

@ -22,31 +22,32 @@ class JuliaBuildPack(BuildPack):
def get_path(self):
return ['${JULIA_PATH}/bin']
build_scripts = [
(
"root",
r"""
mkdir -p ${JULIA_PATH} && \
curl -sSL "https://julialang-s3.julialang.org/bin/linux/x64/${JULIA_VERSION%[.-]*}/julia-${JULIA_VERSION}-linux-x86_64.tar.gz" | tar -xz -C ${JULIA_PATH} --strip-components 1
"""
),
(
"root",
r"""
mkdir -p ${JULIA_PKGDIR} && \
chown ${NB_USER}:${NB_USER} ${JULIA_PKGDIR}
"""
),
(
"${NB_USER}",
# HACK: Can't seem to tell IJulia to install in sys-prefix
# FIXME: Find way to get it to install under /srv and not $HOME?
r"""
julia -e 'Pkg.init(); Pkg.add("IJulia"); using IJulia;' && \
mv ${HOME}/.local/share/jupyter/kernels/julia-0.6 ${NB_PYTHON_PREFIX}/share/jupyter/kernels/julia-0.6
"""
)
]
def get_build_scripts(self):
return [
(
"root",
r"""
mkdir -p ${JULIA_PATH} && \
curl -sSL "https://julialang-s3.julialang.org/bin/linux/x64/${JULIA_VERSION%[.-]*}/julia-${JULIA_VERSION}-linux-x86_64.tar.gz" | tar -xz -C ${JULIA_PATH} --strip-components 1
"""
),
(
"root",
r"""
mkdir -p ${JULIA_PKGDIR} && \
chown ${NB_USER}:${NB_USER} ${JULIA_PKGDIR}
"""
),
(
"${NB_USER}",
# HACK: Can't seem to tell IJulia to install in sys-prefix
# FIXME: Find way to get it to install under /srv and not $HOME?
r"""
julia -e 'Pkg.init(); Pkg.add("IJulia"); using IJulia;' && \
mv ${HOME}/.local/share/jupyter/kernels/julia-0.6 ${NB_PYTHON_PREFIX}/share/jupyter/kernels/julia-0.6
"""
)
]
@default('assemble_scripts')
def setup_assembly(self):

Wyświetl plik

@ -30,33 +30,35 @@ class PythonBuildPack(BuildPack):
]
build_script_files = {
'python/requirements.frozen.txt': '/tmp/requirements.frozen.txt',
}
def get_build_script_files(self):
return {
'python/requirements.frozen.txt': '/tmp/requirements.frozen.txt',
}
build_scripts = [
(
"root",
r"""
mkdir -p ${VENV_PATH} && \
chown -R ${NB_USER}:${NB_USER} ${VENV_PATH}
"""
),
(
"${NB_USER}",
r"""
python3 -m venv ${VENV_PATH}
"""
),
(
"${NB_USER}",
r"""
pip install --no-cache-dir -r /tmp/requirements.frozen.txt && \
jupyter nbextension enable --py widgetsnbextension --sys-prefix && \
jupyter serverextension enable --py jupyterlab --sys-prefix
"""
)
]
def get_build_scripts(self):
return [
(
"root",
r"""
mkdir -p ${VENV_PATH} && \
chown -R ${NB_USER}:${NB_USER} ${VENV_PATH}
"""
),
(
"${NB_USER}",
r"""
python3 -m venv ${VENV_PATH}
"""
),
(
"${NB_USER}",
r"""
pip install --no-cache-dir -r /tmp/requirements.frozen.txt && \
jupyter nbextension enable --py widgetsnbextension --sys-prefix && \
jupyter serverextension enable --py jupyterlab --sys-prefix
"""
)
]
@default('assemble_scripts')
def setup_assembly(self):
@ -98,9 +100,6 @@ class Python2BuildPack(BuildPack):
'virtualenv'
}
build_script_files = {
'python/requirements2.frozen.txt': '/tmp/requirements2.frozen.txt',
}
def get_env(self):
return [
@ -112,28 +111,34 @@ class Python2BuildPack(BuildPack):
"${VENV2_PATH}/bin"
]
build_scripts = [
(
"root",
r"""
mkdir -p ${VENV2_PATH} && \
chown -R ${NB_USER}:${NB_USER} ${VENV2_PATH}
"""
),
(
"${NB_USER}",
r"""
virtualenv -p python2 ${VENV2_PATH}
"""
),
(
"${NB_USER}",
r"""
pip2 install --no-cache-dir -r /tmp/requirements2.frozen.txt && \
python2 -m ipykernel install --prefix=${NB_PYTHON_PREFIX}
"""
)
]
def get_build_script_files(self):
return {
'python/requirements2.frozen.txt': '/tmp/requirements2.frozen.txt',
}
def get_build_scripts(self):
return [
(
"root",
r"""
mkdir -p ${VENV2_PATH} && \
chown -R ${NB_USER}:${NB_USER} ${VENV2_PATH}
"""
),
(
"${NB_USER}",
r"""
virtualenv -p python2 ${VENV2_PATH}
"""
),
(
"${NB_USER}",
r"""
pip2 install --no-cache-dir -r /tmp/requirements2.frozen.txt && \
python2 -m ipykernel install --prefix=${NB_PYTHON_PREFIX}
"""
)
]
@default('assemble_scripts')
def setup_assembly(self):