From b2263ba1245f12428a63e19ced88f27d92d7ca7a Mon Sep 17 00:00:00 2001 From: Gavin Mak Date: Wed, 7 Jun 2023 21:59:17 +0000 Subject: [PATCH] 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 Reviewed-by: Mike Frysinger Commit-Queue: Gavin Mak --- progress.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/progress.py b/progress.py index 69c95927..f2edf144 100644 --- a/progress.py +++ b/progress.py @@ -23,7 +23,7 @@ except ImportError: 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). # 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._show = not delay 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. self._show_jobs = False self._active = 0 @@ -129,7 +130,7 @@ class Progress(object): def _write(self, s): s = "\r" + s if self._elide: - col = os.get_terminal_size().columns + col = os.get_terminal_size(sys.stderr.fileno()).columns if len(s) > col: s = s[: col - 1] + ".." sys.stderr.write(s) @@ -157,7 +158,7 @@ class Progress(object): msg = self._last_msg self._last_msg = msg - if _NOT_TTY or IsTraceToStderr(): + if not _TTY or IsTraceToStderr(): return elapsed_sec = time.time() - self._start @@ -199,7 +200,7 @@ class Progress(object): def end(self): self._update_event.set() - if _NOT_TTY or IsTraceToStderr() or not self._show: + if not _TTY or IsTraceToStderr() or not self._show: return duration = duration_str(time.time() - self._start)