fix crash in autotest_quickmerge when downgraded package doesn't exist

This CL catches a ValueError exception that gets thrown when trying to
get a portage API handle on a non-existant package, and returns this as
a failure to downgrade the given package rather than causing
autotest_quickmerge to crash.

BUG=chromium:239649
TEST=Manually deleted autotest-telemetry package from portage database.
Ran autotest_quickmerge, saw a warning about being unable to downgrade
package rather than seeing autotest_quickmerge crash.

Change-Id: I9e93edaf7d4fa17b176d259adf9934ede362dccc
Reviewed-on: https://gerrit.chromium.org/gerrit/57500
Commit-Queue: Aviv Keshet <akeshet@chromium.org>
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
Tested-by: Aviv Keshet <akeshet@chromium.org>
diff --git a/scripts/autotest_quickmerge.py b/scripts/autotest_quickmerge.py
index 57fafa7..a3dc5dd 100644
--- a/scripts/autotest_quickmerge.py
+++ b/scripts/autotest_quickmerge.py
@@ -57,6 +57,9 @@
                                   ['new_files', 'modified_files',
                                    'new_directories'])
 
+class PortagePackageAPIError(Exception):
+  """Exception thrown when unable to retrieve a portage package API."""
+
 
 def GetStalePackageNames(change_list, autotest_sysroot):
   """Given a rsync change report, returns the names of stale test packages.
@@ -141,12 +144,13 @@
   matching_packages = vartree.dbapi.cp_list(package_cp)
 
   if not matching_packages:
-    raise ValueError('No matching package for %s in portage_root %s' % (
-                     package_cp, portage_root))
+    raise PortagePackageAPIError('No matching package for %s in portage_root '
+                                 '%s' % (package_cp, portage_root))
 
   if len(matching_packages) > 1:
-    raise ValueError('Too many matching packages for %s in portage_root '
-                     '%s' % (package_cp, portage_root))
+    raise PortagePackageAPIError('Too many matching packages for %s in '
+                                 'portage_root %s' % (package_cp,
+                                                      portage_root))
 
   # Convert string match to package dblink.
   package_cpv = matching_packages[0]
@@ -171,7 +175,13 @@
   Returns:
     True on success. False on failure (nonzero return code from `mv` command).
   """
-  package, _ = GetPackageAPI(portage_root, package_cp)
+  try:
+    package, _ = GetPackageAPI(portage_root, package_cp)
+  except PortagePackageAPIError:
+    # Unable to fetch a corresponding portage package API for this
+    # package_cp (either no such package, or name ambigious and matches).
+    # So, just fail out.
+    return False
 
   source_directory = package.dbdir
   destination_path = os.path.join(