I have a program written in python and used git command in it..
For some reason I don't want to use git-python or others instead of subprocess.
But I'm currently stuck in getting git clone
output.
I've tried some code snippet. Some works fine with commands like ping 8.8.8.8
, but not the git clone
.
for example
using thread
def log_worker(stdout):
while True:
last = non_block_read(stdout).strip()
if last != "":
print(last)
def non_block_read(output):
fd = output.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
try:
return output.read()
except:
return ''
def test():
mysql_process = subprocess.Popen(
"ping google.com",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
thread = Thread(target=log_worker, args=[mysql_process.stdout])
thread.daemon = True
thread.start()
mysql_process.wait()
thread.join(timeout=1)
test()
or
newlines = ['\n', '\r\n', '\r']
def unbuffered(proc, stream='stdout'):
stream = getattr(proc, stream)
with contextlib.closing(stream):
while True:
print('tt')
out = []
last = stream.read(1)
# Don't loop forever
if last == '' and proc.poll() is not None:
break
print('last', last)
while last not in newlines:
print("loop")
# Don't loop forever
if last == '' and proc.poll() is not None:
break
out.append(last)
last = stream.read(1)
out = ''.join(out)
yield out
def example():
cmd = ['ls', '-l', '/']
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
# Make all end-of-lines '\n'
universal_newlines=True,
shell = True
)
for line in unbuffered(proc):
print('new line')
print line
example()
and most common one
for line in iter(proc.stdout.readline, ''):
sys.stdout.write('{:.2f} {}\n'.format(
time.time() - start,
line.rstrip()
))
sys.stdout.flush()
all of them works fine with ping google.com
, but not git clone
.
Is there any way to solve this?
Thanks in advance!
UPDATE1:
In face, I'm just want to get the finished percent of git clone
. Log or any log files are not needed.