Skip to main content

Shell ToolKit (SHTK)

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shtk-0.9.7.tar.gz (37.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shtk-0.9.7-py3-none-any.whl (28.2 kB view details)

Uploaded Python 3

File details

Details for the file shtk-0.9.7.tar.gz.

File metadata

  • Download URL: shtk-0.9.7.tar.gz
  • Upload date:
  • Size: 37.5 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for shtk-0.9.7.tar.gz
Algorithm Hash digest
SHA256 c0df08f99376d6830dc3af50719b66e675e117930cc9b8d9ec29706b1838353f
MD5 cc2b675dcf3fd15d6a71b444118b942a
BLAKE2b-256 4c79de0a7fcb7670b26f7b045d09a03bb5fbd051436f9652ad78998e7083cde0

See more details on using hashes here.

File details

Details for the file shtk-0.9.7-py3-none-any.whl.

File metadata

  • Download URL: shtk-0.9.7-py3-none-any.whl
  • Upload date:
  • Size: 28.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for shtk-0.9.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4297722694a8605cc9e83e2826556048980a9e0e67109b7a690c427f59379f58
MD5 c4d296c5163112515191740e659a1e74
BLAKE2b-256 7088a7bbd113851412be14fbe4c1638cd9c7fcb2f6453b407b836c5b1824eedc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page