So the best method to read a subprocess' output is to use subprocess.PIPE
. For example
import subprocess
from collections import namedtuple
def git_clone(url):
process = subprocess.Popen(['git', 'clone', url], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return namedtuple('Std', 'out, err')(process.stdout.read(), process.stderr.read())
# test on fake url
out, err = git_clone('http://fake.url')
print('out = {}\nerr = {}'.format(out, err)
outputs:
out = b''
err = b"Cloning into 'fake.url'...\nfatal: unable to access 'http://fake.url/': Couldn't resolve host 'fake.url'\n"
Hence you can test success by changing the function to be
from warnings import warn
def git_clone(url):
process = subprocess.Popen(['git', 'clone', url], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if not process.stdout.read():
warn(process.stderr.read())
return False
return True