# Copyright (c) 2024 Justin Davis (davisjustin302@gmail.com)## MIT License# ruff: noqa: S404, S603from__future__importannotationsimportsubprocessfrompathlibimportPathfromtrtutils._logimportLOGfrom._findimportfind_trtexec
[docs]defrun_trtexec(command:str,trtexec_path:Path|str|None=None,)->tuple[bool,str,str]:""" Run a command using trtexec. The goal of this function is make it easier to use trtexec within Python scripts. By returning the stdout/stderr streams via strings back to the Python program it can simplify logic or scripts which utilize trtexec. Parameters ---------- command : str The command to run using trtexec trtexec_path : Path | str | None, optional The path to the trtexec binary to use. If None, find_trtexec will be used. Returns ------- tuple[bool, str, str] A tuple containing the following elements: (success, stdout, stderr) """iftrtexec_pathisNone:trtexec_path=find_trtexec()ifisinstance(trtexec_path,Path):trtexec_path=str(trtexec_path)command=f"{trtexec_path}{command}"com_list=[pforpincommand.split(" ")iflen(p)>0]try:process=subprocess.run(com_list,capture_output=True,check=True,)exceptsubprocess.CalledProcessErrorase:err_msg=f"Error running trtexec command: {command}"err_msg+=f"\n\tReturn value: {e.returncode}"LOG.error(err_msg)returnFalse,e.stdout.decode(),e.stderr.decode()stdout=""ifprocess.stdoutisnotNone:stdout=process.stdout.decode()stderr=""ifprocess.stderrisnotNone:stderr=process.stderr.decode()returnTrue,stdout,stderr