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