11"""Module contianing the :class:`GalaxyEngine` implementation of :class:`Engine`."""
2+ from __future__ import absolute_import
23
4+ import abc
35import contextlib
46
7+ from galaxy .tools .verify import interactor
8+
59from planemo .galaxy .activity import execute
10+ from planemo .galaxy .config import external_galaxy_config
611from planemo .galaxy .serve import serve_daemon
712from planemo .runnable import RunnableType
813from .interface import BaseEngine
914
1015
1116class GalaxyEngine (BaseEngine ):
12- """An :class:`Engine` implementation backed by Galaxy.
17+ """An :class:`Engine` implementation backed by a managed Galaxy.
1318
1419 More information on Galaxy can be found at http://galaxyproject.org/.
1520 """
1621
22+ __metaclass__ = abc .ABCMeta
23+
1724 handled_runnable_types = [
1825 RunnableType .cwl_tool ,
1926 RunnableType .cwl_workflow ,
@@ -24,14 +31,71 @@ class GalaxyEngine(BaseEngine):
2431 def _run (self , runnable , job_path ):
2532 """Run CWL job in Galaxy."""
2633 self ._ctx .vlog ("Serving artifact [%s] with Galaxy." % (runnable ,))
27- with self .serve_runnables ([runnable ]) as config :
34+ with self .ensure_runnables_served ([runnable ]) as config :
2835 self ._ctx .vlog ("Running job path [%s]" % job_path )
2936 run_response = execute (self ._ctx , config , runnable , job_path , ** self ._kwds )
3037
3138 return run_response
3239
40+ @abc .abstractmethod
41+ def ensure_runnables_served (self , runnables ):
42+ """Use a context manager and describe Galaxy instance with runnables being served."""
43+
44+ def _run_test_case (self , test_case ):
45+ if hasattr (test_case , "job_path" ):
46+ # Simple file-based job path.
47+ super (GalaxyEngine , self )._run_test_case (test_case )
48+ else :
49+ with self .ensure_runnables_served ([test_case .runnable ]) as config :
50+ galaxy_interactor_kwds = {
51+ "galaxy_url" : config .galaxy_url ,
52+ "master_api_key" : config .master_api_key ,
53+ "api_key" : config .user_api_key ,
54+ "keep_outputs_dir" : "" , # TODO: this...
55+ }
56+ tool_id = test_case .tool_id
57+ test_index = test_case .test_index
58+ tool_version = test_case .tool_version
59+ galaxy_interactor = interactor .GalaxyInteractorApi (** galaxy_interactor_kwds )
60+
61+ test_results = []
62+
63+ def _register_job_data (job_data ):
64+ test_results .append ({
65+ 'id' : tool_id + "-" + str (test_index ),
66+ 'has_data' : True ,
67+ 'data' : job_data ,
68+ })
69+
70+ verbose = self ._ctx .verbose
71+ try :
72+ if verbose :
73+ # TODO: this is pretty hacky, it'd be better to send a stream
74+ # and capture the output information somehow.
75+ interactor .VERBOSE_GALAXY_ERRORS = True
76+
77+ interactor .verify_tool (
78+ tool_id ,
79+ galaxy_interactor ,
80+ test_index = test_index ,
81+ tool_version = tool_version ,
82+ register_job_data = _register_job_data ,
83+ quiet = not verbose ,
84+ )
85+ except Exception :
86+ pass
87+
88+ return test_results [0 ]
89+
90+
91+ class LocalManagedGalaxyEngine (BaseEngine ):
92+ """An :class:`Engine` implementation backed by a managed Galaxy.
93+
94+ More information on Galaxy can be found at http://galaxyproject.org/.
95+ """
96+
3397 @contextlib .contextmanager
34- def serve_runnables (self , runnables ):
98+ def ensure_runnables_served (self , runnables ):
3599 # TODO: define an interface for this - not everything in config would make sense for a
36100 # pre-existing Galaxy interface.
37101 with serve_daemon (self ._ctx , runnables , ** self ._serve_kwds ()) as config :
@@ -41,7 +105,7 @@ def _serve_kwds(self):
41105 return self ._kwds .copy ()
42106
43107
44- class DockerizedGalaxyEngine ( GalaxyEngine ):
108+ class DockerizedManagedGalaxyEngine ( LocalManagedGalaxyEngine ):
45109 """An :class:`Engine` implementation backed by Galaxy running in Docker.
46110
47111 More information on Galaxy can be found at http://galaxyproject.org/.
@@ -53,7 +117,20 @@ def _serve_kwds(self):
53117 return serve_kwds
54118
55119
120+ class ExternalGalaxyEngine (GalaxyEngine ):
121+ """An :class:`Engine` implementation backed by an external Galaxy instance.
122+ """
123+
124+ @contextlib .contextmanager
125+ def ensure_runnables_served (self , runnables ):
126+ # TODO: ensure tools are available
127+ with external_galaxy_config (self ._ctx , runnables , ** self ._kwds ) as config :
128+ config .install_workflows ()
129+ yield config
130+
131+
56132__all__ = (
57- "GalaxyEngine" ,
58- "DockerizedGalaxyEngine" ,
133+ "DockerizedManagedGalaxyEngine" ,
134+ "ExternalGalaxyEngine" ,
135+ "LocalManagedGalaxyEngine" ,
59136)
0 commit comments