Add deapexer info --print-type

This option will just print the type of an apex (COMPRESSED or
UNCOMPRESSED). It will be used by signing tool to sign compressed
apexes.

Bug: 172912232
Test: atest --host apex_compression_test
Change-Id: I5f34012e2ee74f2913ae3a6d66537d8f7a7898c8
diff --git a/tools/apex_compression_test.py b/tools/apex_compression_test.py
index 428c181..e87b85b 100644
--- a/tools/apex_compression_test.py
+++ b/tools/apex_compression_test.py
@@ -206,6 +206,10 @@
     self._to_cleanup.append(decompressed_apex_fp)
     return decompressed_apex_fp
 
+  def _get_type(self, apex_file_path):
+    cmd = ['deapexer', 'info', '--print-type', apex_file_path]
+    return run_host_command(cmd, True).strip()
+
   def test_compression(self):
     uncompressed_apex_fp = os.path.join(get_current_dir(), TEST_APEX + '.apex')
     # TODO(samiul): try compressing a compressed APEX
@@ -218,6 +222,9 @@
     self.assertLess(compressed_file_size, uncompressed_file_size,
                     'Compressed APEX is not smaller than uncompressed APEX')
 
+    # Verify type of the apex is 'COMPRESSED'
+    self.assertEqual(self._get_type(compressed_apex_fp), 'COMPRESSED')
+
     # Verify the contents of the compressed apex files
     content_in_compressed_apex = self._get_container_files(compressed_apex_fp)
     self.assertIsNotNone(content_in_compressed_apex['original_apex'])
@@ -239,6 +246,9 @@
     # Decompress it
     decompressed_apex_fp = self._decompress_apex(compressed_apex_fp)
 
+    # Verify type of the apex is 'UNCOMPRESSED'
+    self.assertEqual(self._get_type(decompressed_apex_fp), 'UNCOMPRESSED')
+
     # Verify decompressed APEX is same as uncompressed APEX
     self.assertEqual(get_sha1sum(uncompressed_apex_fp),
                      get_sha1sum(decompressed_apex_fp),
diff --git a/tools/deapexer.py b/tools/deapexer.py
index 49c00be..f98b4af 100644
--- a/tools/deapexer.py
+++ b/tools/deapexer.py
@@ -24,13 +24,14 @@
 from __future__ import print_function
 
 import argparse
+import apex_manifest
+import enum
 import os
 import shutil
 import sys
 import subprocess
 import tempfile
 import zipfile
-import apex_manifest
 
 BLOCK_SIZE = 4096
 
@@ -235,9 +236,36 @@
     shutil.rmtree(os.path.join(args.dest, "lost+found"))
 
 
+class ApexType(enum.Enum):
+  INVALID = 0
+  UNCOMPRESSED = 1
+  COMPRESSED = 2
+
+
+def GetType(apex_path):
+  with zipfile.ZipFile(apex_path, 'r') as zip_file:
+    names = zip_file.namelist()
+    has_payload = 'apex_payload.img' in names
+    has_original_apex = 'original_apex' in names
+    if has_payload and has_original_apex:
+      return ApexType.INVALID
+    if has_payload:
+      return ApexType.UNCOMPRESSED
+    if has_original_apex:
+      return ApexType.COMPRESSED
+    return ApexType.INVALID
+
+
 def RunInfo(args):
-  manifest = apex_manifest.fromApex(args.apex)
-  print(apex_manifest.toJsonString(manifest))
+  if args.print_type:
+    res = GetType(args.apex)
+    if res == ApexType.INVALID:
+      print(args.apex + ' is not a valid apex')
+      sys.exit(1)
+    print(res.name)
+  else:
+    manifest = apex_manifest.fromApex(args.apex)
+    print(apex_manifest.toJsonString(manifest))
 
 
 def RunDecompress(args):
@@ -297,6 +325,9 @@
 
   parser_info = subparsers.add_parser('info', help='prints APEX manifest')
   parser_info.add_argument('apex', type=str, help='APEX file')
+  parser_info.add_argument('--print-type',
+                           help='Prints type of the apex (COMPRESSED or UNCOMPRESSED)',
+                           action='store_true')
   parser_info.set_defaults(func=RunInfo)
 
   # Handle sub-command "decompress"