Simplifications to art_apex_test.py am: af3ed62955 am: a1292f25bb
am: 5ef9f4fc97

Change-Id: Idf797c33ab7c051e9813530733c7a77c9b7e37b7
diff --git a/build/apex/art_apex_test.py b/build/apex/art_apex_test.py
index e7f3e00..3a5dfee 100755
--- a/build/apex/art_apex_test.py
+++ b/build/apex/art_apex_test.py
@@ -27,14 +27,29 @@
 
 logging.basicConfig(format='%(message)s')
 
+# Flavors of ART APEX package.
+FLAVOR_RELEASE = 'release'
+FLAVOR_DEBUG = 'debug'
+FLAVOR_TESTING = 'testing'
+FLAVOR_AUTO = 'auto'
+FLAVORS_ALL = [FLAVOR_RELEASE, FLAVOR_DEBUG, FLAVOR_TESTING, FLAVOR_AUTO]
+
+# Bitness options for APEX package
+BITNESS_32 = '32'
+BITNESS_64 = '64'
+BITNESS_MULTILIB = 'multilib'
+BITNESS_AUTO = 'auto'
+BITNESS_ALL = [BITNESS_32, BITNESS_64, BITNESS_MULTILIB, BITNESS_AUTO]
+
 # Architectures supported by APEX packages.
-archs = ["arm", "arm64", "x86", "x86_64"]
+ARCHS = ["arm", "arm64", "x86", "x86_64"]
+
 # Directory containing ART tests within an ART APEX (if the package includes
 # any). ART test executables are installed in `bin/art/<arch>`. Segregating
 # tests by architecture is useful on devices supporting more than one
 # architecture, as it permits testing all of them using a single ART APEX
 # package.
-art_test_dir = 'bin/art'
+ART_TEST_DIR = 'bin/art'
 
 class FSObject:
   def __init__(self, name, is_dir, is_exec, is_symlink, size):
@@ -290,8 +305,8 @@
     # TODO: Implement the suggestion above (here and in other places in this
     # script).
     test_found = False
-    for arch in archs:
-      test_path = '%s/%s/%s' % (art_test_dir, arch, filename)
+    for arch in ARCHS:
+      test_path = '%s/%s/%s' % (ART_TEST_DIR, arch, filename)
       test_is_file, _ = self.is_file(test_path)
       if test_is_file:
         test_found = True
@@ -320,8 +335,8 @@
     self._expected_file_globs.add(path_glob)
 
   def check_optional_art_test_executable(self, filename):
-    for arch in archs:
-      self.ignore_path('%s/%s/%s' % (art_test_dir, arch, filename))
+    for arch in ARCHS:
+      self.ignore_path('%s/%s/%s' % (ART_TEST_DIR, arch, filename))
 
   def check_no_superfluous_files(self, dir_path):
     paths = []
@@ -940,8 +955,8 @@
     return 'No superfluous ART tests checker'
 
   def run(self):
-    for arch in archs:
-      self._checker.check_no_superfluous_files('%s/%s' % (art_test_dir, arch))
+    for arch in ARCHS:
+      self._checker.check_no_superfluous_files('%s/%s' % (ART_TEST_DIR, arch))
 
 
 class List:
@@ -1069,8 +1084,6 @@
   if not test_args.flattened and not test_args.host and not test_args.debugfs:
     logging.error("Need debugfs.")
     return 1
-  if test_args.bitness not in ['32', '64', 'multilib', 'auto']:
-    logging.error('--bitness needs to be one of 32|64|multilib|auto')
 
   try:
     if test_args.host:
@@ -1094,52 +1107,47 @@
   # Handle legacy flavor flags.
   if test_args.debug:
     logging.warning('Using deprecated option --debug')
-    test_args.flavor='debug'
+    test_args.flavor = FLAVOR_DEBUG
   if test_args.testing:
     logging.warning('Using deprecated option --testing')
-    test_args.flavor='testing'
-  if test_args.flavor == 'auto':
+    test_args.flavor = FLAVOR_TESTING
+  if test_args.flavor == FLAVOR_AUTO:
     logging.warning('--flavor=auto, trying to autodetect. This may be incorrect!')
-    if fnmatch.fnmatch(test_args.apex, '*.release*'):
-      logging.warning('  Detected Release APEX')
-      test_args.flavor='release'
-    elif fnmatch.fnmatch(test_args.apex, '*.debug*'):
-      logging.warning('  Detected Debug APEX')
-      test_args.flavor='debug'
-    elif fnmatch.fnmatch(test_args.apex, '*.testing*'):
-      logging.warning('  Detected Testing APEX')
-      test_args.flavor='testing'
-    else:
-      logging.error('  Could not detect APEX flavor, neither \'release\', \'debug\' nor ' +
-                    '\'testing\' in \'%s\'',
-          test_args.apex)
+    for flavor in [ FLAVOR_RELEASE, FLAVOR_DEBUG, FLAVOR_TESTING ]:
+      flavor_pattern = '*.%s*' % flavor
+      if fnmatch.fnmatch(test_args.apex, flavor_pattern):
+        test_args.flavor = flavor
+        break
+    if test_args.flavor == FLAVOR_AUTO:
+      logging.error('  Could not detect APEX flavor, neither \'%s\', \'%s\' nor \'%s\' in \'%s\'',
+                  FLAVOR_RELEASE, FLAVOR_DEBUG, FLAVOR_TESTING, test_args.apex)
       return 1
 
   checkers = []
