1# Lint as: python2, python3 2# Copyright 2015 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6 7from __future__ import absolute_import 8from __future__ import division 9from __future__ import print_function 10import logging 11import os.path 12import subprocess 13import tempfile 14 15def copy_private_bucket(host, bucket, filename, destination, timeout_s=30): 16 """ 17 Copies files/directories from a private google storage location to the DUT. 18 Uses a test server box as a temp location. 19 We do this because it's easier than trying to get the client DUT 20 authenticated. The Test server is already authenticated, so copy to the test 21 server and then send file to client. 22 23 @param host: Autotest host machine object. 24 @param bucket: path to name of gs bucket. 25 @param filename: string, name of the file or dir in 'bucket' to copy. 26 @param destination: path in DUT where the file should be copied to. 27 @param timeout_s: int, timeout in seconds to wait for copy to finish 28 29 """ 30 31 assert (bucket.startswith('gs://')) 32 33 src = os.path.join(bucket, filename) 34 35 log("SOURCE path: " + src) 36 37 with tempfile.NamedTemporaryFile(suffix='.wpr') as tempsource: 38 tempsourcepath = tempsource.name 39 40 args = ['gsutil', 'cp', src, tempsourcepath] 41 log("Copying to temporary test server destination : " + tempsourcepath) 42 43 p = subprocess.Popen(args, 44 stdout=subprocess.PIPE, 45 stderr=subprocess.PIPE) 46 47 output = p.communicate() 48 49 log("STDOUT | " + output[0].decode('utf-8')) 50 log("STDERR | " + output[1].decode('utf-8')) 51 52 if p.returncode: 53 raise subprocess.CalledProcessError(returncode=p.returncode, 54 cmd=args) 55 56 host.send_file(tempsourcepath, os.path.join(destination, filename)) 57 log("Sent file to DUT : " + host.hostname) 58 59 60def log(message): 61 """ 62 Wraps around logging.debug() and adds a prefix to show that messages are 63 coming from this utility. 64 65 @param message: string, the message to log. 66 67 """ 68 message = "| gs wrapper | " + message 69 logging.debug(message)