I've followed a number of questions on SO about capturing subprocess output. using git within subprocess seems to output to error instead of stdout or stderr
This is what I want to replicate from shell.
git add .
git commit -m 'commit message blah'
git push -u origin master
with tracking of output from each stage. In this example, I have no updates to commit.
$git add .
$ git commit -m 'wip'
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
as per code below, I am using
output = run("git add .", check=True, capture_output=True, shell=True)
and checking output.returncode, but experiencing an CalledProcessError instead of values to output. any ideas? I can fudge functionality through error trapping, but it's far from optimal.
I can't capture the "nothing to commit, working tree clean" output from 'git push', this seems weird. as are the values for '' stderr & stdout. returncode is a 0 after 'git commit'.
nb: 'git add .' does not produce any output in shell.
environment : ubuntu app on windows 10. Python3, anaconda 64 bit.
from subprocess import *
import sys
try:
print("start git add.")
#output = run("git add .", check=True, stdout=PIPE, stderr=PIPE, shell=True)
output = run("git add .", check=True, capture_output=True, shell=True)
print("git add completed.")
print("output.stderr:", "'"+output.stderr.decode('utf-8')+"'")
print("output.stdout:", "'"+output.stdout.decode('utf-8')+"'")
print("output.stderr==None:", output.stderr==None)
print("output.stdout==None:", output.stdout==None)
print("output.returncode:", output.returncode)
if output.returncode==0:
output=None
print("start git commit.")
#output = run(f"git commit -m 'commit message three'", check=True, stdout=PIPE, stderr=PIPE, shell=True)
output = run("git commit -m 'commit message three'", check=True, capture_output=True, shell=True)
#exits to exception here when error occurs dueing git commit.
print("git commit completed.")
print("output.stderr:", output.stderr.decode('utf-8'))
print("output.stdout:", output.stdout.decode('utf-8'))
print("output.stderr==None:", output.stderr==None)
print("output.stdout==None:", output.stdout==None)
print("output.returncode:", output.returncode)
if output.returncode==0:
output=None
print("start git push.")
#output = run("git push -u origin master -f", check=True, stdout=PIPE, stderr=PIPE, shell=True)
output = run("git push -u origin master -f", capture_output=True)
print("git push completed.")
print("output.stderr:", output.stderr)
print("output.stdout:", output.stdout)
print("output.stderr==None:", output.stderr==None)
print("output.stdout==None:", output.stdout==None)
print("output.returncode:", output.returncode)
if output.returncode==0:
print("success @ git push, output.returncode==0.")
except CalledProcessError as error:
print("CalledProcessError error caught.")
print('CalledProcessError:An exception occurred: {}'.format(error))
print("CalledProcessError:sys.exc_info()[0]:", sys.exc_info()[0])
print("CalledProcessError:output:", output)
print("CalledProcessError:output.stderr:", output.stderr)
print("CalledProcessError:output.stdout:", output.stdout)
output from this code block
start git add.
git add completed.
output.stderr: ''
output.stdout: ''
output.stderr==None: False
output.stdout==None: False
output.returncode: 0
start git commit.
CalledProcessError error caught.
CalledProcessError:An exception occurred: Command 'git commit -m 'commit message three'' returned non-zero exit status 1.
CalledProcessError:sys.exc_info()[0]: <class 'subprocess.CalledProcessError'>
CalledProcessError:output: None
Traceback (most recent call last):
File "<stdin>", line 15, in <module>
File "/home/bmt/anaconda3/lib/python3.7/subprocess.py", line 487, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'git commit -m 'commit message three'' returned non-zero exit status 1.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 41, in <module>
AttributeError: 'NoneType' object has no attribute 'stderr'
edit: my main error was using 'check=True' and 'shell=True' also changing the shell command from one string to a list of strings.
output = run("git add .", check=True, capture_output=True, shell=True)
to
output = run(["git", "add", "."], capture_output=True)