-  if test_args.bitness == 'auto':
+  if test_args.bitness == BITNESS_AUTO:
     logging.warning('--bitness=auto, trying to autodetect. This may be incorrect!')
     has_32 = apex_provider.get('lib') is not None
     has_64 = apex_provider.get('lib64') is not None
     if has_32 and has_64:
       logging.warning('  Detected multilib')
-      test_args.bitness = 'multilib'
+      test_args.bitness = BITNESS_MULTILIB
     elif has_32:
       logging.warning('  Detected 32-only')
-      test_args.bitness = '32'
+      test_args.bitness = BITNESS_32
     elif has_64:
       logging.warning('  Detected 64-only')
-      test_args.bitness = '64'
+      test_args.bitness = BITNESS_64
     else:
       logging.error('  Could not detect bitness, neither lib nor lib64 contained.')
       List(apex_provider).print_list()
       return 1
 
-  if test_args.bitness == '32':
+  if test_args.bitness == BITNESS_32:
     base_checker = Arch32Checker(apex_provider)
-  elif test_args.bitness == '64':
+  elif test_args.bitness == BITNESS_64:
     base_checker = Arch64Checker(apex_provider)
   else:
-    assert test_args.bitness == 'multilib'
+    assert test_args.bitness == BITNESS_MULTILIB
     base_checker = MultilibChecker(apex_provider)
 
   checkers.append(ReleaseChecker(base_checker))
@@ -1147,11 +1155,11 @@
     checkers.append(ReleaseHostChecker(base_checker))
   else:
     checkers.append(ReleaseTargetChecker(base_checker))
-  if test_args.flavor == 'debug' or test_args.flavor == 'testing':
+  if test_args.flavor == FLAVOR_DEBUG or test_args.flavor == FLAVOR_TESTING:
     checkers.append(DebugChecker(base_checker))
     if not test_args.host:
       checkers.append(DebugTargetChecker(base_checker))
-  if test_args.flavor == 'testing':
+  if test_args.flavor == FLAVOR_TESTING:
     checkers.append(TestingTargetChecker(base_checker))
 
   # These checkers must be last.
@@ -1191,7 +1199,7 @@
   test_args.tmpdir = '.'
   test_args.tree = False
   test_args.list = False
-  test_args.bitness = 'auto'
+  test_args.bitness = BITNESS_AUTO
   failed = False
 
   if not os.path.exists(test_args.debugfs):
@@ -1202,9 +1210,9 @@
   # TODO: Add host support.
   # TODO: Add support for flattened APEX packages.
   configs = [
-    {'name': 'com.android.art.release', 'flavor': 'release', 'host': False},
-    {'name': 'com.android.art.debug',   'flavor': 'debug',   'host': False},
-    {'name': 'com.android.art.testing', 'flavor': 'testing', 'host': False},
+    {'name': 'com.android.art.release', 'flavor': FLAVOR_RELEASE, 'host': False},
+    {'name': 'com.android.art.debug',   'flavor': FLAVOR_DEBUG,   'host': False},
+    {'name': 'com.android.art.testing', 'flavor': FLAVOR_TESTING, 'host': False},
   ]
 
   for config in configs:
@@ -1232,8 +1240,8 @@
 
   parser.add_argument('--flattened', help='Check as flattened (target) APEX', action='store_true')
 
-  parser.add_argument('--flavor', help='Check as FLAVOR APEX, release|debug|testing|auto',
-                      default='auto')
+  parser.add_argument('--flavor', help='Check as FLAVOR APEX', choices=FLAVORS_ALL,
+                      default=FLAVOR_AUTO)
   # Deprecated flavor flags.
   # TODO: Stop supporting those flags eventually.
   parser.add_argument('--debug', help='Check as debug APEX', action='store_true')
@@ -1246,7 +1254,8 @@
   parser.add_argument('--tmpdir', help='Directory for temp files')
   parser.add_argument('--debugfs', help='Path to debugfs')
 
-  parser.add_argument('--bitness', help='Bitness to check, 32|64|multilib|auto', default='auto')
+  parser.add_argument('--bitness', help='Bitness to check', choices=BITNESS_ALL,
+                      default=BITNESS_AUTO)
 
   if len(sys.argv) == 1:
     art_apex_test_default(parser)