Check that non AFDO images exist before trying to use them.

Check the non-AFDO images exist before we try to get performance
numbers for them.

BUG=None
TEST=white box testing.

Change-Id: I182c9da1dd29c377b98e3012ba7b5e186cdf2e4f
Reviewed-on: https://chrome-internal-review.googlesource.com/243956
Commit-Ready: Luis Lozano <llozano@chromium.org>
Tested-by: Luis Lozano <llozano@chromium.org>
Reviewed-by: Yunlian Jiang <yunlian@google.com>
diff --git a/buildbot_test_toolchains.py b/buildbot_test_toolchains.py
index ea6099b..e02f6b3 100755
--- a/buildbot_test_toolchains.py
+++ b/buildbot_test_toolchains.py
@@ -161,14 +161,15 @@
       f.write(official_image)
 
       # Now add non-AFDO image to test file.
-      official_nonafdo_image = """
+      if nonafdo_image:
+        official_nonafdo_image = """
           nonafdo_image {
             chromeos_root: %s
             build: %s
             compiler: gcc
           }
           """ % (self._chromeos_root, nonafdo_image)
-      f.write(official_nonafdo_image)
+        f.write(official_nonafdo_image)
 
       label_string = '%s_trybot_image' % compiler_string
       if USE_NEXT_GCC_PATCH in self._patches:
@@ -233,14 +234,10 @@
       self._ce.RunCommand(cmd)
 
     # Now create new tar files and copy them over.
-    labels = ['test', 'vanilla', 'nonafdo']
-    for label_name in labels:
-      if label_name == 'test':
-        test_path = trybot_image
-      elif label_name == 'vanilla':
-        test_path = vanilla_image
-      else:
-        test_path = nonafdo_image
+    labels = {'test': trybot_image, 'vanilla': vanilla_image}
+    if nonafdo_image:
+      labels['nonafdo'] = nonafdo_image
+    for label_name, test_path in labels.iteritems():
       tar_file_name = '%s_%s_image.tar' % (self._weekday, label_name)
       cmd = ('cd %s; tar -cvf %s %s/chromiumos_test_image.bin; '
              'cp %s %s/.') % (images_path, tar_file_name, test_path,
@@ -287,6 +284,8 @@
 
     vanilla_image = self._ParseVanillaImage(trybot_image)
     nonafdo_image = self._ParseNonAFDOImage(trybot_image)
+    if not buildbot_utils.DoesImageExist(self._chromeos_root, nonafdo_image):
+      nonafdo_image = ''
 
     # The trybot image is ready here, in some cases, the vanilla image
     # is not ready, so we need to make sure vanilla image is available.
diff --git a/utils/buildbot_utils.py b/utils/buildbot_utils.py
index 0116fc6..a636644 100644
--- a/utils/buildbot_utils.py
+++ b/utils/buildbot_utils.py
@@ -1,8 +1,7 @@
 # Copyright 2014 Google Inc. All Rights Reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
-
-"""Module for building with cbuildbot."""
+"""Utilities for launching and accessing ChromeOS buildbots."""
 
 from __future__ import print_function
 
@@ -25,7 +24,11 @@
     1,  # "warnings"
     6,  # "retry"
 ]
-"""Utilities for launching and accessing ChromeOS buildbots."""
+
+
+class BuildbotTimeout(Exception):
+  """Exception to throw when a buildbot operation timesout."""
+  pass
 
 
 def ParseReportLog(url, build):
@@ -98,7 +101,7 @@
       if str(description) in my_dict['reason']:
         # We found a match; we're done.
         return my_dict
-    except:
+    except KeyError:
       print("reason is not in dictionary: '%s'" % repr(my_dict))
     else:
       # Keep going.
@@ -297,23 +300,29 @@
   logger.GetLogger().LogOutput('build_status is %d' % build_status)
   return trybot_image
 
-def WaitForImage(chromeos_root, build):
-  """Wait for a image to be ready."""
 
-  ready = False
-  elapsed_time = 0
+def DoesImageExist(chromeos_root, build):
+  """Check if the image for the given build exists."""
+
   ce = command_executer.GetCommandExecuter()
   command = ('gsutil ls gs://chromeos-image-archive/%s'
              '/chromiumos_test_image.tar.xz' % (build))
-  while not ready and elapsed_time < TIME_OUT:
-    ret = ce.ChrootRunCommand(chromeos_root,
-                              command,
-                              print_to_console=False)
-    if not ret:
-      return ret
-    logger.GetLogger().LogOutput("Image %s not ready, waiting for 10 minutes"
-                                 % build)
+  ret = ce.ChrootRunCommand(chromeos_root, command, print_to_console=False)
+  return not ret
+
+
+def WaitForImage(chromeos_root, build):
+  """Wait for an image to be ready."""
+
+  elapsed_time = 0
+  while elapsed_time < TIME_OUT:
+    if DoesImageExist(chromeos_root, build):
+      return
+    logger.GetLogger().LogOutput('Image %s not ready, waiting for 10 minutes' %
+                                 build)
     time.sleep(SLEEP_TIME)
     elapsed_time += SLEEP_TIME
 
-  return ret
+  logger.GetLogger().LogOutput('Image %s not found, waited for %d hours' %
+                               (build, (TIME_OUT / 3600)))
+  raise BuildbotTimeout('Timeout while waiting for image %s' % build)