Skip to main content

Handy subprocess runner with pipelines support.

Project description

Piperum

Handy subprocess runner with pipelines support.

  • no external dependencies
  • supports timeouts
  • do not use sh/bash etc as a backend
  • deals with control terminal and process groups: subprocesses are able to securely read user input (like sudo does)

Usage

from piperum import piperum as pr

Subprocess run variants

# Run single process in foreground
## equivalent to sh: '/usr/bin/ls -1'
pr.run("/usr/bin/ls -1")  # raises subprocess.CalledProcessError if returncode != 0

## run with timeout for waiting process completion
## valid for [run, cap]
pr.run("sleep 10", timeout=5)  # raises subprocess.TimeoutExpired

# Run pipelined processes in foreground
## equivalent to sh: '/usr/bin/ls -1 | /usr/bin/grep tmp'
pr.run("/usr/bin/ls -1", "/usr/bin/grep tmp")

# Run pipelined processes in foreground and capture its output
## equivalent to sh: '$(/usr/bin/ls -1 | /usr/bin/grep tmp)'
some_var = pr.cap("/usr/bin/ls -1", "/usr/bin/grep tmp")

# Run pipelined processes in backgtound
## equivalent to sh: '/usr/bin/curl http://example.com/somearch.tgz -O - | /usr/bin/tar -xzf - &'
bgtask = pr.run_bg("/usr/bin/curl http://example.com/somearch.tgz -O -", "/usr/bin/tar -xzf -")
bgtask.pid                 # background process (or process group if pipeline) pid
bgtask.is_alive            # True if process is still running, otherwise - False
bgtask.returncode          # returncode if process is finished, otherwise - None
bgtasl.wait(timeout=None)  # blocking wait until process finish, optional timeout
bgtask.kill(signal=os.SIGKILL)  # send signal to procees

Standard streams redirection

# Read stdin

## valid for [run, cap, run_bg]
## equivalent to sh: 'cat /some_path/somefile.sql | /usr/bin/psql -d domedb'
pr.run("/usr/bin/psql -d somedb", inpfl="/some_path/somefile.sql")

## valid for [run, cap]
## equivalent to sh: 'echo "SELECT * FROM table;" | /usr/bin/psql -d domedb'
pr.run("/usr/bin/psql -d somedb", inptxt="SELECT * FROM table;")


# Redirect stdout
 
## valid for [run, run_bg]
## equivalent to sh: '/usr/bin/ls -1 > /some_path/filename.txt'
pr.run("/usr/bin/ls -1", outfl="/some_path/filename.txt")

## valid for [run, run_bg]
## equivalent to sh: '/usr/bin/ls -1 >> /some_path/filename.txt'
pr.run("/usr/bin/ls -1", outfl="+/some_path/filename.txt")


# Redirect stderr

## [valid](valid) for [run, cap, run_bg]
## equivalent to sh: '/usr/bin/ls -1 2>/some_path/filename.txt'
pr.run("/usr/bin/ls -1", errfl="/some_path/filename.txt")

## equivalent to sh: '/usr/bin/ls -1 2>>/some_path/filename.txt'
pr.run("/usr/bin/ls -1", errfl="+/some_path/filename.txt")

## valid for [run, cap, run_bg]
## equivalent to sh: '/usr/bin/ls -1 2>&1'
pr.run("/usr/bin/ls -1", err2out=True)

Change process environment

# Working directory
## equivalent to sh: 'pushd /somepath; /usr/bin/pwd; popd'
pr(cwd="/somepath").run("/usr/bin/pwd")

# Extra environment variables
## equivalent to sh: 'PGPASSWD="secret" /usr/bin/psql -d somedb'
pr(PGPASSWD="secret").run("/usr/bin/psql -d somedb")

# You're able to assign it to variable for reuse
pr2 = pr(cwd="../somepath", PGPASSWD="secret")
pr2.run("/usr/bin/psql -d somedb")
pr2.run("/usr/bin/pwd")

# or
with pr(cwd="../somepath", PGPASSWD="secret") as pt2:
    pr2.run("/usr/bin/psql -d somedb")
    pr2.run("/usr/bin/pwd")

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

piperum-0.0.8.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

piperum-0.0.8-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file piperum-0.0.8.tar.gz.

File metadata

  • Download URL: piperum-0.0.8.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for piperum-0.0.8.tar.gz
Algorithm Hash digest
SHA256 8025a273ea3d15a6a5eec81e0990d50ec6b74f924ea87219561db9f1b00968cc
MD5 67e85ad30a15b25b1f5d16aaf422abc2
BLAKE2b-256 8d817b5e7f37c02d8331ee346479f4379142fd82a484705e23f49d26a7a125ff

See more details on using hashes here.

File details

Details for the file piperum-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: piperum-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for piperum-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 300ecf268e53a0105c3f4269b4125f91dea367cb9477747acec9abcc25571990
MD5 671b9084d6373c942cd2569d87b176d8
BLAKE2b-256 e2275d036d1e6fcd8ab2e55615258f76d59c26142d15ee7693f0018330b90e90

See more details on using hashes here.

Supported by

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