No project description provided
Project description
[Normal]PyRunner
PyRunner allows you to run shell commands from Python in a way similar to how it is happening inside shell scripts
The following features apply:
- you don't think about separation of arguments and command; entire string is executed instead
- you don't think if command is built-in or external
- output of a command to either
stdout
orstderr
is printed into appropriate stream and visible to user (until muted) - called command is printed
- commands can be called with
sudo
and the password for the current user will be asked once - entire script can be called with
sudo
and the password, asked by sudo command, will be in effect - the result of a command can be grabbed, including
stdout
,stderr
and return code
The idea is to completely avoid boilerplate coding when writing shell automation scripts in Python.
Examples
Importing library
>>> import pyrunner
Calling ls
command
>>> _=pyrunner.exec("ls")
$ 'ls'
examples.py
__pycache__
pyrunner.py
README.md
setup.py
Note _=
is used for Python REPL to suppress result printing.
Note command is printed with $
prefix.
Collecting command output as a whole string
>>> hostname=pyrunner.exec("hostname")
$ 'hostname'
myhost
>>> print(hostname)
myhost
Collecting command output as a list of strings
>>> files = []
>>> _=pyrunner.exec("ls", out_lines=files)
$ 'ls'
mnt
__pycache__
pyrunner.py
README.md
setup.py
>>> files
['mnt', '__pycache__', 'pyrunner.py', 'README.md', 'setup.py', '']
Suppressing printouts
>>> pyrunner.exec("ls", mute_out=True)
$ 'ls'
'mnt\n__pycache__\npyrunner.py\nREADME.md\nsetup.py'
Calling uname
command with arguments
>>> _=pyrunner.exec("uname -a")
$ 'uname -a'
Linux myhostname 5.15.0-86-generic #96-Ubuntu SMP Wed Sep 20 08:23:49 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Calling sudo command (mount
)
>>> _=pyrunner.exec("mount /dev/loop46 mnt", sudo=True)
$ sudo mount /dev/loop46 mnt
[sudo] password for myuser:
mount: /home/myuser/mypath/pyrunner/mnt: WARNING: source write-protected, mounted read-only.
Note sudo=True
is used to initiate sudo
operations.
Note command is now prefixed with $ sudo
Checking mount is worked
>>> _=pyrunner.exec("ls mnt")
$ 'ls mnt'
bin
chromium.png
etc
firstrun
lib
man1
meta
snap
tests
usr
showing some image content.
Unmounting
>>> _=pyrunner.exec("umount mnt", sudo=True)
$ sudo umount mnt
Note password is not asked.
Security notes
Password is stored in Python variable. It is okay with standalone scripts, but can be not okay with interactive shells
like Jupyter. sudoers
check is not supported yet.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file NormalPyRunner-0.1.2.tar.gz
.
File metadata
- Download URL: NormalPyRunner-0.1.2.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49d7db872c36de8433f4e4d1f1def0a121b8ee7afeff70bdf495b5d44e6af0d7 |
|
MD5 | 760e62afd9c28ab84751993640af39b3 |
|
BLAKE2b-256 | 1ceba298f489b11d45918518b8bcf09213bad83735e96948910f7d8c11e3506e |
File details
Details for the file NormalPyRunner-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: NormalPyRunner-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 650884ff3c4cf7bbc8e108ed4af6015d8ac19b1536b1c082b5b3db61954d9fd4 |
|
MD5 | 6285579e64b92c4847194d5b714312b9 |
|
BLAKE2b-256 | ce739369b8545f334cc3d295ddd558a8e8190d9ba738e47e98abb6e2aa63f828 |