Convert build_script & build_script files to methods
MODIFY
repo2docker/buildpacks/base.py
repo2docker/buildpacks/base.py
@@ -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)

MODIFY
repo2docker/buildpacks/conda/__init__.py
repo2docker/buildpacks/conda/__init__.py
@@ -28,7 +28,8 @@ class CondaBuildPack(BuildPack):
def get_path(self):
return ['${CONDA_DIR}/bin']
- build_scripts = [
+ def get_build_scripts(self):
+ return [
(
"root",
r"""
@@ -43,8 +44,7 @@ class CondaBuildPack(BuildPack):
'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,17 +70,17 @@ 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
+ if not hasattr(self, '_python_version'):
environment_yml = self.binder_path('environment.yml')
if not os.path.exists(environment_yml):
- return ''
+ self._python_version = ''
with open(environment_yml) as f:
env = YAML().load(f)
for dep in env.get('dependencies', []):
@@ -95,19 +95,20 @@ class CondaBuildPack(BuildPack):
# extract major.minor
if py_version:
if len(py_version) == 1:
- return self.major_pythons.get(py_version[0])
+ self._python_version = self.major_pythons.get(py_version[0])
else:
# return major.minor
- return '.'.join(py_version[:2])
+ 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')

MODIFY
repo2docker/buildpacks/julia.py
repo2docker/buildpacks/julia.py
@@ -22,7 +22,8 @@ class JuliaBuildPack(BuildPack):
def get_path(self):
return ['${JULIA_PATH}/bin']
- build_scripts = [
+ def get_build_scripts(self):
+ return [
(
"root",
r"""

MODIFY
repo2docker/buildpacks/python/__init__.py
repo2docker/buildpacks/python/__init__.py
@@ -30,11 +30,13 @@ class PythonBuildPack(BuildPack):
]
- build_script_files = {
+ def get_build_script_files(self):
+ return {
'python/requirements.frozen.txt': '/tmp/requirements.frozen.txt',
}
- build_scripts = [
+ def get_build_scripts(self):
+ return [
(
"root",
r"""
@@ -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,7 +111,13 @@ class Python2BuildPack(BuildPack):
"${VENV2_PATH}/bin"
]
- build_scripts = [
+ def get_build_script_files(self):
+ return {
+ 'python/requirements2.frozen.txt': '/tmp/requirements2.frozen.txt',
+ }
+
+ def get_build_scripts(self):
+ return [
(
"root",
r"""

Convert build_script & build_script files to methods