40
40
from .utils import (aslist , convert_pathsep_to_unix ,
41
41
docker_windows_path_adjust , json_dumps , onWindows ,
42
42
windows_default_container_id )
43
+ from .context import LoadingContext , RuntimeContext , getdefault
43
44
44
45
ACCEPTLIST_EN_STRICT_RE = re .compile (r"^[a-zA-Z0-9._+-]+$" )
45
46
ACCEPTLIST_EN_RELAXED_RE = re .compile (r".*" ) # Accept anything
@@ -80,25 +81,23 @@ def __init__(self,
80
81
self .tmpdir = tmpdir
81
82
self .script = script
82
83
83
- def run (self , ** kwargs ): # type: (**Any ) -> None
84
+ def run (self , runtimeContext ): # type: (RuntimeContext ) -> None
84
85
try :
85
86
ev = self .builder .do_eval (self .script )
86
87
normalizeFilesDirs (ev )
87
88
self .output_callback (ev , "success" )
88
89
except Exception as e :
89
90
_logger .warning (u"Failed to evaluate expression:\n %s" ,
90
- e , exc_info = kwargs . get ( ' debug' ) )
91
+ e , exc_info = runtimeContext . debug )
91
92
self .output_callback ({}, "permanentFail" )
92
93
93
94
def job (self ,
94
95
job_order , # type: Dict[Text, Text]
95
96
output_callbacks , # type: Callable[[Any, Any], Any]
96
- mutation_manager , # type: MutationManager
97
- basedir , # type: Text
98
- ** kwargs # type: Any
97
+ runtimeContext # type: RuntimeContext
99
98
):
100
99
# type: (...) -> Generator[ExpressionTool.ExpressionJob, None, None]
101
- builder = self ._init_job (job_order , mutation_manager , basedir , ** kwargs )
100
+ builder = self ._init_job (job_order , runtimeContext )
102
101
103
102
yield ExpressionTool .ExpressionJob (
104
103
builder , self .tool ["expression" ], output_callbacks ,
@@ -169,13 +168,13 @@ def __init__(self, job, output_callback, cachebuilder, jobcache):
169
168
self .cachebuilder = cachebuilder
170
169
self .outdir = jobcache
171
170
172
- def run (self , ** kwargs ):
173
- # type: (**Any ) -> None
171
+ def run (self , runtimeContext ):
172
+ # type: (RuntimeContext ) -> None
174
173
self .output_callback (self .job .collect_output_ports (
175
174
self .job .tool ["outputs" ],
176
175
self .cachebuilder ,
177
176
self .outdir ,
178
- kwargs . get ( " compute_checksum" , True )), "success" )
177
+ getdefault ( runtimeContext . compute_checksum , True )), "success" )
179
178
180
179
181
180
def check_adjust (builder , file_o ):
@@ -216,33 +215,29 @@ def make_path_mapper(reffiles, stagedir, basedir, separateDirs=True):
216
215
OutputPorts = Dict [Text , Union [None , Text , List [Union [Dict [Text , Any ], Text ]], Dict [Text , Any ]]]
217
216
218
217
class CommandLineTool (Process ):
219
- def __init__ (self , toolpath_object , eval_timeout , debug , js_console ,
220
- force_docker_pull , job_script_provider , ** kwargs ):
221
- # type: (Dict[Text, Any], float, bool, bool, bool, Optional[DependenciesConfiguration], **Any) -> None
218
+ def __init__ (self , toolpath_object , loadingContext ):
219
+ # type: (Dict[Text, Any], LoadingContext) -> None
222
220
super (CommandLineTool , self ).__init__ (
223
- toolpath_object , eval_timeout , debug , js_console ,
224
- force_docker_pull , job_script_provider , ** kwargs )
225
- self .find_default_container = kwargs .get ("find_default_container" , None )
221
+ toolpath_object , loadingContext )
226
222
227
223
def make_job_runner (self ,
228
- use_container = True , # type: Optional[bool]
229
- ** kwargs # type: Any
224
+ runtimeContext # type: RuntimeContext
230
225
): # type: (...) -> Type[JobBase]
231
226
dockerReq , _ = self .get_requirement ("DockerRequirement" )
232
- if not dockerReq and use_container :
233
- if self .find_default_container :
234
- default_container = self .find_default_container (self )
227
+ if not dockerReq and runtimeContext . use_container :
228
+ if runtimeContext .find_default_container :
229
+ default_container = runtimeContext .find_default_container (self )
235
230
if default_container :
236
231
self .requirements .insert (0 , {
237
232
"class" : "DockerRequirement" ,
238
233
"dockerPull" : default_container
239
234
})
240
235
dockerReq = self .requirements [0 ]
241
- if default_container == windows_default_container_id and use_container and onWindows ():
236
+ if default_container == windows_default_container_id and runtimeContext . use_container and onWindows ():
242
237
_logger .warning (DEFAULT_CONTAINER_MSG % (windows_default_container_id , windows_default_container_id ))
243
238
244
- if dockerReq and use_container :
245
- if kwargs . get ( ' singularity' ) :
239
+ if dockerReq and runtimeContext . use_container :
240
+ if runtimeContext . singularity :
246
241
return SingularityCommandLineJob
247
242
else :
248
243
return DockerCommandLineJob
@@ -268,9 +263,7 @@ def updatePathmap(self, outdir, pathmap, fn):
268
263
def job (self ,
269
264
job_order , # type: Dict[Text, Text]
270
265
output_callbacks , # type: Callable[[Any, Any], Any]
271
- mutation_manager , # type: MutationManager
272
- basedir , # type: Text
273
- ** kwargs # type: Any
266
+ runtimeContext # RuntimeContext
274
267
):
275
268
# type: (...) -> Generator[Union[JobBase, CallbackJob], None, None]
276
269
@@ -281,15 +274,15 @@ def job(self,
281
274
workReuse = self .get_requirement (require_prefix + "WorkReuse" )[0 ]
282
275
enableReuse = workReuse .get ("enableReuse" , True ) if workReuse else True
283
276
284
- jobname = uniquename (kwargs . get ( " name" , shortname (self .tool .get ("id" , "job" ) )))
285
- if kwargs . get ( " cachedir" ) and enableReuse :
286
- cacheargs = kwargs .copy ()
287
- cacheargs [ " outdir" ] = "/out"
288
- cacheargs [ " tmpdir" ] = "/tmp"
289
- cacheargs [ " stagedir" ] = "/stage"
290
- cachebuilder = self ._init_job (job_order , mutation_manager , basedir , ** cacheargs )
277
+ jobname = uniquename (runtimeContext . name or shortname (self .tool .get ("id" , "job" )))
278
+ if runtimeContext . cachedir and enableReuse :
279
+ cachecontext = runtimeContext .copy ()
280
+ cachecontext . outdir = "/out"
281
+ cachecontext . tmpdir = "/tmp"
282
+ cachecontext . stagedir = "/stage"
283
+ cachebuilder = self ._init_job (job_order , cachecontext )
291
284
cachebuilder .pathmapper = PathMapper (cachebuilder .files ,
292
- basedir ,
285
+ runtimeContext . basedir ,
293
286
cachebuilder .stagedir ,
294
287
separateDirs = False )
295
288
_check_adjust = partial (check_adjust , cachebuilder )
@@ -298,10 +291,10 @@ def job(self,
298
291
299
292
cmdline = flatten (list (map (cachebuilder .generate_arg , cachebuilder .bindings )))
300
293
(docker_req , docker_is_req ) = self .get_requirement ("DockerRequirement" )
301
- if docker_req and kwargs . get ( " use_container" ) :
294
+ if docker_req and runtimeContext . use_container :
302
295
dockerimg = docker_req .get ("dockerImageId" ) or docker_req .get ("dockerPull" )
303
- elif kwargs . get ( " default_container" , None ) is not None and kwargs . get ( " use_container" ) :
304
- dockerimg = kwargs . get ( " default_container" )
296
+ elif runtimeContext . default_container is not None and runtimeContext . use_container :
297
+ dockerimg = runtimeContext . default_container
305
298
else :
306
299
dockerimg = None
307
300
@@ -340,12 +333,12 @@ def job(self,
340
333
_logger .debug ("[job %s] keydictstr is %s -> %s" , jobname ,
341
334
keydictstr , cachekey )
342
335
343
- jobcache = os .path .join (kwargs [ " cachedir" ] , cachekey )
336
+ jobcache = os .path .join (runtimeContext . cachedir , cachekey )
344
337
jobcachepending = jobcache + ".pending"
345
338
346
339
if os .path .isdir (jobcache ) and not os .path .isfile (jobcachepending ):
347
- if docker_req and kwargs . get ( " use_container" ) :
348
- cachebuilder .outdir = kwargs . get ( " docker_outdir" ) or "/var/spool/cwl"
340
+ if docker_req and runtimeContext . use_container :
341
+ cachebuilder .outdir = runtimeContext . docker_outdir or "/var/spool/cwl"
349
342
else :
350
343
cachebuilder .outdir = jobcache
351
344
@@ -356,7 +349,8 @@ def job(self,
356
349
_logger .info ("[job %s] Output of job will be cached in %s" , jobname , jobcache )
357
350
shutil .rmtree (jobcache , True )
358
351
os .makedirs (jobcache )
359
- kwargs ["outdir" ] = jobcache
352
+ runtimeContext = runtimeContext .copy ()
353
+ runtimeContext .outdir = jobcache
360
354
open (jobcachepending , "w" ).close ()
361
355
362
356
def rm_pending_output_callback (output_callbacks , jobcachepending ,
@@ -367,18 +361,12 @@ def rm_pending_output_callback(output_callbacks, jobcachepending,
367
361
368
362
output_callbacks = partial (
369
363
rm_pending_output_callback , output_callbacks , jobcachepending )
370
- # output_callbacks = cast(
371
- # Callable[..., Any], # known bug in mypy
372
- # # https://github.com/python/mypy/issues/797
373
- # partial(rm_pending_output_callback, output_callbacks,
374
- # jobcachepending))
375
364
376
-
377
- builder = self ._init_job (job_order , mutation_manager , basedir , ** kwargs )
365
+ builder = self ._init_job (job_order , runtimeContext )
378
366
379
367
reffiles = copy .deepcopy (builder .files )
380
368
381
- j = self .make_job_runner (** kwargs )(
369
+ j = self .make_job_runner (runtimeContext )(
382
370
builder , builder .job , make_path_mapper , self .requirements ,
383
371
self .hints , jobname )
384
372
j .successCodes = self .tool .get ("successCodes" )
@@ -391,13 +379,13 @@ def rm_pending_output_callback(output_callbacks, jobcachepending,
391
379
_logger .debug (u"[job %s] initializing from %s%s" ,
392
380
j .name ,
393
381
self .tool .get ("id" , "" ),
394
- u" as part of %s" % kwargs [ " part_of" ]
395
- if " part_of" in kwargs else "" )
382
+ u" as part of %s" % runtimeContext . part_of
383
+ if runtimeContext . part_of else "" )
396
384
_logger .debug (u"[job %s] %s" , j .name , json_dumps (job_order ,
397
385
indent = 4 ))
398
386
399
387
builder .pathmapper = make_path_mapper (
400
- reffiles , builder .stagedir , basedir , kwargs . get ( "separateDirs" , True ) )
388
+ reffiles , builder .stagedir , runtimeContext . basedir , True )
401
389
builder .requirements = j .requirements
402
390
403
391
_check_adjust = partial (check_adjust , builder )
@@ -475,12 +463,12 @@ def rm_pending_output_callback(output_callbacks, jobcachepending,
475
463
json_dumps (builder .bindings , indent = 4 ))
476
464
477
465
dockerReq = self .get_requirement ("DockerRequirement" )[0 ]
478
- if dockerReq and kwargs . get ( " use_container" ) :
479
- out_prefix = kwargs . get ( " tmp_outdir_prefix" , 'tmp' )
480
- j .outdir = kwargs . get ( " outdir" ) or \
466
+ if dockerReq and runtimeContext . use_container :
467
+ out_prefix = getdefault ( runtimeContext . tmp_outdir_prefix , 'tmp' )
468
+ j .outdir = runtimeContext . outdir or \
481
469
tempfile .mkdtemp (prefix = out_prefix ) # type: ignore
482
- tmpdir_prefix = kwargs . get ( ' tmpdir_prefix' , 'tmp' )
483
- j .tmpdir = kwargs . get ( " tmpdir" ) or \
470
+ tmpdir_prefix = getdefault ( runtimeContext . tmpdir_prefix , 'tmp' )
471
+ j .tmpdir = runtimeContext . tmpdir or \
484
472
tempfile .mkdtemp (prefix = tmpdir_prefix ) # type: ignore
485
473
j .stagedir = tempfile .mkdtemp (prefix = tmpdir_prefix )
486
474
else :
@@ -494,8 +482,8 @@ def rm_pending_output_callback(output_callbacks, jobcachepending,
494
482
j .inplace_update = inplaceUpdateReq ["inplaceUpdate" ]
495
483
normalizeFilesDirs (j .generatefiles )
496
484
497
- readers = {}
498
- muts = set ()
485
+ readers = {} # type: Dict[Text, Any]
486
+ muts = set () # type: Set[Text]
499
487
500
488
if builder .mutation_manager :
501
489
def register_mut (f ):
@@ -558,7 +546,7 @@ def register_reader(f):
558
546
j .pathmapper = builder .pathmapper
559
547
j .collect_outputs = partial (
560
548
self .collect_output_ports , self .tool ["outputs" ], builder ,
561
- compute_checksum = kwargs . get ( " compute_checksum" , True ),
549
+ compute_checksum = getdefault ( runtimeContext . compute_checksum , True ),
562
550
jobname = jobname ,
563
551
readers = readers )
564
552
j .output_callback = output_callbacks
0 commit comments