uwtools.api.utils
API access to utility functions.
- uwtools.api.utils.atomic(path)
Yields a path to a temporary file, finally renaming that file to
path.- Parameters:
path (Path) – The final path.
- Yields:
The path to a temporary file.
- Yieldtype:
Path.
- Return type:
Iterator[Path]
- uwtools.api.utils.run_shell_cmd(cmd, callback=None, cwd=None, env=None, executable=None, log_output=False, quiet=False, start_new_session=False, taskname=None)
Run a command in a shell.
- Parameters:
callback (Callable[[Popen], None] | None) – Called with the
Popenprocess object.cwd (Path | str | None) – Change to this directory before running the command.
env (dict[str, str] | None) – Environment variables to set before running the command.
executable (str | None) – Interpreter to run command in (e.g. “/bin/bash”).
log_output (bool) – Log output from successful command? (Error output is always logged.)
quiet (bool) – Log
INFOmessages asDEBUG.start_new_session (bool) – Run process in a new session?
taskname (str | None) – Name of task executing this command, for logging.
- Returns:
Success indication and combined
stdout/stderr.- Return type:
The
Callablespecified by thecallbackargument will be called once with thePopenobject as soon as it is available; i.e. it may be called while the command is running, and does not wait for the command to complete.Use
executableto use a shell other than thesubprocess.Popendefault,/bin/sh.It is assumed that
uwtools.api.loggin.use_uwtools_loggerhas previously been called to set up logging iflog_outputisTrue.The
start_new_sessionargument is connected directly to the argument of the same name whensubprocess.Popenis called. Among other potential use cases, it prevents the command from receiving aSIGINTgenerated by a user Ctrl-C, useful when the command is running in a thread and the main control thread should handle the interrupt.If
tasknameis provided, log messages will be prefixed with[<taskname>].