cros_build_lib: Fix UncompressFile to cope with xz_auto

In crrev.com/c/2383130, a new script xz_auto was introduced. We
have to change UncompressFile() to deal with this new script so
it passes the right argument when calling the script.

BUG=chromium:1127418
TEST=Test with afdo verifier locally, which uses the function.
TEST=xz_auto -dc file.xz > file works (case for the function call)
TEST=xz_auto -d -i file.xz -o file
TEST=xz_auto -dc file.xz -o file fails on argparse
TEST=verified pixz and xz both works
TEST=xz_auto -c file > file.xz

Change-Id: Ic9bd34bdee5f9cfdf066f4bf28f464dc884e9e84
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2406377
Tested-by: Tiancong Wang <tcwang@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Tiancong Wang <tcwang@google.com>
Auto-Submit: Tiancong Wang <tcwang@google.com>
diff --git a/lib/cros_build_lib.py b/lib/cros_build_lib.py
index 40e70aa..5038ee6 100644
--- a/lib/cros_build_lib.py
+++ b/lib/cros_build_lib.py
@@ -1162,14 +1162,7 @@
   comp_type = CompressionExtToType(outfile)
   assert comp_type and comp_type != COMP_NONE
   comp = FindCompressor(comp_type)
-  if os.path.basename(comp) == 'pixz':
-    # pixz does not accept '-c'; instead an explicit '-i' indicates input file
-    # should not be deleted, and '-o' specifies output file.
-    cmd = [comp, '-i', infile, '-o', outfile]
-    run(cmd)
-  else:
-    cmd = [comp, '-c', infile]
-    run(cmd, stdout=outfile)
+  run([comp, '-c', infile], stdout=outfile)
 
 
 def UncompressFile(infile, outfile):
@@ -1183,14 +1176,7 @@
   comp_type = CompressionExtToType(infile)
   assert comp_type and comp_type != COMP_NONE
   comp = FindCompressor(comp_type)
-  if os.path.basename(comp) == 'pixz':
-    # pixz does not accept '-c'; instead an explicit '-i' indicates input file
-    # should not be deleted, and '-o' specifies output file.
-    cmd = [comp, '-d', '-i', infile, '-o', outfile]
-    run(cmd)
-  else:
-    cmd = [comp, '-dc', infile]
-    run(cmd, stdout=outfile)
+  run([comp, '-dc', infile], stdout=outfile)
 
 
 class CreateTarballError(RunCommandError):
diff --git a/scripts/xz_auto.py b/scripts/xz_auto.py
index c2b299b..8bc65e6 100644
--- a/scripts/xz_auto.py
+++ b/scripts/xz_auto.py
@@ -16,7 +16,6 @@
 from chromite.lib import osutils
 from chromite.utils import memoize
 
-
 assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
 
 
@@ -36,10 +35,17 @@
   return str(int(max(1, multiprocessing.cpu_count() / 2)))
 
 
-def GetDecompressCommand():
+def GetDecompressCommand(stdout):
   """Returns decompression command."""
   if HasPixz():
-    return ['pixz', '-d', '-p', GetJobCount()]
+    cmd = ['pixz', '-d', '-p', GetJobCount()]
+    if stdout:
+      # Explicitly tell pixz the file is the input, so it will dump the output
+      # to stdout, instead of automatically choosing an output name.
+      cmd.append('-i')
+    return cmd
+  if stdout:
+    return ['xz', '-dc']
   return ['xz', '-d']
 
 
@@ -47,17 +53,31 @@
   """Return a command line parser."""
   parser = commandline.ArgumentParser(description=__doc__)
   parser.add_argument(
-      '-d', '--decompress', '--uncompress',
+      '-d',
+      '--decompress',
+      '--uncompress',
       help='Decompress rather than compress.',
       action='store_true')
+  parser.add_argument(
+      '-c',
+      dest='stdout',
+      action='store_true',
+      help="Write to standard output and don't delete input files.")
   return parser
 
 
 def main(argv):
   parser = GetParser()
   known_args, argv = parser.parse_known_args()
+  if '-i' in argv or '-o' in argv:
+    parser.error('It is invalid to use -i or -o with xz_auto')
+
   # xz doesn't support multi-threaded decompression, so try using pixz for that.
   if known_args.decompress:
-    args = GetDecompressCommand()
+    args = GetDecompressCommand(known_args.stdout)
     os.execvp(args[0], args + argv)
-  os.execvp('xz', ['xz', '-T0'] + argv)
+  else:
+    cmd = ['xz', '-T0']
+    if known_args.stdout:
+      cmd.append('-c')
+    os.execvp(cmd[0], cmd + argv)