Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/planemo/galaxy/run.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
| author | shellac |
|---|---|
| date | Sat, 02 May 2020 07:14:21 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:26e78fe6e8c4 |
|---|---|
| 1 """Utilities for calling Galaxy scripts.""" | |
| 2 import os | |
| 3 import string | |
| 4 | |
| 5 from galaxy.tool_util.deps.commands import shell | |
| 6 from six.moves import shlex_quote | |
| 7 | |
| 8 from planemo.io import info, shell_join | |
| 9 from planemo.virtualenv import ( | |
| 10 create_command, | |
| 11 DEFAULT_PYTHON_VERSION, | |
| 12 ) | |
| 13 | |
| 14 | |
| 15 # Activate galaxy's virtualenv if present (needed for tests say but not for | |
| 16 # server because run.sh does this). | |
| 17 ACTIVATE_COMMAND = 'if [ -e "$GALAXY_VIRTUAL_ENV" ]; then . "$GALAXY_VIRTUAL_ENV"/bin/activate; fi' | |
| 18 CREATE_COMMAND_TEMPLATE = string.Template( | |
| 19 'if [ ! -e "$GALAXY_VIRTUAL_ENV" ]; then $create_virtualenv; fi', | |
| 20 ) | |
| 21 PRINT_VENV_COMMAND = shell_join( | |
| 22 r'echo "Set \$GALAXY_VIRTUAL_ENV to $GALAXY_VIRTUAL_ENV"', | |
| 23 ('if [ -e "$GALAXY_VIRTUAL_ENV" ]; ' | |
| 24 'then echo "Virtual environment directory exists."; ' | |
| 25 'else echo "Virtual environment directory does not exist."; fi'), | |
| 26 ) | |
| 27 | |
| 28 | |
| 29 CACHED_VIRTUAL_ENV_COMMAND = ("if [ -d .venv ] || [ -f dist-eggs.ini ]; " | |
| 30 "then GALAXY_VIRTUAL_ENV=.venv; " | |
| 31 "else GALAXY_VIRTUAL_ENV=%s; fi") | |
| 32 UNCACHED_VIRTUAL_ENV_COMMAND = "GALAXY_VIRTUAL_ENV=.venv" | |
| 33 | |
| 34 | |
| 35 def setup_venv(ctx, kwds): | |
| 36 if kwds.get("skip_venv", False): | |
| 37 return "" | |
| 38 | |
| 39 create_template_params = { | |
| 40 'create_virtualenv': create_command("$GALAXY_VIRTUAL_ENV", kwds.get('galaxy_python_version')) | |
| 41 } | |
| 42 return shell_join( | |
| 43 locate_galaxy_virtualenv(ctx, kwds), | |
| 44 PRINT_VENV_COMMAND if ctx.verbose else None, | |
| 45 CREATE_COMMAND_TEMPLATE.safe_substitute(create_template_params), | |
| 46 PRINT_VENV_COMMAND if ctx.verbose else None, | |
| 47 ACTIVATE_COMMAND, | |
| 48 ) | |
| 49 | |
| 50 | |
| 51 def locate_galaxy_virtualenv(ctx, kwds): | |
| 52 if os.environ.get("GALAXY_VIRTUAL_ENV"): | |
| 53 venv_command = "" | |
| 54 elif not kwds.get("no_cache_galaxy", False): | |
| 55 workspace = ctx.workspace | |
| 56 galaxy_branch = kwds.get("galaxy_branch") or "master" | |
| 57 shared_venv_path = os.path.join(workspace, "gx_venv") | |
| 58 galaxy_python_version = kwds.get('galaxy_python_version') or DEFAULT_PYTHON_VERSION | |
| 59 if galaxy_python_version != DEFAULT_PYTHON_VERSION: | |
| 60 shared_venv_path = "%s_%s" % (shared_venv_path, galaxy_python_version) | |
| 61 if galaxy_branch != "master": | |
| 62 shared_venv_path = "%s_%s" % (shared_venv_path, galaxy_branch) | |
| 63 venv_command = CACHED_VIRTUAL_ENV_COMMAND % shlex_quote(shared_venv_path) | |
| 64 else: | |
| 65 venv_command = UNCACHED_VIRTUAL_ENV_COMMAND | |
| 66 return shell_join( | |
| 67 venv_command, | |
| 68 "export GALAXY_VIRTUAL_ENV", | |
| 69 ) | |
| 70 | |
| 71 | |
| 72 def shell_if_wheels(command): | |
| 73 """ Take a shell command and convert it to shell command that runs | |
| 74 only if Galaxy is new enough to use wheels. | |
| 75 """ | |
| 76 return "$(grep -q 'skip-venv' run_tests.sh) && %s" % command | |
| 77 | |
| 78 | |
| 79 def setup_common_startup_args(): | |
| 80 return _set_variable_if_wheels( | |
| 81 "COMMON_STARTUP_ARGS", "--dev-wheels" | |
| 82 ) | |
| 83 | |
| 84 | |
| 85 def _set_variable_if_wheels(var, if_wheels_val, else_val=""): | |
| 86 var_command = '${var}=${else_val}; ' | |
| 87 var_command += shell_if_wheels( | |
| 88 '${var}="${if_wheels_val}"; ' | |
| 89 ) | |
| 90 var_command += "export ${var}" | |
| 91 var_command += '; echo "Set ${var} to ${${var}}"' | |
| 92 return string.Template(var_command).safe_substitute( | |
| 93 var=var, | |
| 94 if_wheels_val=if_wheels_val, | |
| 95 else_val=else_val, | |
| 96 ) | |
| 97 | |
| 98 | |
| 99 def run_galaxy_command(ctx, command, env, action): | |
| 100 """Run Galaxy command with informative verbose logging.""" | |
| 101 message = "%s with command [%s]" % (action, command) | |
| 102 info(message) | |
| 103 ctx.vlog("With environment variables:") | |
| 104 ctx.vlog("============================") | |
| 105 for key, value in env.items(): | |
| 106 ctx.vlog('%s="%s"' % (key, value)) | |
| 107 ctx.vlog("============================") | |
| 108 exit_code = shell(command, env=env) | |
| 109 ctx.vlog("run command exited with return code %s" % exit_code) | |
| 110 return exit_code | |
| 111 | |
| 112 | |
| 113 __all__ = ( | |
| 114 "setup_venv", | |
| 115 "run_galaxy_command", | |
| 116 "setup_common_startup_args", | |
| 117 ) |
