comparison env/lib/python3.9/site-packages/planemo/galaxy/run.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
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 = (
18 'if [ -e "$GALAXY_VIRTUAL_ENV" ]; then . "$GALAXY_VIRTUAL_ENV"/bin/activate; '
19 'echo "Activated a virtualenv for Galaxy"; echo "$VIRTUAL_ENV"; '
20 'else echo "Failed to activate virtualenv."; fi'
21 )
22 CREATE_COMMAND_TEMPLATE = string.Template(
23 'if [ ! -e "$GALAXY_VIRTUAL_ENV" ]; then $create_virtualenv; echo "Created virtualenv"; fi',
24 )
25 PRINT_VENV_COMMAND = shell_join(
26 r'echo "Set \$GALAXY_VIRTUAL_ENV to $GALAXY_VIRTUAL_ENV"',
27 ('if [ -e "$GALAXY_VIRTUAL_ENV" ]; '
28 'then echo "Virtual environment directory exists."; '
29 'else echo "Virtual environment directory does not exist."; fi'),
30 )
31
32
33 CACHED_VIRTUAL_ENV_COMMAND = ("if [ -d .venv ]; "
34 "then GALAXY_VIRTUAL_ENV=.venv; "
35 "else GALAXY_VIRTUAL_ENV=%s; fi")
36 UNCACHED_VIRTUAL_ENV_COMMAND = "GALAXY_VIRTUAL_ENV=.venv"
37
38
39 def setup_venv(ctx, kwds):
40 if kwds.get("skip_venv", False):
41 return ""
42
43 create_template_params = {
44 'create_virtualenv': create_command("$GALAXY_VIRTUAL_ENV", kwds.get('galaxy_python_version'))
45 }
46 return shell_join(
47 locate_galaxy_virtualenv(ctx, kwds),
48 PRINT_VENV_COMMAND if ctx.verbose else None,
49 CREATE_COMMAND_TEMPLATE.safe_substitute(create_template_params),
50 PRINT_VENV_COMMAND if ctx.verbose else None,
51 ACTIVATE_COMMAND,
52 "bash -c 'pwd'" if ctx.verbose else None,
53 "bash -c 'which python'" if ctx.verbose else None,
54 "bash -c 'which pip'" if ctx.verbose else None,
55 "bash -c 'echo $GALAXY_VIRTUAL_ENV'" if ctx.verbose else None,
56 "bash -c 'echo $VIRTUAL_ENV'" if ctx.verbose else None,
57 "bash -c 'ls -a'" if ctx.verbose else None,
58 )
59
60
61 def locate_galaxy_virtualenv(ctx, kwds):
62 if os.environ.get("GALAXY_VIRTUAL_ENV"):
63 venv_command = ""
64 elif not kwds.get("no_cache_galaxy", False):
65 workspace = ctx.workspace
66 galaxy_branch = kwds.get("galaxy_branch") or "master"
67 shared_venv_path = os.path.join(workspace, "gx_venv")
68 galaxy_python_version = kwds.get('galaxy_python_version') or DEFAULT_PYTHON_VERSION
69 shared_venv_path = "%s_%s" % (shared_venv_path, galaxy_python_version)
70 if galaxy_branch != "master":
71 shared_venv_path = "%s_%s" % (shared_venv_path, galaxy_branch)
72 venv_command = CACHED_VIRTUAL_ENV_COMMAND % shlex_quote(shared_venv_path)
73 else:
74 venv_command = UNCACHED_VIRTUAL_ENV_COMMAND
75 return shell_join(
76 venv_command,
77 "export GALAXY_VIRTUAL_ENV",
78 )
79
80
81 def shell_if_wheels(command):
82 """ Take a shell command and convert it to shell command that runs
83 only if Galaxy is new enough to use wheels.
84 """
85 return "$(grep -q 'skip-venv' run_tests.sh) && %s" % command
86
87
88 def setup_common_startup_args():
89 return _set_variable_if_wheels(
90 "COMMON_STARTUP_ARGS", "--dev-wheels"
91 )
92
93
94 def _set_variable_if_wheels(var, if_wheels_val, else_val=""):
95 var_command = '${var}=${else_val}; '
96 var_command += shell_if_wheels(
97 '${var}="${if_wheels_val}"; '
98 )
99 var_command += "export ${var}"
100 var_command += '; echo "Set ${var} to ${${var}}"'
101 return string.Template(var_command).safe_substitute(
102 var=var,
103 if_wheels_val=if_wheels_val,
104 else_val=else_val,
105 )
106
107
108 def run_galaxy_command(ctx, command, env, action):
109 """Run Galaxy command with informative verbose logging."""
110 message = "%s with command [%s]" % (action, command)
111 # info not working in pytest+Github actions the way it did in nose?
112 info(message)
113 ctx.vlog("With environment variables:")
114 ctx.vlog("============================")
115 for key, value in env.items():
116 ctx.vlog('%s="%s"' % (key, value))
117 ctx.vlog("============================")
118 exit_code = shell(command, env=env)
119 ctx.vlog("run command exited with return code %s" % exit_code)
120 return exit_code
121
122
123 __all__ = (
124 "setup_venv",
125 "run_galaxy_command",
126 "setup_common_startup_args",
127 )