Skip to main content

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.

Release files for shtk 0.9.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shtk 0.9.7
File Size Uploaded
shtk-0.9.7.tar.gz 37.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shtk 0.9.7
File Interpreter ABI Platform
shtk-0.9.7-py3-none-any.whl Python 3 none any Details

Total release size: 65.8 kB

Release files / shtk-0.9.7.tar.gz

Download URL shtk-0.9.7.tar.gz
Size 37.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c0df08f99376d6830dc3af50719b66e675e117930cc9b8d9ec29706b1838353f
BLAKE2b-256 checksum
How to use checksums
4c79de0a7fcb7670b26f7b045d09a03bb5fbd051436f9652ad78998e7083cde0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

Release files / shtk-0.9.7-py3-none-any.whl

Download URL shtk-0.9.7-py3-none-any.whl
Size 28.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4297722694a8605cc9e83e2826556048980a9e0e67109b7a690c427f59379f58
BLAKE2b-256 checksum
How to use checksums
7088a7bbd113851412be14fbe4c1638cd9c7fcb2f6453b407b836c5b1824eedc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.9.7 This release

2 release files

0.9.6

3 release files

0.9.5

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page