Skip to main content

A shell-like DSL front for subprocess.Popen

Project description

popen

What is it?

The popen package provides a simple shell-like syntax inside python for running external commands.

Features:

  • Chaining (aka. piping) as Sh('ls') | 'sort'

  • Redirect stderr to stdout as Sh('make').include_stderr | 'wc'

  • Redirect output to file as Sh('ls') > '~/listing.txt'

  • Iteration over output lines as for line in Sh('ls'):

  • Streaming input into commands in the chain as Sh.pipe('~/listing.txt').Sh('grep', '-q', 'code').returncode

  • Expands special characters (~*!?)

  • Expands env vars ($HOME) as print Sh('ls', '$USER', '~/*')

  • Properly splits strings ('echo "hole in one"' becomes ['echo', 'hole in one'])

  • Or iterable arguments (Sh('ls', '-l') | ['wc', '-l'] > '/dev/null')

TL;DR

Installation:

pip install popen

Example:

from popen import Sh
for line in Sh('du', '~/') | 'head -n 10' | 'sort -n':
    print('GOT', line)

Usage

Simples usage, run a command and iterate the output lines:

from popen import Sh
for line in Sh('ls', '-la', '~'):
    print line

Piping that output to a file:

Sh('ls', '-la', '~') > '~/listing'

Note that special characters like ~ are expanded, as are environment variables (e.g. $HOME).

Chaining commands is trivial, here with append instead of write to file:

Sh('ls') | Sh('sort') >> '~/listing'

But the right hand side of | can be very flexible, as it employs lexical splitter on string input or takes an iterable:

Sh('ls', '-la', '~') | 'sort -c' | ['uniq', '-c'] | 'tail' | Sh('wc') > '~/listing'

To run a command and let output go to stdout, ask for the return code:

cmd = Sh('ls') | 'grep polka'
print cmd.returncode

To pipe a string into a command, wrap in an iterable:

print Sh.pipe(['this is a \nmulti line\nstring.']) | 'wc'

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

popen-0.1.17.tar.gz (6.7 kB view details)

Uploaded Source

File details

Details for the file popen-0.1.17.tar.gz.

File metadata

  • Download URL: popen-0.1.17.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for popen-0.1.17.tar.gz
Algorithm Hash digest
SHA256 30d9737c73bc73b25f357db313cbb2327cca801133765cd658ece00c64aa7f3c
MD5 6924147c6423f68445713d927d2ab94b
BLAKE2b-256 3e6d7ef39f90c953261965f5b632529b23a9a8018d631376d504f60f28e9ae52

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