shtk 0.9.7
Shell ToolKit (SHTK)
License: BSD 3-Clause License
Keywords: shell subprocess

# Python Shell Toolkit (SHTK)

Python SHTK is a python module that seeks to make replacing shell scripts with
Python scripts an easier process.  Python has a number of syntax advantages
over traditional shell scripting languages such as BASH, including:
* Classes
* Modules
* With statements
* Try/Except statements
* Async and await for coroutines

The module and package oriented structure of Python's toolchain enables broad
code re-use and redistribution. Python also benefits from a wide selection of
built-in modules, and expands itself via the wide assortment of packages
that can be quickly installed using its built-in package manager. 

Finally, built-in automated test harnesses and long-standing code-quality
integrations make it easy to review, document, test, and maintain its
libraries.  

SHTK is written with the assumption that you want to run more than one command.
Towards this end, improvements over Python's built-in subprocess library
include:
* Much shorter code -- designed to be as close to BASH as possible
* Easy piping of stdout to other commands' stdin
* Easy redirects to files
* Shell objects to track and manage cwd and environment variables
* An evaluate() function that returns the text a command wrote to stdout
* Optional NonzeroExitCodeException raised in response to non-zero exit codes
* Connects commands to sys.stdin, sys.stdout, and sys.stderr by default

The author's primary intended use cases for Python SHTK include replacing BASH
scripts that automate builds of disk images, docker containers, and system
configurations.

## Installation
Using pip you can install shtk as follows:
```
pip3 install shtk
```

Or you can install the module from source as follows:
```
pip3 install .
```

## Tests
To run the automated tests, run the following command from the project's root
directory:

```
pip3 install coverage
python3 run_tests.py
```

## Documentation
The documentation is publically available at https://shtk.readthedocs.org

To build the documentation from source, run the following which generates
documention in ./docs/html/index.html

```
cd docs
make html
cd ..
```

## Examples

```
import shtk

sh = shtk.Shell.get_shell()

ls = sh.command('ls')
wc = sh.command('wc')
cat = sh.command('cat')
sleep = sh.command('sleep')
touch = sh.command('touch')

#touch tmp.txt
sh(touch('tmp.txt'))

#cat tmp.txt
sh(cat('tmp.txt'))

#cat tmp.txt | wc -l
sh(cat('tmp.txt') | wc('-l'))

#wc -l < tmp.txt
sh(wc('-l').stdin('tmp.txt'))

#ls | wc -l > /dev/null
sh(ls | wc('-l').stdout(None))

#ls | wc -l > tmp.txt
sh(ls | wc('-l').stdout('tmp.txt'))

#ls | wc -l >> tmp.txt
sh(ls | wc('-l').stdout('tmp.txt', mode='a'))

with open('test_file1.txt', 'w') as fout:
    msg = """
abc
xyz
The quick brown fox jumps over the lazy dog.
""".lstrip()
    print(msg, file=fout)

try:
    # ls test_file2.txt 2> /dev/null | wc -l
    sh(
        ls('test_file2.txt').stderr('/dev/null') | wc('-l')
    )
except shtk.NonzeroExitCodeException:
    print("Caught a failure")

sh(
    ls('test_file1.txt')
)

#echo $(ls | wc -l)
print(sh.evaluate(ls | wc('-l')).strip())

```

More examples can be found in the source code's examples directory, but they're
still under construction.

BSD 3-Clause License

Copyright (c) 2021, Jon Roose
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.