scripts: gerrit: Make --raw automatic when not attached to a TTY

A common usage of the tool is to nest a call to gerrit in a process
substitution, like so:

    gerrit label-cq $(gerrit --raw search "some-search-query") 2

If --raw is forgotten, the gerrit command will behave oddly and shoot
off lots of errors, as it doesn't know what to do with most of the
output of the gerrit command.

When the output is not a terminal, it's extremely unlikely the user is
expecting output meant to be viewed on a terminal.  Therefore, when
we're not attached to a terminal, just default to --raw.  Pretty
output can be forced even when not attached to a terminal with
--format=pretty.

BUG=none
TEST=gerrit search "some-search-query" # results are pretty
     gerrit search "some-search-query" > file.txt
         # file.txt has raw results
     gerrit --format=pretty search "some-search-query" > file.txt
         # file.txt has "pretty" results

Change-Id: I26b32250020ebe422fb26025282a76f07e7db546
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/3739143
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Jack Rosenthal <jrosenth@chromium.org>
Tested-by: Jack Rosenthal <jrosenth@chromium.org>
diff --git a/scripts/gerrit.py b/scripts/gerrit.py
index f530f49..29b994f 100644
--- a/scripts/gerrit.py
+++ b/scripts/gerrit.py
@@ -113,8 +113,8 @@
   """Type for the requested output format.
 
   AUTO: Automatically determine the format based on what the user
-    might want.  For now, this is just PRETTY, but this behavior is
-    subject to change.
+    might want.  This is PRETTY if attached to a terminal, RAW
+    otherwise.
   RAW: Output CLs one per line, suitable for mild scripting.
   JSON: JSON-encoded output, suitable for spicy scripting.
   MARKDOWN: Suitable for posting in a bug or CL comment.
@@ -1348,7 +1348,10 @@
   opts.gerrit = {}
 
   if opts.format is OutputFormat.AUTO:
-    opts.format = OutputFormat.PRETTY
+    if sys.stdout.isatty():
+      opts.format = OutputFormat.PRETTY
+    else:
+      opts.format = OutputFormat.RAW
 
   opts.Freeze()