Move non-AB OTA generation code to a separate file

Test: Generate a non-AB OTA, apply it
Change-Id: I2f1afbe70d17356fcbf4d59901d201a76a3d6c4f
diff --git a/tools/releasetools/check_target_files_vintf.py b/tools/releasetools/check_target_files_vintf.py
index ef66112..0edefac 100755
--- a/tools/releasetools/check_target_files_vintf.py
+++ b/tools/releasetools/check_target_files_vintf.py
@@ -220,6 +220,52 @@
 
   raise ValueError('{} is not a valid directory or zip file'.format(inp))
 
+def CheckVintfIfTrebleEnabled(target_files, target_info):
+  """Checks compatibility info of the input target files.
+
+  Metadata used for compatibility verification is retrieved from target_zip.
+
+  Compatibility should only be checked for devices that have enabled
+  Treble support.
+
+  Args:
+    target_files: Path to zip file containing the source files to be included
+        for OTA. Can also be the path to extracted directory.
+    target_info: The BuildInfo instance that holds the target build info.
+  """
+
+  # Will only proceed if the target has enabled the Treble support (as well as
+  # having a /vendor partition).
+  if not HasTrebleEnabled(target_files, target_info):
+    return
+
+  # Skip adding the compatibility package as a workaround for b/114240221. The
+  # compatibility will always fail on devices without qualified kernels.
+  if OPTIONS.skip_compatibility_check:
+    return
+
+  if not CheckVintf(target_files, target_info):
+    raise RuntimeError("VINTF compatibility check failed")
+
+def HasTrebleEnabled(target_files, target_info):
+  def HasVendorPartition(target_files):
+    if os.path.isdir(target_files):
+      return os.path.isdir(os.path.join(target_files, "VENDOR"))
+    if zipfile.is_zipfile(target_files):
+      return HasPartition(zipfile.ZipFile(target_files), "vendor")
+    raise ValueError("Unknown target_files argument")
+
+  return (HasVendorPartition(target_files) and
+          target_info.GetBuildProp("ro.treble.enabled") == "true")
+
+
+def HasPartition(target_files_zip, partition):
+  try:
+    target_files_zip.getinfo(partition.upper() + "/")
+    return True
+  except KeyError:
+    return False
+
 
 def main(argv):
   args = common.ParseOptions(argv, __doc__)