Process module¶
The Process module provides a friendlier wrapper around subprocess.run
.
String argument processing¶
from boldi.proc import run
run("echo 'Hello, World!'")
# Prints: Hello, World!
# Returns: CompletedProcess(args=['echo', 'Hello, World!'], returncode=0)
As you can see from the return value, echo
was invoked with one argument: "Hello, World!"
.
Simple string args passed to run()
are processed via shlex.split
.
Quoting arguments¶
Another way to quote arguments is to pass them inside a list.
from boldi.proc import run
run("echo", ["Hello, World!"])
# Prints: Hello, World!
# Returns: CompletedProcess(args=['echo', 'Hello, World!'], returncode=0)
This example is equivalent to the previous one.
Think of the [
list ]
as a way of quoting arguments,
telling the run()
function not to further process those arguments.
Other positional arguments¶
Other types of positional arguments are converted to str
and directly passed as arguments to the subprocess.
This is useful for passing numbers or pathlib.Path
objects that may contain spaces.
Keyword arguments¶
Keyword arguments are the same as in subprocess.run
except that a few default values are different.
See the boldi.proc.run
API docs for details.
Running Python scripts¶
The run_py()
function is a shortcut for running Python scripts.
It correctly selects the current Python interpreter (sys.executable
) as the first argument.
All other arguments are the same as in run()
.
Install¶
Boldi's Process module is distributed as the
boldi-proc
Python package,
thus to install it, run:
...or add "boldi-proc"
as a dependency to your project.
Import¶
Import the module like so:
API¶
boldi.proc
¶
RunArgs
¶
Bases: TypedDict
Arguments to subprocess.run
or subprocess.Popen
.
Source code in pkg/boldi-proc/boldi/proc.py
bufsize: int
¶
capture_output: bool
¶
check: bool
¶
close_fds: bool
¶
cwd: Path
¶
encoding: str
¶
env: Mapping[str, str]
¶
errors: Any
¶
extra_groups: Iterable[str | int]
¶
group: str | int
¶
input: str | None
¶
pipesize: int
¶
preexec_fn: Callable[..., Any]
¶
process_group: int
¶
shell: bool
¶
startupinfo: Any
¶
stderr: IO | int
¶
stdin: IO | int
¶
stdout: IO | int
¶
text: bool
¶
timeout: float
¶
umask: int
¶
universal_newlines: bool
¶
user: str | int
¶
args_iter(*args: Union[str, List[Any]]) -> Iterable[str]
¶
Split mixed and/or quoted command line arguments into a simple list of arguments.
Parameters:
-
args
(Union[str, List[Any]]
, default:()
) –Command line arguments, provided as positional arguments. Each argument is processed differently depending on its type. Strings are split using
shlex.split
into further arguments. Lists are replaced by their contents (each item converted to a string). Other types are converted to strings usingstr
.
Returns:
Examples:
Common use cases:
result = ["prog", "-a=b", "d e", "f"]
assert list(args_iter("prog -a=b 'd e' f")) == result
assert list(args_iter("prog -a=b", ["d e"], "f")) == result
Any argument can be given either as a string or a list, and they can be combined arbitrarily:
result = ["prog", "-a=b", "d e", "f"]
assert list(args_iter("prog -a=b", "'d e' f")) == result
assert list(args_iter("prog", "-a=b", '"d e"', "f")) == result
assert list(args_iter(["prog", "-a=b", "d e", "f"])) == result
Source code in pkg/boldi-proc/boldi/proc.py
run(*args: Union[str, List[Any]], **kwargs: Unpack[RunArgs]) -> subprocess.CompletedProcess
¶
Run a subprocess using the provided command line arguments and updated defaults.
Parameters:
-
args
(Union[str, List[Any]]
, default:()
) –Command line arguments, provided as positional arguments. As defined in
args_iter
. -
kwargs
(Unpack[RunArgs]
, default:{}
) –Arguments to
subprocess.run
. Defaults tocheck=True
,text=True
, and connecting tosys.{stdin, stdout, stderr}
, unless otherwise set by the caller.
Returns:
-
CompletedProcess
–Completed process object.
Source code in pkg/boldi-proc/boldi/proc.py
run_py(*args: Union[str, List[Any]], **kwargs: Unpack[RunArgs]) -> subprocess.CompletedProcess
¶
Run a subprocess using the current Python interpreter, the provided command line arguments and updated defaults.
Parameters:
-
args
(Union[str, List[Any]]
, default:()
) –Command line arguments, provided as positional arguments. As defined in
args_iter
. -
kwargs
(Unpack[RunArgs]
, default:{}
) –Arguments to
subprocess.run
. As defined inrun
.
Returns:
-
CompletedProcess
–Completed process object.