datalad.cmd

Class the starts a subprocess and keeps it around to communicate with it via stdin. For each instruction send over stdin, a response is read and returned. The response structure is determined by “output_proc”

class datalad.cmd.BatchedCommand(cmd: Union[str, Tuple, List], path: Optional[str] = None, output_proc: Optional[Callable] = None, timeout: Optional[float] = None, exception_on_timeout: bool = False)[source]

Bases: SafeDelCloseMixin

Container for a running subprocess. Supports communication with the subprocess via stdin and stdout.

classmethod clean_inactive()[source]
close(return_stderr=False)[source]

Close communication and wait for process to terminate. If the “timeout” parameter to the constructor was not None, and if the configuration setting “datalad.runtime.stalled-external” is set to “abandon”, the method will return latest after “timeout” seconds. If the subprocess did not exit within this time, the attribute “wait_timed_out” will be set to “True”.

Parameters

return_stderr (bool) – if set to “True”, the call will return all collected stderr content as string. In addition, if return_stderr is True and the log level is 5 or lower, and the configuration setting “datalad.log.outputs” evaluates to “True”, the content of stderr will be logged.

Returns

stderr output if return_stderr is True, None otherwise

Return type

str, optional

get_one_line() Optional[str][source]

Get a single stdout line from the generator.

If timeout was specified, and exception_on_timeout is False, and if a timeout occurs, return None. Otherwise, return the string that was read from the generator.

get_requested_error_output(return_stderr: bool)[source]
get_timeout_exception(fd: Optional[int]) Optional[TimeoutExpired][source]

Get a process timeout exception if timeout exceptions should be generated for a process that continues longer than timeout seconds after self.close() was initiated.

proc1(single_command: str)[source]

Simulate the old interface. This method is used only once in AnnexRepo.get_metadata()

process_request(request: Union[Tuple, str]) str[source]
process_running() bool[source]
class datalad.cmd.BatchedCommandProtocol(batched_command: BatchedCommand, done_future: Optional[Any] = None, encoding: Optional[str] = None, output_proc: Optional[Callable] = None)[source]

Bases: GeneratorMixIn, StdOutErrCapture

pipe_connection_lost(fd: int, exc: Optional[Exception])[source]

Called when a file descriptor associated with the child process is closed.

fd is the int file descriptor that was closed.

pipe_data_received(fd: int, data: bytes)[source]
timeout(fd: Optional[int]) bool[source]

Called if the timeout parameter to WitlessRunner.run() is not None and a process file descriptor could not be read (stdout or stderr) or not be written (stdin) within the specified time in seconds, or if waiting for a subprocess to exit takes longer than the specified time.

stdin timeouts are only caught when the type of the stdin- parameter to WitlessRunner.run() is either a Queue, a str, or bytes. Stdout or stderr timeouts are only caught of proc_out and proc_err are True in the protocol class. Process wait timeouts are always caught if timeout is not None. In this case the fd-argument will be None.

fd:

The file descriptor that timed out or None if no progress was made at all, i.e. no stdin element was enqueued and no output was read from either stdout or stderr.

Returns

If the callback returns True, the file descriptor (if any was given) will be closed and no longer monitored. If the return values is anything else than True, the file-descriptor will be monitored further and additional timeouts might occur indefinitely. If None was given, i.e. a process runtime-timeout was detected, and True is returned, the process will be terminated.

class datalad.cmd.ReadlineEmulator(batched_command: BatchedCommand)[source]

Bases: object

This class implements readline() on the basis of an instance of BatchedCommand. Its purpose is to emulate stdout’s for output_procs, This allows us to provide a BatchedCommand API that is identical to the old version, but with an implementation that is based on the threaded runner.

readline()[source]

Read from the stdout provider until we have a line or None (which indicates some error).

class datalad.cmd.SafeDelCloseMixin[source]

Bases: object

A helper class to use where __del__ would call .close() which might fail if “too late in GC game”

datalad.cmd.readline_rstripped(stdout)[source]