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
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 memshell-0.5.4.tar.gz.
File metadata
- Download URL: memshell-0.5.4.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f6643eee5f931d238756dc17cd5e00bdb01127faf4d5c706cb33d98d0b2fabb
|
|
| MD5 |
ad75a097aae636f5faa5ebe374af057f
|
|
| BLAKE2b-256 |
d957ecfe75795a7e5b0663f5f1c581c0c26f04ac64a26f0f033fcf7e694ebfbb
|
File details
Details for the file memshell-0.5.4-py3-none-any.whl.
File metadata
- Download URL: memshell-0.5.4-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21a94c0d2b569ad4b3ed8c125dc63a9cc64d1e8e9717a3ad25721a15af955793
|
|
| MD5 |
003968f373188c4292bcc136c2b13b38
|
|
| BLAKE2b-256 |
fec2be0a541011c7e0794f2e3249e2a08d41164a97bd4273bd9aa98e3dc6f4b0
|