XBuddy: latest-local image + fix xbuddy_list bug

Serve the latest-local image for a given board.

Symlinks the image directory to a corresponding directory in the devserver
/static folder, and deletes the symlinked file when past
XBUDDY_CAPACITY.

ALSO: fix lexicographic sorting of time stamps + pretty print
xbuddy_list

BUG=chromium:252941
BUG=chromium:252942
TEST=Manual

Either run ./build_image more than 5 times, or call
http://{your_ip}:8080/xbuddy?path=x86-generic/latest-local/test
on over 5 different boards.
WARNING: xBuddy's cache management is on, so this may delete some of
the images currently in your build/images directory.

Should correctly serve the test image every time, and delete the last
referenced image (and its corresponding timestamp file) from disk.

Locally built images and images downloaded from GS count equivalently.
i.e. There should never be more than 5 at any given time.

Change-Id: I2f5c4e2d67365f3523ddfdeec2ddedbd38009e32
Reviewed-on: https://gerrit.chromium.org/gerrit/60707
Reviewed-by: Ryan Cui <rcui@chromium.org>
Commit-Queue: Joy Chen <joychen@chromium.org>
Reviewed-by: Joy Chen <joychen@chromium.org>
Tested-by: Joy Chen <joychen@chromium.org>
diff --git a/build_util.py b/build_util.py
index e5e32f1..0dfc97d 100644
--- a/build_util.py
+++ b/build_util.py
@@ -8,15 +8,27 @@
 
 class BuildObject(object):
   """
-    Common base class that defines key paths in the source tree.
+  Common base class that defines key paths in the source tree.
+
+  Classes that inherit from BuildObject can access scripts in the src/scripts
+  directory, and have a handle to the static directory of the devserver.
   """
   def __init__(self, root_dir, static_dir):
     self.devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
     self.static_dir = static_dir
     try:
-      self.scripts_dir = '%s/src/scripts' % os.environ['CROS_WORKON_SRCROOT']
+      chroot_dir = os.environ['CROS_WORKON_SRCROOT']
+      self.scripts_dir = os.path.join(chroot_dir, 'src/scripts')
+      self.images_dir = os.path.join(chroot_dir, 'src/build/images')
     except KeyError:
       # Outside of chroot: This is a corner case. Since we live either in
       # platform/dev or /usr/bin/, scripts have to live in ../../../src/scripts
       self.scripts_dir = os.path.abspath(os.path.join(
           self.devserver_dir, '../../../src/scripts'))
+      self.images_dir = os.path.abspath(os.path.join(
+          self.devserver_dir, '../../../src/build/images'))
+
+  def GetLatestImageDir(self, board):
+    """Returns the latest image dir based on shell script."""
+    cmd = '%s/get_latest_image.sh --board %s' % (self.scripts_dir, board)
+    return os.popen(cmd).read().strip()