releasetools: Add common.ExtractAvbPublicKey().

Bug: 123716522
Test: python -m unittest test_common
Change-Id: I1f645008a14cc882ef280f169c36e4b14a53ef88
diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py
index 9cda0bd..e71667b 100644
--- a/tools/releasetools/common.py
+++ b/tools/releasetools/common.py
@@ -50,7 +50,8 @@
     if base_out_path is None:
       base_search_path = "out"
     else:
-      base_search_path = os.path.join(base_out_path, os.path.basename(os.getcwd()))
+      base_search_path = os.path.join(base_out_path,
+                                      os.path.basename(os.getcwd()))
 
     platform_search_path = {
         "linux2": os.path.join(base_search_path, "host/linux-x86"),
@@ -552,11 +553,7 @@
   """
   if key is None:
     key = info_dict["avb_" + partition + "_key_path"]
-  avbtool = os.getenv('AVBTOOL') or info_dict["avb_avbtool"]
-  pubkey_path = MakeTempFile(prefix="avb-", suffix=".pubkey")
-  RunAndCheckOutput(
-      [avbtool, "extract_public_key", "--key", key, "--output", pubkey_path])
-
+  pubkey_path = ExtractAvbPublicKey(key)
   rollback_index_location = info_dict[
       "avb_" + partition + "_rollback_index_location"]
   return "{}:{}:{}".format(partition, rollback_index_location, pubkey_path)
@@ -2123,6 +2120,21 @@
   return pubkey
 
 
+def ExtractAvbPublicKey(key):
+  """Extracts the AVB public key from the given public or private key.
+
+  Args:
+    key: The input key file, which should be PEM-encoded public or private key.
+
+  Returns:
+    The path to the extracted AVB public key file.
+  """
+  output = MakeTempFile(prefix='avb-', suffix='.avbpubkey')
+  RunAndCheckOutput(
+      ['avbtool', 'extract_public_key', "--key", key, "--output", output])
+  return output
+
+
 def MakeRecoveryPatch(input_dir, output_sink, recovery_img, boot_img,
                       info_dict=None):
   """Generates the recovery-from-boot patch and writes the script to output.
diff --git a/tools/releasetools/test_common.py b/tools/releasetools/test_common.py
index cfd070d..8709124 100644
--- a/tools/releasetools/test_common.py
+++ b/tools/releasetools/test_common.py
@@ -491,6 +491,13 @@
     wrong_input = os.path.join(self.testdata_dir, 'testkey.pk8')
     self.assertRaises(AssertionError, common.ExtractPublicKey, wrong_input)
 
+  def test_ExtractAvbPublicKey(self):
+    privkey = os.path.join(self.testdata_dir, 'testkey.key')
+    pubkey = os.path.join(self.testdata_dir, 'testkey.pubkey.pem')
+    with open(common.ExtractAvbPublicKey(privkey)) as privkey_fp, \
+        open(common.ExtractAvbPublicKey(pubkey)) as pubkey_fp:
+      self.assertEqual(privkey_fp.read(), pubkey_fp.read())
+
   def test_ParseCertificate(self):
     cert = os.path.join(self.testdata_dir, 'testkey.x509.pem')
 
@@ -1218,7 +1225,7 @@
       dp_diff.WriteScript(self.script, output_zip, write_verify_script=True)
 
     self.assertNotIn("block_image_update", str(self.script),
-        "Removed partition should not be patched.")
+                     "Removed partition should not be patched.")
 
     lines = self.get_op_list(self.output_path)
     self.assertEqual(lines, ["remove foo"])