gist: 37630 Download_button fork
public
Public Clone URL: git://gist.github.com/37630.git
pipedarg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
#
# pipedarg
# by Keith Gaughan <http://talideon.com/>
#
# The software is hereby placed in the public domain, and the author
# disclaims ownership over it and all responsibility for any damage caused
# directly or indirectly through its use. It may be freely copied and/or
# mangled, provided that altered versions have a different name and are
# not attributed to the original author.
#
 
"""
Copies standard input to a temporary file and executes the command specified
in the arguments with the name of the temporary file as the last argument.
 
Obviously this won't work if the executed process forks a child to do its
work and exits immediately.
"""
 
from __future__ import with_statement
import sys
import os
import tempfile
 
with tempfile.NamedTemporaryFile() as tf:
    tf.write(sys.stdin.read())
    tf.flush()
    args = sys.argv[1:]
    os.spawnvp(os.P_WAIT, args[0], args + [tf.name])

Owner

kgaughan

Revisions