Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

memshell-0.4.1.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

memshell-0.4.1-py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page