Coming from sh โ
sh turns commands into Python functions: sh.git("status"). proclet builds them explicitly: Command("git").args("status"). Most sh special keyword arguments (_timeout, _ok_code, _in, _out...) map to one proclet method each.
Why switch โ
- Windows.
shis POSIX only;procletruns on Linux, macOS and Windows. - Typed. Every setting is a method your type checker and IDE know about, instead of
**kwargsresolved at run time. - Streaming pipelines.
sh.wc("-l", _in=sh.ls())runslsto completion and buffers its output before startingwc(unless_piped=True).procletpipelines run all stages at once, connected with raw OS pipes. - Graceful timeouts.
shkills a timed-out process withSIGKILLby default;procletsendsSIGTERMfirst and kills only after a grace period. - Privileges and priority. Run as another user and group, choose supplementary groups, lower the CPU and IO priority.
sh is terser for quick, interactive-style scripts on POSIX: if that is what you use it for and you don't need Windows, it remains a fine choice.
Cheat sheet โ
sh | proclet |
|---|---|
sh.git("status") returns stdout as str | Command("git").args("status").stdout(out).exec(), with out = StringIO() |
sh.Command("/opt/bin/tool") | Command(Path("/opt/bin/tool")) |
_ok_code=[0, 2] | .ok_exit_codes(0, 2) |
_timeout=10 | .timeout(10) |
_cwd="repo" | .cwd(Path("repo")) |
_env={...} | .env({...}), or .env(EnvCmd.inherit().add({...}).build()) to keep the current env |
_in="data" | .stdin("data") |
_out=callback / _err=callback | .stdout(callback) / .stderr(callback) |
_out="build.log" | .stdout(Path("build.log")) |
_err_to_out=True | pass the same callable or file object to .stdout(...) and .stderr(...) |
_fg=True | .inherit() |
_bg=True | .exec(wait_completion=False) |
await sh.ls(_async=True) | await Command("ls").aexec() |
sh.wc("-l", _in=sh.ls()) | Command("ls").pipe(Command("wc").args("-l")) |
git = sh.git.bake("-C", repo) | Command("git").copy(base), or a Bind subclass |
_uid=1000 | .user(1000), plus .group(...) and .extra_groups(...) |
sh.ErrorReturnCode | proclet.ErrorReturnCode |
sh.TimeoutException | proclet.TimeoutException |
sh.CommandNotFound | proclet.CommandNotFound |
Getting output back โ
sh returns stdout as a string. In proclet, output goes where you send it, so collect it in a StringIO. If your code does this a lot, a three-line helper brings back the sh feel:
from io import StringIO
from proclet import Command
def output(cmd: Command) -> str:
out = StringIO()
cmd.stdout(out).exec()
return out.getvalue()
branch = output(Command("git").args("branch", "--show-current")).strip()Unless redirected, proclet discards output and reads stdin from the null device.
Errors โ
The exception names are the same, the details differ:
# sh
import sh
try:
sh.make("build")
except sh.ErrorReturnCode as err:
print(err.exit_code, err.stderr.decode())# proclet
from io import StringIO
from proclet import Command, ErrorReturnCode
errors = StringIO()
try:
Command("make").args("build").stderr(errors).exec()
except ErrorReturnCode as err:
print(err.exit_code, errors.getvalue())shraises a subclass per exit code (ErrorReturnCode_2...) and attaches the capturedstdout/stderr.procletraisesErrorReturnCodewithexit_codeandcmd: capture the streams you need with a redirect.sh.CommandNotFoundis raised when the command is looked up;procletraisesCommandNotFoundwhen theCommandis built (for a name) or run (for aPath).
Baking โ
bake(...) presets arguments. In proclet, copy(...) takes every setting of another command, and Bind turns a binary into a class:
from proclet import Command
base = Command("git").args("-C", "repo").timeout(30)
# args() replaces the arguments: start from the copied ones to extend them.
Command("git").copy(base).args(*base.args(), "fetch", "--all").exec()For anything used in more than one place, prefer a Bind: it keeps the shared context (cwd, env, user, priority) and gives each operation a typed method.
Differences to watch for โ
- No TTY.
shconnects stdout to a pseudo-terminal by default (_tty_out=True), so tools behave as in a terminal: colours, progress bars, line buffering.procletuses pipes, likesubprocess: some tools drop colours or buffer their output. Pass the tool's own flag when you need it (--color=always,PYTHONUNBUFFERED=1...). - Callbacks.
shpasses each line with its newline, so doesproclet: wrap a logger withstrip_redirect(...).sh's extra callback arguments (stdin queue, process) have no equivalent; use aQueuewith.stdin(...)to feed input. - Iteration.
_iter=Truehas no direct equivalent: handle lines in a.stdout(callback). - User switching.
_uidsets the user's primary group and all its groups. Inproclet, set.group(...)explicitly; supplementary groups are kept unless you pass.extra_groups(...).
See also: Why proclet, Recipes.