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.5.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

memshell-0.5.3-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

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

Hashes for memshell-0.5.3.tar.gz
Algorithm Hash digest
SHA256 ecf36e6856a8c9b765875b47af33f7e59940df660477b98767a21d8ce99f7165
MD5 78fb78e918d44f4b4954a9c57a9d2554
BLAKE2b-256 461f46adab7d4a8ae951298729c1acdbfcf45c780244134758605048a7de2b88

See more details on using hashes here.

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

Hashes for memshell-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ece8b76f12a8981c87baa817dd085914cddaf762ac2d7acf7662a5d9d3856dde
MD5 51cc1d0a23cc2b4d837b3f387d4c1e08
BLAKE2b-256 a8476783a41e6e9bd10e971a93bad4dc6f456f0454c71ae0357eaaf66e5992ca

See more details on using hashes here.

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