@contextlib.contextmanager<EOL>def cmd_as_file(cmd, *args, **kwargs):
kwargs['<STR_LIT>'] = subprocess.PIPE<EOL>stdin = kwargs.pop('<STR_LIT>', None)<EOL>if isinstance(stdin, basestring):<EOL><INDENT>with tempfile.TemporaryFile() as stdin_file:<EOL><INDENT>stdin_file.write(stdin)<EOL>stdin_file.seek(<NUM_LIT:0>)<EOL>kwargs['<STR_LIT>'] = stdin_file<EOL>p = subprocess.Popen(cmd, *args, **kwargs)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>p = subprocess.Popen(cmd, *args, **kwargs)<EOL><DEDENT>try:<EOL><INDENT>yield p.stdout<EOL><DEDENT>finally:<EOL><INDENT>p.stdout.close()<EOL>if p.wait():<EOL><INDENT>raise subprocess.CalledProcessError(p.returncode, cmd)<EOL><DEDENT><DEDENT>
Launch `cmd` and treat its stdout as a file object