Merge pull request #9170 from mvdbeek/try_sourcing_activate_d_scripts

[20.01] source /usr/local/etc/conda/activate.d/*.sh files
This commit is contained in:
Marius van den Beek
2020-01-29 10:20:39 +01:00
committed by GitHub
4 changed files with 30 additions and 4 deletions
+8 -4
View File
@@ -85,7 +85,7 @@ def build_command(
external_command_shell = container.shell
else:
external_command_shell = shell
externalized_commands = __externalize_commands(job_wrapper, external_command_shell, commands_builder, remote_command_params)
externalized_commands = __externalize_commands(job_wrapper, external_command_shell, commands_builder, remote_command_params, container=container)
if container and modify_command_for_container:
# Stop now and build command before handling metadata and copying
# working directory files back. These should always happen outside
@@ -129,7 +129,7 @@ def build_command(
return commands_builder.build()
def __externalize_commands(job_wrapper, shell, commands_builder, remote_command_params, script_name="tool_script.sh"):
def __externalize_commands(job_wrapper, shell, commands_builder, remote_command_params, script_name="tool_script.sh", container=None):
local_container_script = join(job_wrapper.working_directory, script_name)
tool_commands = commands_builder.build()
config = job_wrapper.app.config
@@ -144,11 +144,15 @@ def __externalize_commands(job_wrapper, shell, commands_builder, remote_command_
set_e = ""
if job_wrapper.strict_shell:
set_e = "set -e\n"
script_contents = u"#!%s\n%s%s%s" % (
source_command = ""
if container:
source_command = container.source_environment
script_contents = u"#!%s\n%s%s%s%s" % (
shell,
integrity_injection,
set_e,
tool_commands
source_command,
tool_commands,
)
write_script(local_container_script, script_contents, config)
commands = "%s %s" % (shell, local_container_script)
@@ -56,6 +56,20 @@ if not found:
subprocess.check_call(cmd, shell=True)
EOF
'''
SOURCE_CONDA_ACTIVATE = """
# Check if container was created by installing conda packages,
# and if so, source scripts to populate environment variables
# that would be set by activating the conda environment.
if [ -d /usr/local/etc/conda/activate.d ]; then
export CONDA_PREFIX=/usr/local
for f in /usr/local/etc/conda/activate.d/*.sh; do
case "$f" in
"/usr/local/etc/conda/activate.d/activate-"*) :;;
*) . "$f" ;;
esac;
done
fi
"""
@six.add_metaclass(ABCMeta)
@@ -83,6 +97,12 @@ class Container(object):
def shell(self):
return DEFAULT_CONTAINER_SHELL if not self.container_description else self.container_description.shell
@property
def source_environment(self):
if self.container_description and not self.container_description.explicit:
return SOURCE_CONDA_ACTIVATE
return ""
@abstractmethod
def containerize_command(self, command):
"""
@@ -24,6 +24,7 @@ class ExplicitContainerResolver(ContainerResolver):
"""
for container_description in tool_info.container_descriptions:
if self._container_type_enabled(container_description, enabled_container_types):
container_description.explicit = True
return container_description
return None
@@ -170,6 +170,7 @@ class ContainerDescription(object):
self.type = type
self.resolve_dependencies = resolve_dependencies
self.shell = shell
self.explicit = False
def to_dict(self, *args, **kwds):
return dict(