stack.py: Fixing --symbols-zip argument

Bug: 1284954
Change-Id: I2043f5609356c068e7d0deb945828a0b5b9e1b7e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3812375
Reviewed-by: Mohamed Heikal <mheikal@chromium.org>
Commit-Queue: Sam Maier <smaier@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1032103}
NOKEYCHECK=True
GitOrigin-RevId: 5c6cdcd5494e3a60fffa67ffd873c156385f32c9
diff --git a/development/scripts/stack.py b/development/scripts/stack.py
index 53664b1..276bf1b 100755
--- a/development/scripts/stack.py
+++ b/development/scripts/stack.py
@@ -28,6 +28,7 @@
 import subprocess
 import symbol
 import sys
+import zipfile
 
 sys.path.insert(0, os.path.join(os.path.dirname(__file__),
                                 os.pardir, os.pardir, os.pardir, os.pardir,
@@ -102,6 +103,7 @@
   print("       symbolization")
   sys.exit(1)
 
+
 def UnzipSymbols(symbolfile, symdir=None):
   """Unzips a file to DEFAULT_SYMROOT and returns the unzipped location.
 
@@ -123,33 +125,28 @@
     os.makedirs(symdir)
 
   logging.info('extracting %s...', symbolfile)
-  saveddir = os.getcwd()
-  os.chdir(symdir)
-  try:
-    unzipcode = subprocess.call(["unzip", "-qq", "-o", symbolfile])
-    if unzipcode > 0:
-      os.remove(symbolfile)
-      raise SymbolDownloadException("failed to extract symbol files (%s)."
-                                    % symbolfile)
-  finally:
-    os.chdir(saveddir)
+  with zipfile.ZipFile(symbolfile, 'r') as zip_ref:
+    zip_ref.extractall(symdir)
 
-  android_symbols = glob.glob("%s/out/target/product/*/symbols" % symdir)
-  if android_symbols:
-    return (symdir, android_symbols[0])
-
-  # This is a zip of Chrome symbols, so symbol.CHROME_SYMBOLS_DIR needs to be
-  # updated to point here.
-  symbol.CHROME_SYMBOLS_DIR = symdir
-  return (symdir, symdir)
+  return symdir
 
 
 def main(argv, test_symbolizer=None):
   try:
     options, arguments = getopt.getopt(argv, "p", [
-        "pass-through", "more-info", "less-info", "chrome-symbols-dir=",
-        "output-directory=", "apks-directory=", "symbols-dir=", "symbols-zip=",
-        "arch=", "fallback-so-file=", "verbose", "quiet", "help",
+        "pass-through",
+        "more-info",
+        "less-info",
+        "chrome-symbols-dir=",
+        "output-directory=",
+        "apks-directory=",
+        "symbols-dir=",
+        "symbols-zip=",
+        "arch=",
+        "fallback-so-file=",
+        "verbose",
+        "quiet",
+        "help",
     ])
   except getopt.GetoptError:
     PrintUsage()
@@ -195,6 +192,15 @@
   if len(arguments) > 1:
     PrintUsage()
 
+  rootdir = None
+  if zip_arg:
+    rootdir = UnzipSymbols(zip_arg)
+    for subdir, dirs, _ in os.walk(rootdir):
+      if 'lib.unstripped' in dirs:
+        unzipped_output_dir = subdir
+        break
+    constants.SetOutputDirectory(unzipped_output_dir)
+
   logging.basicConfig(level=log_level)
   # Do an up-front test that the output directory is known.
   if not symbol.CHROME_SYMBOLS_DIR:
@@ -203,12 +209,8 @@
   logging.info('Reading Android symbols from: %s',
                os.path.normpath(symbol.SYMBOLS_DIR))
   chrome_search_path = symbol.GetLibrarySearchPaths()
-  logging.info('Searching for Chrome symbols from within: %s',
-               ':'.join((os.path.normpath(d) for d in chrome_search_path)))
-
-  rootdir = None
-  if zip_arg:
-    rootdir, symbol.SYMBOLS_DIR = UnzipSymbols(zip_arg)
+  logging.info('Searching for Chrome symbols from within: %s', ':'.join(
+      (os.path.normpath(d) for d in chrome_search_path)))
 
   if not arguments or arguments[0] == '-':
     logging.info('Reading native crash info from stdin (symbolization starts '
@@ -235,11 +237,11 @@
     load_vaddrs = {}
 
     with llvm_symbolizer.LLVMSymbolizer() as symbolizer:
-      logging.info('Searching for Chrome symbols from within: %s',
-                   ':'.join((os.path.normpath(d) for d in chrome_search_path)))
-      stack_core.ConvertTrace(lines, load_vaddrs, more_info,
-                              fallback_so_file, arch_defined,
-                              test_symbolizer or symbolizer, apks_directory)
+      logging.info('Searching for Chrome symbols from within: %s', ':'.join(
+          (os.path.normpath(d) for d in chrome_search_path)))
+      stack_core.ConvertTrace(lines, load_vaddrs, more_info, fallback_so_file,
+                              arch_defined, test_symbolizer or symbolizer,
+                              apks_directory)
 
   if rootdir:
     # be a good citizen and clean up...os.rmdir and os.removedirs() don't work
@@ -249,6 +251,7 @@
 
   return 0
 
+
 if __name__ == "__main__":
   sys.exit(main(sys.argv[1:]))