Skip to main content

Continuous shell process

Project description

Install

pip install shell_proc

Run

Run a series of terminal commands.

import sys
from shell_proc import Shell

with Shell(stdout=sys.stdout, stderr=sys.stderr) as sh:
    sh.run('mkdir storage')
    sh('cd storage')  # Same as sh.run()
    sh('python -m venv ./myvenv')
    sh('source ./myvenv/bin/activate')
    sh('pip install requests')
    sh('pip list')

Manually call commands and check results.

import io
import sys
from shell_proc import Shell

# Initialize and run tasks
sh = Shell('mkdir storage',
           'cd storage',
           'python -m venv ./myvenv',
           stderr=io.StringIO())

# Manually run tasks
if sh.is_windows():
    sh.run('call .\\myvenv\\Scripts\\activate.bat')
else:
    sh.run('source ./myvenv/bin/activate')

# Not exactly success. If True no output was printed to stderr. Stderr could also be warning like need to update pip
success = sh('pip install requests')
print("***** Successful install: ", success)
if not success:
    err = sh.get_stderr()  # All text collected into stderr from subprocess stderr
    print(err, file=sys.stderr)
    # sh.print_stderr()  # Also available

sh.stdout = io.StringIO()  # Start saving output for new tasks
sh('pip list')
print('***** Output Printed', sh.has_print_out())
sh.stdout.seek(0)  # Move to start of io.StringIO()
print(sh.stdout.read())  # Print all read data

# Should close when finished to stop threads from reading stdout and stderr subprocess.PIPE
# (will close automatically eventually)
sh.close()

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

shell_proc-1.0.0.tar.gz (6.4 kB view hashes)

Uploaded Source

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