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.
Install
$ python3 -m pip install simpleshell
Use
from simpleshell import ss
output = ss('ls -la') # Prints the result on the screen through stdout
pprint(vars(output)) # Prints the dict below
{'args': 'ls -la',
'returncode': 0,
'stderr': [''],
'stdout': ['total 28',
'drwxr-xr-x 13 calvin staff 416 Oct 23 00:08 .',
'drwxr-xr-x 7 calvin staff 224 Oct 22 16:56 ..',
'drwxr-xr-x 13 calvin staff 416 Oct 22 23:51 .git',
'-rw-r--r-- 1 calvin staff 63 Oct 22 21:28 .gitignore',
'drwxr-xr-x 9 calvin staff 288 Oct 23 00:08 .idea',
'-rw-r--r-- 1 calvin staff 1036 Oct 22 22:27 LICENSE',
'-rw-r--r-- 1 calvin staff 287 Oct 22 23:24 Makefile',
'-rw-r--r-- 1 calvin staff 4320 Oct 23 00:08 README.md',
'drwxr-xr-x 4 calvin staff 128 Oct 22 23:51 dist',
'-rw-r--r-- 1 calvin staff 104 Oct 22 21:57 pyproject.toml',
'-rw-r--r-- 1 calvin staff 594 Oct 22 23:50 setup.cfg',
'drwxr-xr-x 4 calvin staff 128 Oct 22 23:51 src',
'drwxr-xr-x 2 calvin staff 64 Oct 22 17:03 tests',
'']}
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.
This is useful for using grep to monitor for something to disappear.
In the below example, grep is waiting for process_waiting_to_finish process to complete and exit.
>>> output = ss('ps | grep "[p]rocess_waiting_to_finish"', print_output_on_error=False, exit_on_error=False)
>>> print(type(output))
<class 'subprocess.CalledProcessError'>
>>> print(output.returncode)
1
Examining the output's return code is important. In the below example, we have specified an invalid flag -Y to grep,
and as a result grep exited with 2. This is different from the previous example which exited with 1,
which signals that the error is about missing search results, as opposed to failing because of an invalid flag.
>>> output = ss('ps | grep -Y "[p]rocess_waiting_to_finish"', print_output_on_error=False, exit_on_error=False)
>>> print(output.returncode)
2
-
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simpleshell-0.0.9.tar.gz.
File metadata
- Download URL: simpleshell-0.0.9.tar.gz
- Upload date:
- Size: 4.3 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.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
559ee4e6de0ac24ed37374beed2112ab0643645c89c0f6bd74fc672cc1cf11ee
|
|
| MD5 |
8415a9f0decf2dd72a97ff3082ab9293
|
|
| BLAKE2b-256 |
b7f6f44148317e267d7c1ec1561bfe16b3e5afbb333d67df88ca7473c14a7431
|
File details
Details for the file simpleshell-0.0.9-py3-none-any.whl.
File metadata
- Download URL: simpleshell-0.0.9-py3-none-any.whl
- Upload date:
- Size: 4.5 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.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5029f5c74097f6e8d7a02dc5c3260e5b895d57cad7f03c09d6b3bf86d751e003
|
|
| MD5 |
b1155a2167373c8a6f544e21ac4fb9a5
|
|
| BLAKE2b-256 |
6b3531fb3e9c5a72e72d1abede3458a7617451001961d0a6ed0a20b4e35425f8
|