Skip to main content

A simple wrapper for synchronous shell commands

Project description

Simple Shell

Run a shell command, and receive stdout, stderr, and returncode. It's dead simple.

$ python3
[...]
>>> from simpleshell import ss
>>> output = ss('head -3 LICEN*')
MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy

>>> print(output)
CompletedProcess(
    args='head LICEN*',
    returncode=0,
    stdout=['MIT License', '', 'Permission is hereby granted, free of charge, to any person obtaining a copy'],
    stderr=['']
)
>>>

All calls are synchronous, therefore it's not possible to see the output until the command exits. This makes Simple Shell unsuitable for tailing a log.

Return values

On success

CompletedProcess object with member variables args, returncode, stdout, stderr.

stdout and stderr may be a str or a list[str] based on optional parameter convert_stdout_stderr_to_list.

On error

if optional parameter 'exit_on_error':
    nothing
else:
    subprocess.CalledProcessError exception object

Optional parameters

  • print_output_on_success=True

When False, nothing gets printed when the command exits with 0.

>>> output = ss('head -3 LICEN*', print_output_on_success=False)
>>>
  • print_output_on_error=True

When False, nothing gets printed when the command exists with a non-0.
In the below example, the command exited with return code 127 and caused the python3 process to exit.

>>> output = ss('invalid command', print_output_on_error=False)
$ echo $?
127
$
  • convert_stdout_stderr_to_list=True

When False, output.stdout and output.stderr are strings with \n embedded in them.

>>> output = ss('head -3 LICEN*', convert_stdout_stderr_to_list=False)
MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy

>>> print(output)
CompletedProcess(
    args='head LICEN*',
    returncode=0,
    stdout='MIT License\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\n',
    stderr=''
)
>>>
  • keep_empty_lines=True

When False and convert_stdout_stderr_to_list is True, empty lines form output.stdout and output.stderr lists are removed.

In the below example, there is an empty line after the first line MIT License, but output.stdout list doesn't contain the empty line.

⚠️ This parameter does not change the way output is printed.

>>> output = ss('head -3 LICEN*', keep_empty_lines=False)
MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy

>>> print(output)
CompletedProcess(
    args='head LICEN*',
    returncode=0,
    stdout=['MIT License', 'Permission is hereby granted, free of charge, to any person obtaining a copy'],
    stderr=[]
)
>>>
  • exit_on_error=True

When False, a command exiting with a non-0 return code doesn't cause the python3 process to exit. Afterward, subprocess.CalledProcessError exception object is returned so that the caller can further examine the error.

>>> output = ss('invalid command', exit_on_error=False)
/bin/sh: invalid: command not found

>>> print(type(output))
<class 'subprocess.CalledProcessError'>
>>> print(output)
Command 'invalid command' returned non-zero exit status 127.
>>>
  • echo=False

When True, the command is printed before the output is printed. This is useful for creating a screen capture which shows the command that was run.

⚠️ The leading $ does not change to # even if you're root. PR welcome.

>>> output = ss('head -3 LICEN*', echo=True)
$ head -3 LICEN*
MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy

>>>
  • timeout=60

Number of seconds to wait for the process to finish before exiting with an error. Since all calls are synchronous, it's not possible to see the output until the command exits.

In the below example, the exception detail was printed because print_output_on_error defaults to True, and the python3 process exited because exit_on_error also defaults to True.

>>> ss('sleep 10', timeout=3)
Command 'sleep 10' timed out after 3 seconds
$

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

simpleshell-0.0.4.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

simpleshell-0.0.4-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file simpleshell-0.0.4.tar.gz.

File metadata

  • Download URL: simpleshell-0.0.4.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for simpleshell-0.0.4.tar.gz
Algorithm Hash digest
SHA256 adce2ee1d899fb08b99c53a1b4167a1f848c6f27344034cc2d25289463d1d1f8
MD5 9a26b5da692a82499c730615c5257a96
BLAKE2b-256 2bd2cc9f991ade97cb8d5be2aa543ed31957014018078919caa513c63af29230

See more details on using hashes here.

File details

Details for the file simpleshell-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: simpleshell-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for simpleshell-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bcc7bd8e78206bab1d5d3a120993b1bd129d94d6572f32ae205b7062c35ebc73
MD5 dea0b936b40f794faf903a13174db52c
BLAKE2b-256 1f9b7b9353145233a895847dcc578e7afeb0035ebb703bc62a1fed68a2a1b375

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