Let’s see how iterpipes works using a series of examples. All the module functions used here are described in the API documentation in detail.
Get the iterable of files in the / directory:
>>> from iterpipes import linecmd, run
>>> files = run(linecmd('ls {}', '/'))
>>> list(files)[:3]
[u'bin\n', u'boot\n', u'dev\n']
Total lines in *.py files under the . directory, use safe shell escaping for parameters:
>>> from iterpipes import cmd
>>> total = cmd('find {} -name {} -print0 | xargs -0 wc -l | tail -1 | awk {}',
... '.',
... '\*.py',
... '{print $1}')
>>> int(''.join(run(total)).strip())
616
Pipe 100 000 lines through wc -l, join the resulting iterable into a single string and convert it to an int:
>>> wc = lambda xs: int(''.join(cmd('wc -l')(xs)).strip())
>>> numbers = ('%d\n' % i for i in xrange(100000))
>>> wc(numbers)
100000
Delete /tmp/foo/bar and all the files under it, get the return code or check for exceptions:
>>> from iterpipes import call, check_call
>>> call(cmd('rm -fr {}', '/tmp/foo/bar'))
0
>>> check_call(cmd('rm -fr {}', '/tmp/foo/bar'))
Load an Atom feed of the iterpipes source code repository using curl:
>>> from iterpipes import bincmd
>>> from xml.etree import ElementTree as etree
>>> url = 'http://bitbucket.org/vlasovskikh/iterpipes/atom/'
>>> e = etree.fromstring(''.join(run(bincmd('curl -s {}', url))))
>>> e.tag
'{http://www.w3.org/2005/Atom}feed'
It is useful to abstract the execution of a command using a Python function. For example, you may find yourself writing several lines of code for creating tarball archives of directories. You can hide details of creating tarballs by defining the following function:
def make_zipped_tarball(dirname, output_path='.'):
name = os.path.basename(os.path.normpath(dirname))
tar = cmd('tar -czf {} {}',
os.path.join(output_path, '%s.tar.gz' % name),
dirname)
check_call(tar)