sh 0.107
pip install sh==0.107
Newer version available (2.2.1)
Released:
Python subprocess wrapper
Navigation
Unverified details
These details have not been verified by PyPIProject links
Meta
- License: MIT License (MIT)
- Author: Andrew Moffat
Classifiers
- Development Status
- Environment
- Intended Audience
- License
- Programming Language
- Topic
Project description
``sh`` is a unique subprocess wrapper that maps your system programs to
Python functions dynamically. ``sh`` helps you write shell scripts in
Python by giving you the good features of Bash (easy command calling, easy
piping) with all the power and flexibility of Python.
```python
from sh import ifconfig
print ifconfig("eth0")
```
``sh`` is not a collection of system commands implemented in Python.
# Getting
PATH:
```python
import sh
ffmpeg = sh.Command("/usr/bin/ffmpeg")
ffmpeg(movie_file)
```
The last usage pattern is for trying ``sh`` through an interactive REPL. By
default, this acts like a star import (so all of your system programs will be
immediately available as functions):
1, $2, etc by using
ARG1, ARG2, etc:
```python
print ARG1, ARG2
# if an argument isn't defined, it's set to None
if ARG10 is None: do_something()
```
You can access the entire argparse/optparse-friendly list of commandline
arguments through "ARGV". This is recommended for flexibility:
```python
import argparse
parser = argparse.ArgumentParser(prog="PROG")
parser.add_argument("-x", default=3, type=int)
ns = parser.parse_args(ARGV)
print ns.x
```
## Weirdly-named Commands
``sh`` automatically handles underscore-dash conversions. For example, if you want
to call apt-get:
```python
apt_get("install", "mplayer", y=True)
```
``sh`` looks for "apt_get", but if it doesn't find it, replaces all underscores
with dashes and searches again. If the command still isn't found, a
CommandNotFound exception is raised.
Commands with other, less-commonly symbols in their names must be accessed
directly through the "Command" class wrapper. The Command class takes the full
path to the program as a string:
```python
p27 = Command(which("python2.7"))
print p27("-h")
```
The Command wrapper is also useful for commands that are not in your standard PATH:
```python
script = Command("/tmp/temporary-script.sh")
print script()
```
## Non-standard Exit Codes
Normally, if a command returns an exit code that is not 0, ``sh`` raises an exception
based on that exit code. However, if you have determined that an error code
is normal and want to retrieve the output of the command without ``sh`` raising an
exception, you can use the "_ok_code" special argument to suppress the exception:
```python
output = sh.ls("dir_that_exists", "dir_that_doesnt", _ok_code=2)
```
In the above example, even though you're trying to list a directory that doesn't
exist, you can still get the output from the directory that does exist by telling
the command that 2 is an "ok" exit code, so don't raise an exception.
_ok_code can also take a list or tuple of numbers for multiple ok exit codes.
Python functions dynamically. ``sh`` helps you write shell scripts in
Python by giving you the good features of Bash (easy command calling, easy
piping) with all the power and flexibility of Python.
```python
from sh import ifconfig
print ifconfig("eth0")
```
``sh`` is not a collection of system commands implemented in Python.
# Getting
```python
import sh
ffmpeg = sh.Command("/usr/bin/ffmpeg")
ffmpeg(movie_file)
```
The last usage pattern is for trying ``sh`` through an interactive REPL. By
default, this acts like a star import (so all of your system programs will be
immediately available as functions):
ARG1, ARG2, etc:
```python
print ARG1, ARG2
# if an argument isn't defined, it's set to None
if ARG10 is None: do_something()
```
You can access the entire argparse/optparse-friendly list of commandline
arguments through "ARGV". This is recommended for flexibility:
```python
import argparse
parser = argparse.ArgumentParser(prog="PROG")
parser.add_argument("-x", default=3, type=int)
ns = parser.parse_args(ARGV)
print ns.x
```
## Weirdly-named Commands
``sh`` automatically handles underscore-dash conversions. For example, if you want
to call apt-get:
```python
apt_get("install", "mplayer", y=True)
```
``sh`` looks for "apt_get", but if it doesn't find it, replaces all underscores
with dashes and searches again. If the command still isn't found, a
CommandNotFound exception is raised.
Commands with other, less-commonly symbols in their names must be accessed
directly through the "Command" class wrapper. The Command class takes the full
path to the program as a string:
```python
p27 = Command(which("python2.7"))
print p27("-h")
```
The Command wrapper is also useful for commands that are not in your standard PATH:
```python
script = Command("/tmp/temporary-script.sh")
print script()
```
## Non-standard Exit Codes
Normally, if a command returns an exit code that is not 0, ``sh`` raises an exception
based on that exit code. However, if you have determined that an error code
is normal and want to retrieve the output of the command without ``sh`` raising an
exception, you can use the "_ok_code" special argument to suppress the exception:
```python
output = sh.ls("dir_that_exists", "dir_that_doesnt", _ok_code=2)
```
In the above example, even though you're trying to list a directory that doesn't
exist, you can still get the output from the directory that does exist by telling
the command that 2 is an "ok" exit code, so don't raise an exception.
_ok_code can also take a list or tuple of numbers for multiple ok exit codes.
Project details
Unverified details
These details have not been verified by PyPIProject links
Meta
- License: MIT License (MIT)
- Author: Andrew Moffat
Classifiers
- Development Status
- Environment
- Intended Audience
- License
- Programming Language
- Topic
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file sh-0.107.tar.gz
.
File metadata
- Download URL: sh-0.107.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3170a15bb91fa31c9fbe7458dd2181c4251d0786a018c9b7754caca12f7e690 |
|
MD5 | d92e8a7b5c729e3d4620a5964563ed54 |
|
BLAKE2b-256 | 618b56df0200beba5730435cd55e288f9acab3de87b95f58fba835ea294cb917 |