Created: 2011-07-13 05:16
Updated: 2013-10-10 16:03
License: bsd-3-clause


A collection of shell utilities/helpers for Python.


import sys
import shellshock.runner

class MyRunner(shellshock.runner.Runner):
    __optspec__ = [
        { 'args': ['-f', '--file'], 'dest':'filename' },
        { 'args': ['-n', '--dry-run'], 'dest':'dryrun', 'action': 'store_true' },

    def run(self):
        if self.options.dryrun:
            print "Dry Run!"
        print "File: %s" % (self.options.file)


Automatically creates an option parser (at self._optparser) using optparse, and converts __optspec__ into self.options and self.args. The original args (prior parsing) are stored in self._orig_args. Parses sys.argv by default, if no args are passed into the class. If you want to send no args, then do something like this:


Basically passes the each dict directly to a parser.add_option() call as **kwargs, with the 'args' key-value pair stripped and the value is passed through to add_option() as *args. Therefore:

__optspec__ = [
    { 'args': ['-f','--file'], 'dest':'filename' }

is equivalent to:

parser.add_option('-f', '--file', dest='filename')


import shellshock.tee

tee = shellshock.tee.Tee(files=['test.log', 'test.log.1'],
print >> tee, "My Message"

print >> tee, "My Message 2"


The purpose of Tee is to allow the unix 'tee' utility to be easily emulated within Python. It creates a file-like object that allows the user to write to it, and that data will be written to all associated files, as well as (optionally) stderr and/or stdout.

Apparently I can't claim that this is a file-like object without a read() method, but this is a write-only object, so I don't see the need to define it. Personally, I view calling the read() method on a file that was open for writing only as an undefined operation.


Brandon Sandrowicz


BSD 3-clause license. See LICENSE file for terms.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more