Running commands โ
Run a command, react to failures and start long-running processes.
Run a command โ
python
from io import StringIO
from proclet import Command
out = StringIO()
Command("git").args("status", "--short").stdout(out).exec()
print(out.getvalue())- A command is configured with chainable setters, then run with
exec(). Called without a value, most setters return the current one:cmd.args(),cmd.env(),cmd.cwd()... exec()waits for completion and returns the finishedsubprocess.Popen.- Unless redirected, stdin reads from the null device and the output is discarded.
Handle failures โ
python
from proclet import Command, CommandNotFound, ErrorReturnCode, TimeoutException
try:
plan = (
Command("terraform")
.args("plan", "-detailed-exitcode")
.ok_exit_codes(0, 2) # 2 means "changes pending", not a failure
.timeout(300)
.inherit()
.exec()
)
print("changes pending" if plan.returncode == 2 else "up to date")
except CommandNotFound as err:
print(f"{err.command} is not installed")
except ErrorReturnCode as err:
print(f"{err.cmd} failed with exit code {err.exit_code}")
except TimeoutException as err:
print(f"{err.cmd} took more than {err.timeout}s and was terminated")CommandNotFoundis raised at construction for a name missing fromPATH; aPathis only checked when the command runs.ErrorReturnCodeis raised for an exit code outsideok_exit_codes(...)(default: only0). A process killed by a signal reports a negative exit code.TimeoutExceptionis raised oncetimeout(...)elapses; the process is terminated first.procletnever logs, prints or exits on its own: the caller decides what a failure means.
Run a command in the background โ
python
from proclet import Command
server = Command("python").args("-m", "http.server", "8000").exec(wait_completion=False)
try:
... # talk to the server
finally:
server.terminate()
server.wait()exec(wait_completion=False)returns the runningsubprocess.Popenas soon as it is spawned.timeout(...)andok_exit_codes(...)aren't enforced then; redirect files are still closed once the process exits.