Skip to main content

ShellWrap

This small module allows you to write shell commands inside your Python program. This module is not a replacement for subprocess or any of the Python modules that are a high level abstractions on top of program and command execution or shell interaction. On the contrary ShellWrap executes a shell interpreter that will live as long as your Python script is running. A simple function decorator will allow Python function to have a shell script body. These functions will be augmented and passed to the shell interpreter. Let's start with some examples:

Example: Check if a site is UP

This example will regularily check for the given site to respond with a 200, Ok. This is handy when you are waiting for a site to become responsive or waiting for it to boot up.

import time
from shellwrap import ShellWrap
shell = ShellWrap('bash').get_shell()

@shell()
def is_the_site_up(url):
    'curl -sL -m 2 -w "%{http_code}" "${url}" -o /dev/null | grep -q 200'

while is_the_site_up('https://google.com').fails():
    time.sleep(4)

You just have to annotate a function with the @shell decorator and it will be parsed and each line (a loose string) will be fed into a running bash interpreter.

Example: Getting results

The return value of calling a function annotated with @shell is a Result type that has a few convenience functions:

  • Result().fails() returns True if the command failed (its exitcode is != 0)
  • Result().success() returns True if the command succeeded (equivalent to not .fails())
  • Result().exitcode contains the exit code of the last command executed in your function
  • Result().stdout contains all the output of the execution of your function

Example: Passing arguments to functions

You can define a function with arguments, like is_the_site_up above and these arguments will be converted into variables for the execution of your function, inside the bash environment.

@shell()
def passing_arguments_to_funcs(arg0, arg1, someother):
    'echo "got $arg0 and $arg1"'
    'echo "and also $someother"'

result = passing_arguments_to_funcs('hello', 10, 'love you bash')

assert(result.exitcode == 0)

This will result in the following being displayed:

got hello and 10
and also I love you bash

Example: Timeouts

You might have a process that you only want to execute for a few seconds, for it to return. If the process get stuck, you want to kill it and continue with your life. This is possible with ShellWrap by using the timeout parameter in your decorator.

@shell(timeout=3)
def will_timeout_in_3_secs(mymessage):
    'sleep 1'
    'echo "${mymessage}"'
    'sleep 1'

result = will_timeout_in_3_secs('Only patience will reveal the truth.')
if result.success():
    print('Your patience will be rewarded')

In the last example, we will give the decorated function 10 seconds to run, and we know it will take around 8 seconds to complete. The first run will succeed, but if you change the timeout parameter to 7, the function will fail. Will still print the message passed as an argument to the function (the echo was called), but the function will result in an error, as it was killed because of the timeout.

Contributing to the project

Please see our Contribution page!

License

The project is distributed under the MIT license.

Metadata

Release files for shellwrap 0.1

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

Source distribution (sdist)

Source distribution for shellwrap 0.1
File Size Uploaded
shellwrap-0.1.0.tar.gz 5.8 kB Details

Built distributions (wheels)

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

Total release size: 18.8 kB

Release files / shellwrap-0.1.0.tar.gz

Download URL shellwrap-0.1.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bcc5b64832547dab862924a7317ab4ce45b6e90073d0ae771e0975172800dc8a
BLAKE2b-256 checksum
How to use checksums
d861b272318a67ad3d47ea0986bb6858cbfc43f439d68803972ff3d5ce99e7a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3

Release files / shellwrap-0.1.0-py3-none-any.whl

Download URL shellwrap-0.1.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e0fb45b02c52aede296560a0a792d83dfd278eafa493655b368701105ead58d
BLAKE2b-256 checksum
How to use checksums
f0d65a9f2c11c0d748b07b232923cc8c850d4c68db8e26ab5efae2f85936072c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3

Release files / shellwrap-0.1-py3-none-any.whl

Download URL shellwrap-0.1-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5f2c13c655f28b00fcedfb6f8c76b1754e8668e7e17123e20b263c3940546fd
BLAKE2b-256 checksum
How to use checksums
bf9492c3954997be395d455a674d5b2118de99d1499f2fbec7625dbb1e46b26d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3

Release history Release notifications | RSS feed

This release

0.1 This release

3 release files

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