Make devserver backward compatible when staging older builds

We recently added a manifest file in the Google Storage bucket so that
devserver can get the bucket listing without using "gsutil ls", which
supports only the eventual consistency model. However, older builds
which are already on the Google Storage do not have manifest files in
their buckets. This change makes devserver_util backward compatible by
falling back to using "gsutil ls" whenever the manifest file is not
present.

BUG=chromium-os:33802
TEST=local devserver

Change-Id: I01eaec04777a8015de24600ecc3a92566e7edd62
Reviewed-on: https://gerrit.chromium.org/gerrit/31148
Tested-by: Yu-Ju Hong <yjhong@chromium.org>
Reviewed-by: Chris Sosa <sosa@chromium.org>
Commit-Ready: Yu-Ju Hong <yjhong@chromium.org>
diff --git a/devserver_util.py b/devserver_util.py
index afba671..8dc47b6 100644
--- a/devserver_util.py
+++ b/devserver_util.py
@@ -124,10 +124,21 @@
 
   deadline = time.time() + timeout
   while time.time() < deadline:
+    uploaded_list = []
     to_delay = delay + random.uniform(.5 * delay, 1.5 * delay)
-    # Run "gsutil cat" to retrieve the list
-    uploaded_list = gsutil_util.GSUtilRun(cmd, msg).splitlines()
-    # Check if all target artifacts are available
+    try:
+      # Run "gsutil cat" to retrieve the list.
+      uploaded_list = gsutil_util.GSUtilRun(cmd, msg).splitlines()
+    except gsutil_util.GSUtilError:
+      # For backward compatibility, fallling back to use "gsutil ls"
+      # when the manifest file is not present.
+      cmd = 'gsutil ls %s/*' % archive_url
+      msg = 'Failed to list payloads.'
+      payload_list = gsutil_util.GSUtilRun(cmd, msg).splitlines()
+      for payload in payload_list:
+        uploaded_list.append(payload.rsplit('/', 1)[1])
+
+    # Check if all target artifacts are available.
     if IsAvailable(to_wait_list, uploaded_list):
       return uploaded_list
     cherrypy.log('Retrying in %f seconds...%s' % (to_delay, err_str))