Merge "Refactor compare_images"
am: 2ce9bc6563

Change-Id: Ie2d2249fffc78d4a70de5b7c218ce7df87f0f20b
diff --git a/vndk/tools/image-diff-tool/diff.py b/vndk/tools/image-diff-tool/diff.py
index a623d65..803f093 100644
--- a/vndk/tools/image-diff-tool/diff.py
+++ b/vndk/tools/image-diff-tool/diff.py
@@ -18,7 +18,6 @@
 import sys
 from collections import defaultdict
 from pathlib import Path
-from operator import itemgetter
 import hashlib
 import argparse
 import zipfile
@@ -42,7 +41,9 @@
 
 def strip_and_sha1sum(filepath):
   tmp_filepath = filepath + '.tmp.no-build-id'
-  strip_all_and_remove_build_id = lambda: silent_call(["llvm-strip", "--strip-all", "--keep-section=.ARM.attributes", "--remove-section=.note.gnu.build-id", filepath, "-o", tmp_filepath])
+  strip_all_and_remove_build_id = lambda: silent_call(
+      ["llvm-strip", "--strip-all", "--keep-section=.ARM.attributes",
+       "--remove-section=.note.gnu.build-id", filepath, "-o", tmp_filepath])
   try:
     if strip_all_and_remove_build_id():
       return sha1sum(tmp_filepath)
@@ -58,27 +59,26 @@
 def main(all_targets, search_paths, ignore_signing_key=False):
   def get_target_name(path):
     return os.path.basename(os.path.normpath(path))
+
+  def run(path):
+    is_native_component = silent_call(["llvm-objdump", "-a", path])
+    is_apk = path.endswith('.apk')
+    if is_native_component:
+      return strip_and_sha1sum(path)
+    elif is_apk and ignore_signing_key:
+      return sha1sum_without_signing_key(path)
+    else:
+      return sha1sum(path)
+
   artifact_target_map = defaultdict(list)
   for target in all_targets:
-    def valid_path(p):
-      if os.path.isdir(p) or not os.path.exists(p):
-        return False
-      for s in search_paths:
-        if os.path.join(target, s).lower() + os.path.sep in p.lower():
-          return True
-      return False
-    paths = [str(path) for path in Path(target).glob('**/*') if valid_path(str(path))]
+    paths = []
+    for search_path in search_paths:
+      for path in Path(target, search_path).glob('**/*'):
+        if path.exists() and not path.is_dir():
+          paths.append((str(path), str(path.relative_to(target))))
 
-    def run(path):
-      is_native_component = silent_call(["llvm-objdump", "-a", path])
-      is_apk = path.endswith('.apk')
-      if is_native_component:
-        return strip_and_sha1sum(path), path[len(target):]
-      elif is_apk and ignore_signing_key:
-        return sha1sum_without_signing_key(path), path[len(target):]
-      else:
-        return sha1sum(path), path[len(target):]
-    results = [run(p) for p in paths]
+    results = [(run(path), filename) for path, filename in paths]
 
     for sha1, filename in results:
       artifact_target_map[(sha1, filename)].append(get_target_name(target))