Skip to content

Instantly share code, notes, and snippets.

@jonmorehouse
Created January 19, 2017 22:17
Show Gist options
  • Save jonmorehouse/e836be0e2a22f05e2d825fe16ed2ec91 to your computer and use it in GitHub Desktop.
Save jonmorehouse/e836be0e2a22f05e2d825fe16ed2ec91 to your computer and use it in GitHub Desktop.
import subprocess
import sys
import select
cmd = 'git clone --progress https://github.com/jonmorehouse/dotfiles foobar'
p = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
reads = [p.stdout.fileno(), p.stderr.fileno()]
ret = select.select(reads, [], [])
for fd in ret[0]:
if fd == p.stdout.fileno():
line = p.stdout.read(24)
sys.stdout.write(line)
if fd == p.stderr.fileno():
# NOTE: we can't do readline, because new lines for _some outputs_ are vertical tab delimited
line = p.stderr.read(24)
# this method does not exist. Unfortunately, the closest we have at
# this level of abstraction is something like read(20). Maybe we
# should use
# https://docs.python.org/2/library/io.html#io.RawIOBase.readall?
# line = p.stderr.readall()
sys.stderr.write(line)
if p.poll() is not None:
break
print p.returncode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment