I am trying to run git clone
command using subprocess.check_output()
so that I can verify if it clones successfully or not, but its throwing error.
Command 'git clone <url>' returned non-zero exit status 128
I am doing:
resp = subprocess.check_output('git clone <url>', shell=True)
How can I get the output of git clone so that I can verify it worked fine or not and can catch any errors if any.
Thanks
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With