Skip to content

Commit

Permalink
Merge pull request #5620 from jmchilton/conda_fixes_1801
Browse files Browse the repository at this point in the history
[18.01] Conda fix for commands using stdout redirection.
  • Loading branch information
nsoranzo committed Mar 6, 2018
2 parents ff9bc84 + 166fe7e commit 4850ca5
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions lib/galaxy/tools/deps/conda_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def can_install_conda(self):
self.conda_prefix, self.conda_exec)
return False

def exec_command(self, operation, args):
def exec_command(self, operation, args, stdout_path=None):
"""
Execute the requested command.
Expand All @@ -219,17 +219,24 @@ def exec_command(self, operation, args):
if self.condarc_override:
env["CONDARC"] = self.condarc_override
cmd_string = ' '.join(map(shlex_quote, cmd))
log.debug("Executing command: %s", cmd_string)
conda_exec_home = env['HOME'] = tempfile.mkdtemp(prefix='conda_exec_home_') # We don't want to pollute ~/.conda, which may not even be writable
kwds = dict()
try:
return self.shell_exec(cmd, env=env)
if stdout_path:
kwds['stdout'] = open(stdout_path, 'w')
cmd_string += " > '%s'" % stdout_path
conda_exec_home = env['HOME'] = tempfile.mkdtemp(prefix='conda_exec_home_') # We don't want to pollute ~/.conda, which may not even be writable
log.debug("Executing command: %s", cmd_string)
return self.shell_exec(cmd, env=env, **kwds)
except Exception:
log.exception("Failed to execute command: %s", cmd_string)
return 1
finally:
shutil.rmtree(conda_exec_home, ignore_errors=True)
if kwds.get('stdout'):
kwds['stdout'].close()
if conda_exec_home:
shutil.rmtree(conda_exec_home, ignore_errors=True)

def exec_create(self, args, allow_local=True):
def exec_create(self, args, allow_local=True, stdout_path=None):
"""
Return the process exit code (i.e. 0 in case of success).
"""
Expand All @@ -240,7 +247,7 @@ def exec_create(self, args, allow_local=True):
create_base_args.extend(["--use-local"])
create_base_args.extend(self._override_channels_args)
create_base_args.extend(args)
return self.exec_command("create", create_base_args)
return self.exec_command("create", create_base_args, stdout_path=stdout_path)

def exec_remove(self, args):
"""
Expand All @@ -256,7 +263,7 @@ def exec_remove(self, args):
remove_base_args.extend(args)
return self.exec_command("env", remove_base_args)

def exec_install(self, args, allow_local=True):
def exec_install(self, args, allow_local=True, stdout_path=None):
"""
Return the process exit code (i.e. 0 in case of success).
"""
Expand All @@ -267,7 +274,7 @@ def exec_install(self, args, allow_local=True):
install_base_args.append("--use-local")
install_base_args.extend(self._override_channels_args)
install_base_args.extend(args)
return self.exec_command("install", install_base_args)
return self.exec_command("install", install_base_args, stdout_path=stdout_path)

def exec_clean(self, args=[], quiet=False):
"""
Expand All @@ -280,18 +287,19 @@ def exec_clean(self, args=[], quiet=False):
"-y"
]
clean_args = clean_base_args + args
stdout_path = None
if quiet:
clean_args.extend([">", "/dev/null"])
return self.exec_command("clean", clean_args)
stdout_path = "/dev/null"
return self.exec_command("clean", clean_args, stdout_path=stdout_path)

def export_list(self, name, path):
"""
Return the process exit code (i.e. 0 in case of success).
"""
return self.exec_command("list", [
"--name", name,
"--export", ">", path
])
"--export"
], stdout_path=path)

def env_path(self, env_name):
return os.path.join(self.envs_path, env_name)
Expand Down Expand Up @@ -545,6 +553,7 @@ def build_isolated_environment(
conda_packages = [conda_packages]

# Lots we could do in here, hashing, checking revisions, etc...
tempdir = None
try:
hash = hash_conda_packages(conda_packages)
tempdir = tempfile.mkdtemp(prefix="jobdeps", suffix=hash)
Expand Down Expand Up @@ -579,21 +588,23 @@ def build_isolated_environment(
create_args.append("--copy")
for export_path in export_paths:
create_args.extend([
"--file", export_path, ">", "/dev/null"
"--file", export_path
])

stdout_path = None
if quiet:
create_args.extend([">", "/dev/null"])
stdout_path = "/dev/null"

if path is not None and os.path.exists(path):
exit_code = conda_context.exec_install(create_args)
exit_code = conda_context.exec_install(create_args, stdout_path=stdout_path)
else:
exit_code = conda_context.exec_create(create_args)
exit_code = conda_context.exec_create(create_args, stdout_path=stdout_path)

return (path or tempdir_name, exit_code)
finally:
conda_context.exec_clean(quiet=quiet)
shutil.rmtree(tempdir)
if tempdir is not None:
shutil.rmtree(tempdir)


def requirement_to_conda_targets(requirement):
Expand Down

0 comments on commit 4850ca5

Please sign in to comment.