Remote XBuddy function.

Locate artifacts on GS, given aliases for versions.
BUG=chromium:252942
TEST=Unittests, Manual
xbuddy_unittests.py updated for new functions
Run devserver and query xbuddy using any path with remote aliases.
e.g.
http://{your_ip}:8080/xbuddy/parrot/latest-stable/test
http://{your_ip}:8080/xbuddy/parrot/latest-official/test
http://{your_ip}:8080/xbuddy/parrot/latest-official-paladin/test
http://{your_ip}:8080/xbuddy/parrot/latest-R27/test

Change-Id: I171e23cbfbaa6ce181b09393c2c6e6dfb12baca5
Reviewed-on: https://gerrit.chromium.org/gerrit/61802
Commit-Queue: Joy Chen <joychen@chromium.org>
Reviewed-by: Joy Chen <joychen@chromium.org>
Tested-by: Joy Chen <joychen@chromium.org>
diff --git a/gsutil_util.py b/gsutil_util.py
index 32ab58e..25b7da8 100644
--- a/gsutil_util.py
+++ b/gsutil_util.py
@@ -4,11 +4,13 @@
 
 """Module containing gsutil helper methods."""
 
+import distutils.version
 import random
 import re
 import subprocess
 import time
 
+import devserver_constants
 import log_util
 
 
@@ -147,3 +149,22 @@
       time.sleep(to_delay)
     else:
       return None
+
+
+def GetLatestVersionFromGSDir(gsutil_dir):
+  """Returns most recent version number found in a GS directory.
+
+  This lists out the contents of the given GS bucket or regex to GS buckets,
+  and tries to grab the newest version found in the directory names.
+  """
+  cmd = 'gsutil ls %s' % gsutil_dir
+  msg = 'Failed to find most recent builds at %s' % gsutil_dir
+  dir_names = [p.split('/')[-2] for p in GSUtilRun(cmd, msg).splitlines()]
+  try:
+    versions = filter(lambda x: re.match(devserver_constants.VERSION_RE, x),
+                      dir_names)
+    latest_version = max(versions, key=distutils.version.LooseVersion)
+  except ValueError:
+    raise GSUtilError(msg)
+
+  return latest_version