cotainr.util module#

cotainr - a user space Apptainer/Singularity container builder.

Copyright DeiC, deic.dk Licensed under the European Union Public License (EUPL) 1.2 - see the LICENSE file for details.

This module implements utility functions.

Functions#

answer_is_yes()

Ask user for confirmation (“yes”) of input_text.

get_systems()

Get a dictionary of predefined systems, defined in systems.json

stream_subprocess(*, args, **kwargs)

Run a the command described by args while streaming stdout and stderr.

Attributes#

systems_file

The path to the systems.json file (if present).

API reference#

cotainr.util.answer_is_yes(input_text, max_attempts=1000)#

Ask user for confirmation (“yes”) of input_text.

Parameters:
input_textstr

The prompt to be printed for verification by the user.

max_attemptsint, optional

The maximum number of attempts to get a valid answer from the user before giving up. The default is 1000.

Returns:
answer_is_yesbool

The indicator of whether or not the answer is yes.

cotainr.util.get_systems()#

Get a dictionary of predefined systems, defined in the systems.json file.

Returns:
systemsDict

A dictionary of predefined systems

Raises:
NameError

If some required arguments are missing in the systems.json file.

cotainr.util.stream_subprocess(*, args, log_dispatcher=None, **kwargs)#

Run a the command described by args while streaming stdout and stderr.

The command described by args is run in a subprocess with that subprocess’ stdout and stderr streamed to the main process. Each line in the subprocess’ output is streamed separately to the main process. Extra kwargs are passed to Popen when opening the subprocess.

Parameters:
argslist or str

Program arguments. See the docstring for subprocess.Popen for details.

log_dispatcherLogDispatcher, optional

The log dispatcher which subprocess stdout/stderr messages are forwarded to (the default is None which implies that subprocess messages are forwarded directly to stdout/stderr).

Returns:
completed_processsubprocess.CompletedProcess

Information about the completed subprocess.

Raises:
subprocess.CalledProcessError

If the subprocess returned a non-zero status code.

Notes

The way we handle stdout and stderr messages in separate threads introduces a race condition in cases where subprocesses write to stdout and stderr at the same time. All messages are always guaranteed to be streamed to the console, but they might arrive in the wrong order. We accept this “feature” as is.