99import shutil
1010import platform
1111import traceback
12- import yaml
1312from typing import Dict , List , Optional
1413from datetime import datetime
15- import sophios .post_compile as pc
16- from sophios .wic_types import Json
1714
1815try :
1916 import cwltool .main
@@ -304,7 +301,7 @@ def copy_output_files(yaml_stem: str, basepath: str = '') -> None:
304301 Args:
305302 yaml_stem (str): The --yaml filename (without .extension)
306303 """
307- output_json_file = Path (f'output_{ yaml_stem } .json' )
304+ output_json_file = Path (f'{ basepath } / output_{ yaml_stem } .json' )
308305 if output_json_file .exists ():
309306 pass # TODO
310307
@@ -386,7 +383,7 @@ def build_cmd(workflow_name: str, basepath: str, cwl_runner: str, container_cmd:
386383 elif cwl_runner == 'toil-cwl-runner' :
387384 container_pull = []
388385 now = datetime .now ()
389- date_time = now .strftime ("%Y%m%d%H%M %S" )
386+ date_time = now .strftime ("%Y_%m_%d_%H.%M. %S" )
390387 cmd = [script ] + container_pull + provenance + container_cmd_ + path_check
391388 cmd += ['--outdir' , f'{ basepath } /outdir_toil_{ date_time } ' ,
392389 '--jobStore' , f'file:{ basepath } /jobStore_{ workflow_name } ' , # NOTE: This is the equivalent of --cachedir
@@ -401,90 +398,6 @@ def build_cmd(workflow_name: str, basepath: str, cwl_runner: str, container_cmd:
401398 return cmd
402399
403400
404- def run_cwl_workflow (workflow_name : str , basepath : str , cwl_runner : str , container_cmd : str , use_subprocess : bool , env_commands : List [str ] = []) -> int :
405- """Run the CWL workflow in an environment
406-
407- Args:
408- workflow_name (str): Name of the .cwl workflow file to be executed
409- basepath (str): The path at which the workflow to be executed
410- cwl_runner (str): The CWL runner used to execute the workflow
411- container_cmd (str): The container engine command
412- use_subprocess (bool): When using cwltool, determines whether to use subprocess.run(...)
413- or use the cwltool python api.
414- env_commands (List[str]): environment variables and commands needed to be run before running the workflow
415- Returns:
416- retval: The return value
417- """
418- cmd = build_cmd (workflow_name , basepath , cwl_runner , container_cmd )
419- cmdline = ' ' .join (cmd )
420-
421- retval = 1 # overwrite on success
422- print ('Running ' + cmdline )
423- if use_subprocess :
424- # To run in parallel (i.e. pytest ... --workers 8 ...), we need to
425- # use separate processes. Otherwise:
426- # "signal only works in main thread or with __pypy__.thread.enable_signals()"
427- proc = sub .run (cmd , check = False )
428- retval = proc .returncode
429- else :
430- print ('via cwltool.main.main python API' )
431- try :
432- if cwl_runner == 'cwltool' :
433- retval = cwltool .main .main (cmd [1 :])
434- elif cwl_runner == 'toil-cwl-runner' :
435- _ = sub .run (env_commands , shell = True , check = False , executable = "/bin/bash" )
436- retval = toil .cwl .cwltoil .main (cmd [1 :])
437- else :
438- raise Exception ("Invalid cwl_runner!" )
439-
440- print (f'Final output json metadata blob is in output_{ workflow_name } .json' )
441- except Exception as e :
442- print ('Failed to execute' , workflow_name )
443- print (f'See error_{ workflow_name } .txt for detailed technical information.' )
444- # Do not display a nasty stack trace to the user; hide it in a file.
445- with open (f'error_{ workflow_name } .txt' , mode = 'w' , encoding = 'utf-8' ) as f :
446- # https://mypy.readthedocs.io/en/stable/common_issues.html#python-version-and-system-platform-checks
447- if sys .version_info >= (3 , 10 ):
448- traceback .print_exception (type (e ), value = e , tb = None , file = f )
449- print (e ) # we are always running this on CI
450- # only copy output files if using cwltool
451- if cwl_runner == 'cwltool' :
452- copy_output_files (workflow_name , basepath = basepath )
453- return retval
454-
455-
456- async def run_cwl_serialized_async (workflow : Json , basepath : str ,
457- cwl_runner : str , container_cmd : str ,
458- env_commands : List [str ] = []) -> None :
459- """Prepare and run compiled and serialized CWL workflow asynchronously
460-
461- Args:
462- workflow_json (Json): Compiled and serialized CWL workflow
463- basepath (str): The path at which the workflow to be executed
464- cwl_runner (str): The CWL runner used to execute the workflow
465- container_cmd (str): The container engine command
466- env_commands (List[str]): environment variables and commands needed to be run before running the workflow
467- """
468- workflow_name = workflow ['name' ]
469- basepath = basepath .rstrip ("/" ) if basepath != "/" else basepath
470- output_dirs = pc .find_output_dirs (workflow )
471- pc .create_output_dirs (output_dirs , basepath )
472- compiled_cwl = workflow_name + '.cwl'
473- inputs_yml = workflow_name + '_inputs.yml'
474- # write _input.yml file
475- with open (Path (basepath ) / inputs_yml , 'w' , encoding = 'utf-8' ) as f :
476- yaml .dump (workflow ['yaml_inputs' ], f )
477- workflow .pop ('retval' , None )
478- workflow .pop ('yaml_inputs' , None )
479- workflow .pop ('name' , None )
480- # write compiled .cwl file
481- with open (Path (basepath ) / compiled_cwl , 'w' , encoding = 'utf-8' ) as f :
482- yaml .dump (workflow , f )
483- retval = run_cwl_workflow (workflow_name , basepath ,
484- cwl_runner , container_cmd , False , env_commands = env_commands )
485- assert retval == 0
486-
487-
488401def stage_input_files (yml_inputs : Yaml , root_yml_dir_abs : Path ,
489402 relative_run_path : bool = True , throw : bool = True ) -> None :
490403 """Copies the input files in yml_inputs to the working directory.
0 commit comments