dev: Let devserver use gsutil functions in chromite.

Together with CL:378576, this CL is to make platform/dev to use GS-related
functions in chromite. This could help reduce the maintain cost and
inconsistency errors caused by different versions of gsutil.

BUG=chromium:640609
TEST=Ran unittest.
Test 2 functions with local devserver: trigger_download &
get_latest_build_in_gs (including xbuddy/latest, xbuddy/latest-official,
xbuddy/latest-{dev|beta|stable}, xbuddy/latest-R53).

Change-Id: I27f97ebebb76ea6b6b5ef9cc4d5b81c524f6968c
Reviewed-on: https://chromium-review.googlesource.com/381693
Commit-Ready: Xixuan Wu <xixuan@chromium.org>
Tested-by: Xixuan Wu <xixuan@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
diff --git a/downloader.py b/downloader.py
index 3bd0d03..37dd54b 100755
--- a/downloader.py
+++ b/downloader.py
@@ -1,5 +1,5 @@
-#!/usr/bin/python2
-#
+#!/usr/bin/env python2
+
 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
@@ -18,9 +18,16 @@
 
 import build_artifact
 import common_util
-import gsutil_util
 import log_util
 
+# Make sure that chromite is available to import.
+import setup_chromite # pylint: disable=unused-import
+
+try:
+  from chromite.lib import gs
+except ImportError as e:
+  gs = None
+
 try:
   import android_build
 except ImportError as e:
@@ -167,7 +174,6 @@
 
     Raises:
       build_artifact.ArtifactDownloadError: If failed to download the artifact.
-
     """
     common_util.MkDirP(self._build_dir)
 
@@ -202,7 +208,6 @@
     Raises:
       DownloaderException: A wrapper for exceptions raised by any artifact when
                            calling Process.
-
     """
     required_artifacts = factory.RequiredArtifacts()
     exceptions = [artifact.GetException() for artifact in required_artifacts if
@@ -224,7 +229,6 @@
     Raises:
       build_artifact.ArtifactDownloadError: If we failed to download the
                                             artifact.
-
     """
     try:
       for artifact in artifacts:
@@ -304,6 +308,8 @@
 
     self._archive_url = archive_url
 
+    self._ctx = gs.GSContext() if gs else None
+
   def Wait(self, name, is_regex_name, timeout):
     """Waits for artifact to exist and returns the appropriate names.
 
@@ -318,8 +324,8 @@
     Raises:
       ArtifactDownloadError: An error occurred when obtaining artifact.
     """
-    names = gsutil_util.GetGSNamesWithWait(
-        name, self._archive_url, str(self), timeout=timeout,
+    names = self._ctx.GetGsNamesWithWait(
+        name, self._archive_url, timeout=timeout,
         is_regex_pattern=is_regex_name)
     if not names:
       raise build_artifact.ArtifactDownloadError(
@@ -331,7 +337,7 @@
     """Downloads artifact from Google Storage to a local directory."""
     install_path = os.path.join(local_path, remote_name)
     gs_path = '/'.join([self._archive_url, remote_name])
-    gsutil_util.DownloadFromGS(gs_path, local_path)
+    self._ctx.Copy(gs_path, local_path)
     return install_path
 
   def DescribeSource(self):