Interactive shell object that remembers previous commands, path and env variables
Project description
MemShell
Ever have trouble with Makefiles or Jenkinsfiles or other scripting tools that simply invoke a new shell for every command or block of commands, forgetting what was sourced or what your current path is? (I'm looking at you NPM...) Wouldn't it be nicer if these tools just used a single shell instance so that you can just treat it like you are using your terminal?
That is what this library aims to fix. Using a bit of trickery it is able to create a long-running shell that your Python script can interact with, and it will remember the environment variables, including the PATH and PWD, for as long as the shell object is in scope!
Usage
Basics
from memshell import Shell
shell = Shell()
result = shell.exec(
"pwd",
"cd ~",
"ls",
"cd code",
"ls"
)
print(result.std_out)
print(result.std_err)
print(result.return_code)
shell.close()
Shell.exec
takes variatic arguments as strings of commands to run and returns one
combined output
To set -e
mode pass it in to the exec
method with the modes
argument.
result = shell.exec("spam", "ls", modes=["-e"]) # this will fail at the first command
# (unless you have an executable called 'spam' in your PATH, that is)
print(result.std_out) # ''
print(result.return_code) # 127
print(result.std_err) # 'zsh: command not found: spam'
To get the output from each command individually, use the exec_all
method which takes
a list of strings as commands.
results = shell.exec_all(["pwd", "cd ~", "ls"])
print(len(results)) # 3
print(results[0].std_out) # '/home/yourname/code/project'
print(results[1].return_code) # 0
Shell
can be invoked with a context manager to automatically close out the shell
with Shell() as shell:
... # use shell
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 memshell-0.5.3.tar.gz
.
File metadata
- Download URL: memshell-0.5.3.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.18.1 CPython/3.12.6 Linux/6.10.10-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecf36e6856a8c9b765875b47af33f7e59940df660477b98767a21d8ce99f7165 |
|
MD5 | 78fb78e918d44f4b4954a9c57a9d2554 |
|
BLAKE2b-256 | 461f46adab7d4a8ae951298729c1acdbfcf45c780244134758605048a7de2b88 |
File details
Details for the file memshell-0.5.3-py3-none-any.whl
.
File metadata
- Download URL: memshell-0.5.3-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.18.1 CPython/3.12.6 Linux/6.10.10-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ece8b76f12a8981c87baa817dd085914cddaf762ac2d7acf7662a5d9d3856dde |
|
MD5 | 51cc1d0a23cc2b4d837b3f387d4c1e08 |
|
BLAKE2b-256 | a8476783a41e6e9bd10e971a93bad4dc6f456f0454c71ae0357eaaf66e5992ca |