"""Handles interaction with bash command.Copyright 2021, Bill & Melinda Gates Foundation. All rights reserved."""importsubprocessfromloggingimportDEBUG,getLoggerfrompathlibimportPathfromtypingimportDictERROR_NO_WORKING_DIRECTORY="No working directory provided for command"logger=getLogger()
[docs]defcommand_bash(info:Dict)->Dict:""" Process command request for sbatch commands to be executed. Args: info: Info command Returns: Result dict """if'working_directory'ininfo:wd=Path(info['working_directory'])ifnotwd.exists():output="FAILED: No Directory name %s"%info['working_directory']return_code=-1else:output,return_code=run_bash(wd)result=dict(status="success"ifreturn_code==0else"error",return_code=return_code,output=output)else:result=dict(status="error",return_code=-1,output=ERROR_NO_WORKING_DIRECTORY)returnresult
[docs]defrun_bash(working_directory:Path):""" Just a bash script. Args: working_directory: Working directory """sbp=working_directory.joinpath("batch.sh")ifnotsbp.exists():returnf"FAILED: No Directory name {sbp}"iflogger.isEnabledFor(DEBUG):logger.debug(f"Running 'bash batch.sh' in {working_directory}")result=subprocess.run(['bash','batch.sh'],stdout=subprocess.PIPE,cwd=str(working_directory))return'',result.returncode