sync: Handle case when output isn't connected to a terminal

Currently `repo sync | tee` exits with an OSError.

Bug: https://crbug.com/gerrit/17023
Change-Id: I91ae05f1c91d374b5d57721d45af74db1b2072a5
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/376414
Tested-by: Gavin Mak <gavinmak@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
Commit-Queue: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Gavin Mak 2023-06-07 21:59:17 +00:00 committed by LUCI
parent 945c006f40
commit b2263ba124

View File

@ -23,7 +23,7 @@ except ImportError:
from repo_trace import IsTraceToStderr from repo_trace import IsTraceToStderr
_NOT_TTY = not os.isatty(2) _TTY = sys.stderr.isatty()
# This will erase all content in the current line (wherever the cursor is). # This will erase all content in the current line (wherever the cursor is).
# It does not move the cursor, so this is usually followed by \r to move to # It does not move the cursor, so this is usually followed by \r to move to
@ -97,7 +97,8 @@ class Progress(object):
self._start = time.time() self._start = time.time()
self._show = not delay self._show = not delay
self._units = units self._units = units
self._elide = elide self._elide = elide and _TTY
# Only show the active jobs section if we run more than one in parallel. # Only show the active jobs section if we run more than one in parallel.
self._show_jobs = False self._show_jobs = False
self._active = 0 self._active = 0
@ -129,7 +130,7 @@ class Progress(object):
def _write(self, s): def _write(self, s):
s = "\r" + s s = "\r" + s
if self._elide: if self._elide:
col = os.get_terminal_size().columns col = os.get_terminal_size(sys.stderr.fileno()).columns
if len(s) > col: if len(s) > col:
s = s[: col - 1] + ".." s = s[: col - 1] + ".."
sys.stderr.write(s) sys.stderr.write(s)
@ -157,7 +158,7 @@ class Progress(object):
msg = self._last_msg msg = self._last_msg
self._last_msg = msg self._last_msg = msg
if _NOT_TTY or IsTraceToStderr(): if not _TTY or IsTraceToStderr():
return return
elapsed_sec = time.time() - self._start elapsed_sec = time.time() - self._start
@ -199,7 +200,7 @@ class Progress(object):
def end(self): def end(self):
self._update_event.set() self._update_event.set()
if _NOT_TTY or IsTraceToStderr() or not self._show: if not _TTY or IsTraceToStderr() or not self._show:
return return
duration = duration_str(time.time() - self._start) duration = duration_str(time.time() - self._start)