Command helpers - libvcs._internal.run¶
Misc. legacy helpers subprocess and finding VCS binaries.
libvcs._internal.run.run will be deprecated by
libvcs._internal.subprocess.
Note
This is an internal API not covered by versioning policy.
-
libvcs._internal.run.console_to_str(s)¶
From pypa/pip project, pip.backwardwardcompat. License MIT.
-
class libvcs._internal.run.CmdLoggingAdapter¶
Bases:
LoggerAdapterAdapter for additional command-related data to
logging.Extends
logging.LoggerAdapter’s functionality.Mixes in additional context via
logging.LoggerAdapter.process()forlogging.Formatterwhen emitting log entries.
-
class libvcs._internal.run.ProgressCallbackProtocol¶
Bases:
ProtocolCallback to report subprocess communication.
-
libvcs._internal.run._normalize_command_args(args)¶
Return subprocess arguments without splitting scalar strings or bytes.
- Parameters:
args (_CMD)
- Return type:
-
libvcs._internal.run._stringify_command(args)¶
Return a human-readable command for CommandError.
-
libvcs._internal.run.run(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, encoding=None, errors=None, user=None, group=None, extra_groups=None, umask=-1, log_in_real_time=False, check_returncode=True, callback=None, timeout=None)¶
Run a command.
Run ‘args’ in a shell and return the combined contents of stdout and stderr (Blocking). Throws an exception if the command exits non-zero.
Keyword arguments are passthrough to
subprocess.Popen.- Parameters:
args (list or str, or single str, if shell=True) – the command to run
shell (bool) – boolean indicating whether we are using advanced shell features. Use only when absolutely necessary, since this allows a lot more freedom which could be exploited by malicious code. See the warning here: http://docs.python.org/library/subprocess.html#popen-constructor
cwd (str) – dir command is run from. Defaults to
path.log_in_real_time (bool) – boolean indicating whether to read stdout from the subprocess in real time instead of when the process finishes.
check_returncode (bool) – Indicate whether a
libvcs.exc.CommandErrorshould be raised if return code is different from 0.callback (ProgressCallbackProtocol) –
callback to return output as a command executes, accepts a function signature of
(output, timestamp). Example usage:def progress_cb(output, timestamp): sys.stdout.write(output) sys.stdout.flush() run(['git', 'pull'], callback=progress_cb)
timeout (float, optional) – Seconds to wait before terminating the subprocess. When the deadline is exceeded the process is sent
SIGTERM(thenSIGKILLafter a short grace period) andlibvcs.exc.CommandTimeoutErroris raised with any output collected so far.None(default) disables the deadline and preserves the legacy behaviour of blocking until the process exits.changes (Upcoming)
----------------
3.10 (When minimum python >=)
pipesize (int = -1 will be added after umask.)
bufsize (int)
executable (StrOrBytesPath | None)
stdin (_FILE | None)
stdout (_FILE | None)
stderr (_FILE | None)
close_fds (bool)
env (_ENV | None)
creationflags (int)
restore_signals (bool)
start_new_session (bool)
pass_fds (Any)
umask (int)
- Return type:
-
libvcs._internal.run._TIMEOUT_KILL_GRACE_SECONDS = 0.5¶
Grace period after
terminate()before escalating tokill().
-
libvcs._internal.run._TIMEOUT_POLL_INTERVAL_SECONDS = 0.1¶
Upper bound on the
selectors.select()wait inside the deadline loop.
-
libvcs._internal.run._wait_with_deadline(proc, *, deadline, timeout, callback, cmd)¶
Wait for
procto exit, enforcing a wall-clock deadline.Drains both
stdoutandstderrconcurrently so a child that fills either kernel pipe buffer (~64 KiB on Linux) cannot deadlock waiting for libvcs to read its output. Usesselectorsto unblock when either stream is readable, when the child exits, or when the per-iteration poll interval expires – whichever comes first.When the deadline is exceeded the subprocess is reaped and
libvcs.exc.CommandTimeoutErroris raised with the bytes captured before the timeout. Otherwise the captured stdout/stderr are returned to the caller alongside the exit code so they can be reused without re- reading the now-drained pipes.- Returns:
(returncode, stdout_bytes, stderr_bytes). A byte buffer isNonewhen the corresponding pipe could not be put into non- blocking mode (Windows pipes, unusual fd types) – in that case the caller should fall back to reading the pipe directly.- Return type:
- Parameters:
Notes
The progress
callbackis invoked forstderrchunks only, matching the legacy non-timeout codepath.stdoutis drained into the returned buffer to prevent the child from blocking on a full pipe, but its chunks are not forwarded to the callback in real time. Callers that want streamingstdoutshould redirect it themselves (e.g.stdout=to a file) rather than relying oncallback.
-
libvcs._internal.run._join_buffer(buffers, stream)¶
Concatenate captured chunks for
stream, orNoneif not tracked.
-
libvcs._internal.run._terminate_process(proc, cmd)¶
Terminate
procgracefully, falling back tokillon the grace.
-
libvcs._internal.run._format_cmd_for_log(cmd)¶
Render
cmdas a flat string for thevcs_cmdlog extra.
-
libvcs._internal.run._drain_stream(stream)¶
Best-effort read of any remaining bytes from a subprocess pipe.