11from __future__ import absolute_import
22
3- import os
4- from typing import Callable as tCallable # pylint: disable=unused-import
5- from typing import Any , Dict , Tuple , Union
6-
7- from typing_extensions import Text # pylint: disable=unused-import
83# move to a regular typing import when Python 3.3-3.6 is no longer supported
4+ import functools
5+ import os
6+ import sys
97
108from . import load_tool
11- from .context import LoadingContext , RuntimeContext
9+ from .argparser import arg_parser
10+ from .context import LoadingContext , RuntimeContext , getdefault
1211from .executors import SingleJobExecutor
13- from .process import Process
12+ from .main import find_default_container
13+ from .resolver import tool_resolver
14+ from .secrets import SecretStore
15+ from .utils import DEFAULT_TMP_PREFIX
1416
1517
1618class WorkflowStatus (Exception ):
@@ -28,34 +30,75 @@ def __init__(self, t, factory): # type: (Process, Factory) -> None
2830
2931 def __call__ (self , ** kwargs ):
3032 # type: (**Any) -> Union[Text, Dict[Text, Text]]
31- runtime_context = self .factory .runtime_context .copy ()
32- runtime_context .basedir = os .getcwd ()
33- out , status = self .factory .executor (self .t , kwargs , runtime_context )
33+ out , status = self .factory .executor (
34+ self .t , kwargs , self .factory .runtimeContext )
3435 if status != "success" :
3536 raise WorkflowStatus (out , status )
3637 else :
3738 return out
3839
40+
3941class Factory (object ):
4042 def __init__ (self ,
41- executor = None , # type: tCallable[...,Tuple[Dict[Text,Any], Text]]
42- loading_context = None , # type: LoadingContext
43- runtime_context = None # type: RuntimeContext
44- ): # type: (...) -> None
43+ argsl = None , # type: List[str]
44+ args = None , # type: argparse.Namespace
45+ # type: Callable[..., Tuple[Dict[Text, Any], Text]]
46+ executor = None ,
47+ loadingContext = None , # type: LoadingContext
48+ runtimeContext = None # type: RuntimeContext
49+ ): # type: (...) -> None
50+ if argsl is not None :
51+ args = arg_parser ().parse_args (argsl )
4552 if executor is None :
46- executor = SingleJobExecutor ()
47- self .executor = executor
48- self .loading_context = loading_context
49- if loading_context is None :
50- self .loading_context = LoadingContext ()
51- if runtime_context is None :
52- self .runtime_context = RuntimeContext ()
53+ self .executor = SingleJobExecutor ()
54+ else :
55+ self .executor = executor
56+ if loadingContext is None :
57+ self .loadingContext = LoadingContext (vars (args ))
58+ self ._fix_loadingContext ()
59+ else :
60+ self .loadingContext = loadingContext
61+ if runtimeContext is None :
62+ self .runtimeContext = RuntimeContext (vars (args ))
63+ self ._fix_runtimeContext ()
5364 else :
54- self .runtime_context = runtime_context
65+ self .runtimeContext = runtimeContext
5566
5667 def make (self , cwl ):
5768 """Instantiate a CWL object from a CWl document."""
58- load = load_tool .load_tool (cwl , self .loading_context )
69+ load = load_tool .load_tool (cwl , self .loadingContext )
5970 if isinstance (load , int ):
6071 raise Exception ("Error loading tool" )
6172 return Callable (load , self )
73+
74+ def _fix_loadingContext (self ):
75+ self .loadingContext .resolver = getdefault (
76+ self .loadingContext .resolver , tool_resolver )
77+
78+ def _fix_runtimeContext (self ):
79+ self .runtimeContext .basedir = os .getcwd ()
80+ self .runtimeContext .find_default_container = functools .partial (
81+ find_default_container ,
82+ default_container = None ,
83+ use_biocontainers = None )
84+
85+ if sys .platform == "darwin" :
86+ default_mac_path = "/private/tmp/docker_tmp"
87+ if self .runtimeContext .tmp_outdir_prefix == DEFAULT_TMP_PREFIX :
88+ self .runtimeContext .tmp_outdir_prefix = default_mac_path
89+
90+ for dirprefix in ("tmpdir_prefix" , "tmp_outdir_prefix" , "cachedir" ):
91+ if getattr (self .runtimeContext , dirprefix ) and getattr (self .runtimeContext , dirprefix ) != DEFAULT_TMP_PREFIX :
92+ sl = "/" if getattr (self .runtimeContext , dirprefix ).endswith ("/" ) or dirprefix == "cachedir" \
93+ else ""
94+ setattr (self .runtimeContext , dirprefix ,
95+ os .path .abspath (getattr (self .runtimeContext , dirprefix )) + sl )
96+ if not os .path .exists (os .path .dirname (getattr (self .runtimeContext , dirprefix ))):
97+ try :
98+ os .makedirs (os .path .dirname (
99+ getattr (self .runtimeContext , dirprefix )))
100+ except Exception as e :
101+ print ("Failed to create directory: %s" , e )
102+
103+ self .runtimeContext .secret_store = getdefault (
104+ self .runtimeContext .secret_store , SecretStore ())
0 commit comments