Home | History | Annotate | Download | only in firmware_TouchMTB
      1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """An interface for accessing Google Cloud Storage."""
      6 
      7 import os
      8 import shutil
      9 import tempfile
     10 
     11 from autotest_lib.client.common_lib import file_utils
     12 from common_util import simple_system
     13 
     14 
     15 PUBLIC_BOTO = 'public/.chromeos.gestures.untrusted.write.boto'
     16 GS_BUCKET = 'gs://chromeos-touchpad'
     17 GS_BUCKET_PUBLIC = GS_BUCKET + '-public'
     18 GSUTIL = 'gsutil'
     19 GSUTIL_URI_ROOT = 'http://storage.googleapis.com/pub'
     20 GSUTIL_TAR_NAME = 'gsutil.tar.gz'
     21 GSUTIL_URI = os.path.join(GSUTIL_URI_ROOT, GSUTIL_TAR_NAME)
     22 GSUTIL_INSTALL_DIR = os.path.join('/', 'usr', 'local', 'share')
     23 GSUTIL_PATH = os.path.join(GSUTIL_INSTALL_DIR, GSUTIL)
     24 
     25 
     26 def download_and_install_gsutil():
     27     """Download and install gsutil package."""
     28     if not os.path.isdir(GSUTIL_PATH):
     29         print 'Installing %s ...' % GSUTIL
     30 
     31         # Download the gsutil tarball to a temporary directory
     32         temp_dir = tempfile.mkdtemp()
     33         gsutil_temp_file = os.path.join(temp_dir, GSUTIL_TAR_NAME)
     34         print '  Downloading gsutil tarball: "%s".' % GSUTIL_URI
     35         file_utils.download_file(GSUTIL_URI, gsutil_temp_file)
     36 
     37         # Untar the gsutil tarball
     38         untar_cmd_str = 'tar xf %s -C %s'
     39         untar_cmd = untar_cmd_str % (gsutil_temp_file, GSUTIL_INSTALL_DIR)
     40         print '  Untarring the gsutil tarball.'
     41         simple_system(untar_cmd)
     42 
     43         # Remove the tarball and the temp directory
     44         shutil.rmtree(temp_dir)
     45 
     46     # Set the PATH environment variable for gsutil
     47     PATH = os.environ['PATH']
     48     os.environ['PATH'] = ':'.join([GSUTIL_PATH, PATH])
     49 
     50 
     51 class CrosGs(object):
     52     """A class handling google cloud storage access."""
     53     def __init__(self, board, boto=PUBLIC_BOTO):
     54         download_and_install_gsutil()
     55 
     56         # Set up gsutil commands
     57         self.bucket = GS_BUCKET_PUBLIC if boto == PUBLIC_BOTO else GS_BUCKET
     58         bucket = self.bucket
     59         self.default_bucket_dir = os.path.join(
     60                 'firmware_test', board, 'data', '')
     61         _cmd_prefix = 'BOTO_CONFIG=%s gsutil ' % boto
     62         self.ls_cmd = '{0} {1} {2}/%s'.format(_cmd_prefix, 'ls', bucket)
     63         upload_cmd_str = '{0} {1} %s %s {2}/%s'
     64         self.upload_cmd = upload_cmd_str.format(_cmd_prefix, 'cp', bucket)
     65         download_cmd_str = '{0} {1} %s {2}/%s %s'
     66         self.download_cmd = download_cmd_str.format(_cmd_prefix, 'cp', bucket)
     67         self.rm_cmd = '{0} {1} {2}/%s'.format(_cmd_prefix, 'rm', bucket)
     68 
     69     def ls(self, files=''):
     70         """ls the files in the selected bucket."""
     71         simple_system(self.ls_cmd % files)
     72 
     73     def upload(self, data, bucket_dir=''):
     74         """Upload the data to the chosen bucket."""
     75         if not bucket_dir:
     76             bucket_dir = self.default_bucket_dir
     77         cp_flag = '-R' if os.path.isdir(data) else ''
     78         simple_system(self.upload_cmd % (cp_flag, data, bucket_dir))
     79         msg = '\nGesture event files have been uploaded to "%s"\n'
     80         data_dir = os.path.basename(data)
     81         print msg % os.path.join(self.bucket, bucket_dir, data_dir)
     82 
     83     def rm(self, single_file):
     84         """Remove single_file."""
     85         simple_system(self.rm_cmd % single_file)
     86 
     87     def rmdir(self, data_dir):
     88         """Remove all files in the data directory."""
     89         simple_system(self.rm_cmd % os.path.join(data_dir, '*'))
     90