@@ -200,52 +200,46 @@ def map_path(fileset: os.PathLike | FileSet) -> Path:
200200 return bindings , values
201201
202202
203- def execute (cmd , strip = False ):
204- """
205- Run the event loop with coroutine.
206-
207- Uses :func:`read_and_display_async` unless a loop is
208- already running, in which case :func:`read_and_display`
209- is used.
203+ def read_and_display (
204+ * cmd : str , strip : bool = False , hide_display : bool = False
205+ ) -> dict [str , int | str ]:
206+ """Capture a process' standard output.
210207
211208 Parameters
212209 ----------
213- cmd : :obj:`list` or :obj:`tuple`
214- The command line to be executed.
215- strip : :obj:`bool`
216- TODO
217-
218- """
219- rc , stdout , stderr = read_and_display (* cmd , strip = strip )
220- """
221- loop = get_open_loop()
222- if loop.is_running():
223- rc, stdout, stderr = read_and_display(*cmd, strip=strip)
224- else:
225- rc, stdout, stderr = loop.run_until_complete(
226- read_and_display_async(*cmd, strip=strip)
227- )
210+ cmd : str
211+ The command to execute, as a list of strings.
212+ strip : bool, optional
213+ If True, the output will be stripped of leading and trailing whitespace.
214+ hide_display : bool, optional
215+ If True, the output will not be displayed.
216+
217+ Returns
218+ -------
219+ dict[str, Any]
220+ A dictionary containing the return code, standard output, and standard error.
221+
222+ Raises
223+ ------
224+ RuntimeError
225+ If the return code is not 0, a RuntimeError is raised with a formatted
226+ error message.
228227 """
229- return rc , stdout , stderr
230-
231-
232- def read_and_display (* cmd , strip = False , hide_display = False ):
233- """Capture a process' standard output."""
234228 try :
235229 process = sp .run (cmd , stdout = sp .PIPE , stderr = sp .PIPE )
236230 except Exception :
237231 # TODO editing some tracing?
238232 raise
239233
234+ stdout = process .stdout .decode ("utf-8" )
240235 if strip :
241- return (
242- process .returncode ,
243- process .stdout .decode ("utf-8" ).strip (),
244- process .stderr .decode ("utf-8" ),
245- )
246- else :
247- return (
248- process .returncode ,
249- process .stdout .decode ("utf-8" ),
250- process .stderr .decode ("utf-8" ),
251- )
236+ stdout = stdout .strip ()
237+ stderr = process .stderr .decode ("utf-8" )
238+ if process .returncode :
239+ msg = f"Error executing command { cmd } (code: { process .returncode } ):"
240+ if stdout :
241+ msg += "\n \n stderr:\n " + stderr
242+ if stdout :
243+ msg += "\n \n stdout:\n " + stdout
244+ raise RuntimeError (msg )
245+ return {"return_code" : process .returncode , "stdout" : stdout , "stderr" : stderr }
0 commit comments