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>
diff --git a/progress.py b/progress.py
index 69c9592..f2edf14 100644
--- a/progress.py
+++ b/progress.py
@@ -23,7 +23,7 @@
 
 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 @@
         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 @@
     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 @@
             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 @@
 
     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)