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