summary refs log tree commit diff stats
path: root/ranger/ext/spawn.py
blob: b07dd2e08824de7d7cac0a12bc517dea7ff60d99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# This file is part of ranger, the console file manager.
# License: GNU GPL version 3, see the file "AUTHORS" for details.

from subprocess import Popen, PIPE, CalledProcessError
ENCODING = 'utf-8'


def spawn(*args):
    """Runs a program, waits for its termination and returns its stdout"""
    if len(args) == 1:
        popen_arguments = args[0]
        shell = isinstance(popen_arguments, str)
    else:
        popen_arguments = args
        shell = False
    process = Popen(popen_arguments, stdout=PIPE, shell=shell)
    stdout, stderr = process.communicate()
    return_value = process.poll()
    if return_value:
        error = CalledProcessError(return_value, popen_arguments[0])
        error.output = stdout
        raise error

    return stdout.decode(ENCODING)