download_images.py revision 43f1a45c8dddfc4ff8c9dfcd87070811abf936dd
1#!/usr/bin/python
2
3# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import os
8
9from utils import command_executer
10
11class ImageDownloader(object):
12
13  def __init__(self, logger_to_use=None, log_level="verbose"):
14    self._logger = logger_to_use
15    self.log_level = log_level
16    self._ce = command_executer.GetCommandExecuter(self._logger,
17                                                   log_level = self.log_level)
18
19  def Run(self, chromeos_root, xbuddy_label):
20    # Get the translation of the xbuddy_label into the real Google Storage
21    # image name.
22    command = ("cd ~/trunk/src/third_party/toolchain-utils/crosperf; "
23               "python translate_xbuddy.py '%s'" % xbuddy_label)
24    retval, build_id_tuple_str, _ = self._ce.ChrootRunCommand(chromeos_root,
25                                                          command, True)
26    build_id_tuple = eval(build_id_tuple_str)
27    build_id = build_id_tuple[0]
28
29    # Make sure the directory for downloading the image exists.
30    download_path = os.path.join(chromeos_root, "chroot/tmp",
31                                 build_id)
32    image_path = os.path.join(download_path, "chromiumos_test_image.bin")
33    if not os.path.exists(download_path):
34      command = "mkdir -p %s" % download_path
35      self._ce.RunCommand (command)
36
37    # Check to see if the image has already been downloaded.  If not,
38    # download the image.
39    retval = 0
40    if not os.path.exists(image_path):
41      command = ("gsutil cp gs://chromeos-image-archive/%s"
42                 "/chromiumos_test_image.tar.xz /tmp/%s" % (build_id,
43                                                            build_id))
44
45      retval = self._ce.ChrootRunCommand(chromeos_root, command)
46
47      # Uncompress and untar the downloaded image.
48      command = ("cd /tmp/%s ;unxz chromiumos_test_image.tar.xz; "
49                 "tar -xvf chromiumos_test_image.tar" % build_id)
50      retval = self._ce.ChrootRunCommand(chromeos_root, command)
51
52    return retval, image_path
53