Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/houqp/shell.py
shell power for python
https://github.com/houqp/shell.py
python scripting shell
Last synced: 15 days ago
JSON representation
shell power for python
- Host: GitHub
- URL: https://github.com/houqp/shell.py
- Owner: houqp
- License: mit
- Created: 2014-04-30T23:40:55.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-07-27T09:51:33.000Z (over 9 years ago)
- Last Synced: 2024-10-17T06:55:10.253Z (29 days ago)
- Topics: python, scripting, shell
- Language: Python
- Size: 392 KB
- Stars: 96
- Watchers: 8
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.rst
- Changelog: HISTORY.rst
- License: LICENSE
Awesome Lists containing this project
README
shell.py
========.. image:: https://badge.fury.io/py/shell.py.png
:target: http://badge.fury.io/py/shell.py.. image:: https://travis-ci.org/houqp/shell.py.svg?branch=master
:target: https://travis-ci.org/houqp/shell.pyBring the good part of Shell scripting to Python.
Install
-------.. code-block:: bash
$ pip install shell.py
Usage
-----Execute a shell command
.......................Block until return:
.. code-block:: python
>>> from shell import ex
>>> ex('echo hello shell.py').stdout()
'hello shell.py\n'Asynchronous execution:
.. code-block:: python
>>> from shell import ex
>>> c = asex('echo hello shell.py')
>>> # do something else
...
>>> c.stdout() # wait until process exit and read stdout
'hello shell.py\n'Pipe commands
............... code-block:: python
from shell import ex
re = (ex("ifconfig")
| "grep -A 1 eth0"
| "grep inet"
| "awk '{print $2}'"
| "cut -d: -f 2").stdout()Or
.. code-block:: python
from shell import pipe_all
pipe_all(["ls -la ~",
"awk '{print $9}'",
"grep -E '^\.'",
"wc -l"]).stdout()Use string as stdin
..................... code-block:: python
>>> from shell import instream
>>> instream("1 2 3").p("awk '{print $1}'").stdout()
'1\n'This is equivalent to:
.. code-block:: python
>>> from shell import ex
>>> ex("echo 1 2 3").p("awk '{print $1}'").stdout()IO redirect
............Overwrite a file:
.. code-block:: python
>>> from shell import ex
>>> ex('echo yolo').wr('/tmp/out')
>>> ex('echo yolo') > '/tmp/out'Append to a file:
.. code-block:: python
>>> from shell import ex
>>> ex('echo yolo').ap('/tmp/out')
>>> ex('echo yolo') >> '/tmp/out'Run commands in parallel
........................Block until all commands return:
.. code-block:: python
>>> from shell import parallel as par
>>> par.ex_all(['sleep 2', 'sleep 2']) # return in 2sAsynchronous parallel execution:
.. code-block:: python
>>> from shell import parallel as par
>>> pe = par.asex_all(['sleep 2', 'sleep 2']) # return immediately
>>> # do something else
...
>>> pe.wait()Set working directory
.....................Set the directory in which the commands are executed:
.. code-block:: python
>>> with shell.cwd('~/server/data/upload/') as old_path:
>>> shell.ex('find ./images -name "*.png"') | 'minify ./public' >> 'upload.log'This is equivalent to:
.. code-block:: python
>>> shell.ex('find ~/server/data/upload/images -name "*.png"') | 'minify ~/server/data/upload/public' >> '~/server/data/upload.log'
See test cases for more examples.
Tests
-----Run tests with nosetests(at least v1.3.0):
.. code-block:: bash
$ make test