|
shil
|
|
|
Shell-util library for python. Includes helpers for subprocess invocation, shell-formatters / pretty-printers, and more. |
|
Overview
The shil library provides various shell-utilities for python.
Features
Shell-formatters / pretty-printers
- A somewhat usable parser / grammar for bash
- Console support for rich & rich protocols
Subprocess Invocation
There's a lot of shell-related libraries out there, especially for invocation (see for example this list).
The interface for shil is hopefully unsurprising, and something that's convenient but not fancy. It's a utility, and not a huge framework.
The main goal is to provide an API that is simple and stable, without a ton of dependencies.
>>> import shil
>>> proc = shil.invoke('echo hello world')
>>> assert proc.succeeded
>>> assert proc.stdout.strip()=='hello world'
>>>
Beyond such basics, shil includes support for rich output and uses pydantic for datastructures.
See the API docs for more detailed information.
Installation
See pypi for available releases.
$ pip install shil
Usage (CLI)
The shil library publishes a small CLI tool, mostly just for testing & demoing the API behaviour. See the CLI docs for the latest (autogenerated) help.
Usage (API)
See also:
- the unit-tests for some examples of library usage
- the smoke-tests for example usage of stand-alone scripts
OOP-style Dispatch
This uses shil.Invocation and returns shil.InvocationResponse.
>>> import shil
>>> req = cmd = shil.Invocation(command='printf hello-world\n')
>>> resp = cmd()
>>> print(resp.stdout)
hello-world
>>>
Functional approach to dispatch
Use shil.invoke, get back shil.InvocationResponse
>>> import shil
>>> resp = shil.invoke(command='printf hello-world\n')
>>> print(resp.stdout)
hello-world
>>>
Loading data when command-output is JSON
>>> import shil
>>> cmd = shil.Invocation(command="""echo '{"foo":"bar"}'""", load_json=True)
>>> resp = cmd()
>>> print(resp.data)
{'foo': 'bar'}
>>> assert type(resp.data) == type({})
>>> assert resp.data['foo'] == 'bar'
>>>
Serialization with Pydantic
>>> import json, shil
>>> req = cmd = shil.Invocation(command="""echo pipes-are-allowed|grep allowed""")
>>> resp = cmd()
>>> keys = resp.dict().keys()
>>> expected = 'stdout stderr failed failure success succeeded data'.split()
>>> assert all([k in keys for k in expected])
>>>
Caller determines logging
Works like this with basic logger:
>>> import logging, shil
>>> logger = logging.getLogger()
>>> resp = shil.invoke('ls /tmp', command_logger=logger.critical, output_logger=logger.warning)
>>>
Supports using rich-logger too:
>>> import shil
>>> from rich.console import Console
>>> console = Console(stderr=True)
>>> resp = shil.invoke('ls /tmp', command_logger=console.log)
>>>
Rich-console Support
Besides using rich-logger as above, you can use the rich-protocol more directly.
Printing works the way you'd expect for Invocation and InvocationResponse.
>>> import shil, rich
>>> req = cmd = shil.Invocation(command='echo {"foo":"bar"}')
>>> resp = cmd()
>>> rich.print(req)
>>> rich.print(resp)
By default, output looks roughly like this:
Stay DRY with Runners
Runner's are basically just partials on shil.invoke. It's simple but this can help reduce copying around repetitive configuration.
>>> import shil
>>> from rich.console import Console
>>> console=Console(stderr=True)
>>> runner = shil.Runner(output_logger=console.log, command_logger=console.log)
>>> resp = runner('ls /tmp')
>>> assert isinstance(resp,(shil.InvocationResult,))
>>>
Metadata
Release files for shil 2025.8.6.5.22
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| shil-2025.8.6.5.22.tar.gz | 13.1 kB | Without provenance | Details for shil-2025.8.6.5.22.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| shil-2025.8.6.5.22-py3-none-any.whl | Python 3 | none | any | Details for shil-2025.8.6.5.22-py3-none-any.whl |
Total release size: 25.4 kB
Release files / shil-2025.8.6.5.22.tar.gz
| Download URL | shil-2025.8.6.5.22.tar.gz |
|---|---|
| Size | 13.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f6a4d1dd7dc0c5de513addc6ecbacbd365b3a6f34b9173ca8f83265e0ed17d0c
|
|
BLAKE2b-256 checksum How to use checksums |
37335b4ca1823cff71acb92e520f608039925044df41e4b9bd897d3538929541
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Release files / shil-2025.8.6.5.22-py3-none-any.whl
| Download URL | shil-2025.8.6.5.22-py3-none-any.whl |
|---|---|
| Size | 12.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2eb75bf993aab96da1f1cfea89089ee96cbfe89b3faeecbf6bf1d12440e09168
|
|
BLAKE2b-256 checksum How to use checksums |
39ba4332589d6fbfbabe56a2d41cc03ed9c7bb004dfdf8bddab51cc42547d10b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Security for shil 2025.8.6.5.22
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.