extract-utils: Switch to ota_extractor

The python tools no longer work and google recommends ota_extractor.
This opens up the possibility for incremental extraction as well.

Change-Id: I03a2ce5d75d192f00ebd604d13694f8694965192
diff --git a/extract_ota.py b/extract_ota.py
deleted file mode 100755
index a41f0ef..0000000
--- a/extract_ota.py
+++ /dev/null
@@ -1,96 +0,0 @@
-#!/usr/bin/env python3
-#
-# Copyright (C) 2020 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-"""Script to extract payload.bin from an OTA update."""
-
-import argparse
-import os
-import tempfile
-import zipfile
-
-import update_payload
-from update_payload import applier
-
-def extract_ota(payload_path, list_partitions, output_dir, partitions):
-  """Extract OTA payload"""
-  payload = update_payload.Payload(payload_path)
-  payload.Init()
-
-  new_parts = {}
-  new_part_info = {}
-  install_operations = []
-  for part in payload.manifest.partitions:
-    name = part.partition_name
-    if list_partitions:
-      print(name)
-    if partitions and name not in partitions:
-      continue
-    new_image = os.path.join(output_dir, name + ".img")
-    new_parts[name] = new_image
-    new_part_info[name] = part.new_partition_info
-    install_operations.append((name, part.operations))
-
-  if not list_partitions:
-    for name, operations in install_operations:
-      applier.PayloadApplier(payload)._ApplyToPartition(
-            operations, name, '%s_install_operations' % name, new_parts[name],
-            new_part_info[name])
-
-def main():
-  parser = argparse.ArgumentParser(
-      description="Extract payload.bin from OTA package")
-  parser.add_argument(
-      "payload",
-      help="payload.bin for the OTA package, or a zip of OTA package itself",
-      nargs=1
-  )
-  parser.add_argument(
-      "-l",
-      dest="list_partitions",
-      help="List partitions, without extracting",
-      action='store_true')
-  parser.add_argument(
-      "-o",
-      dest="output_dir",
-      help="Output directory to put all images, current directory by default"
-  )
-  parser.add_argument(
-      "-p",
-      dest="partitions",
-      help="List of partitions to extract, all by default",
-      nargs="*"
-  )
-  args = parser.parse_args()
-
-  # pylint: disable=no-member
-  with tempfile.TemporaryDirectory() as tempdir:
-    payload_path = args.payload[0]
-    if zipfile.is_zipfile(payload_path):
-      with zipfile.ZipFile(payload_path, "r") as zfp:
-        payload_entry_name = 'payload.bin'
-        zfp.extract(payload_entry_name, tempdir)
-        payload_path = os.path.join(tempdir, payload_entry_name)
-    if args.output_dir is None:
-      args.output_dir = "."
-    if not os.path.exists(args.output_dir):
-      os.makedirs(args.output_dir, exist_ok=True)
-    assert os.path.isdir(args.output_dir)
-    extract_ota(payload_path, args.list_partitions, args.output_dir, args.partitions)
-
-
-if __name__ == '__main__':
-  main()
diff --git a/extract_utils.sh b/extract_utils.sh
index a9a5037..7211848 100644
--- a/extract_utils.sh
+++ b/extract_utils.sh
@@ -58,6 +58,7 @@
 
     export SIMG2IMG="$BINARIES_LOCATION"/simg2img
     export LPUNPACK="$BINARIES_LOCATION"/lpunpack
+    export OTA_EXTRACTOR="$BINARIES_LOCATION"/ota_extractor
     export SIGSCAN="$BINARIES_LOCATION"/SigScan
 
     for version in 0_8 0_9 0_17_2; do
@@ -1557,7 +1558,7 @@
 
             # Extract A/B OTA
             if [ -a "$DUMPDIR"/payload.bin ]; then
-                python3 "$ANDROID_ROOT"/tools/extract-utils/extract_ota.py "$DUMPDIR"/payload.bin -o "$DUMPDIR" -p "system" "odm" "product" "system_ext" "vendor" 2>&1
+                "$OTA_EXTRACTOR" --payload "$DUMPDIR"/payload.bin --output_dir "$DUMPDIR" --partitions "system","odm","product","system_ext","vendor" 2>&1
             fi
 
             for PARTITION in "system" "odm" "product" "system_ext" "vendor"
@@ -1951,7 +1952,7 @@
         if [ -f "$SRC" ] && [ "${SRC##*.}" == "zip" ]; then
             # Extract A/B OTA
             if [ -a "$DUMPDIR"/payload.bin ]; then
-                python3 "$ANDROID_ROOT"/tools/extract-utils/extract_ota.py "$DUMPDIR"/payload.bin -o "$DUMPDIR" -p $(basename "${DST_FILE%.*}") 2>&1
+                "$OTA_EXTRACTOR" --payload "$DUMPDIR"/payload.bin --output_dir "$DUMPDIR" --partitions $(basename "${DST_FILE%.*}") 2>&1
                 if [ -f "$DUMPDIR/$(basename $DST_FILE)" ]; then
                     COPY_FILE="$DUMPDIR/$(basename $DST_FILE)"
                 fi
diff --git a/update_payload b/update_payload
deleted file mode 120000
index 8f87be6..0000000
--- a/update_payload
+++ /dev/null
@@ -1 +0,0 @@
-../../system/update_engine/scripts/update_payload
\ No newline at end of file