summaryrefslogtreecommitdiff
path: root/harnesses/host_controller/utils/gcp/gcs_utils.py
blob: ffe0913056bd34fe253a26b427eb18c6c55f058a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import logging

from vts.utils.python.common import cmd_utils


def GetGsutilPath():
    """Finds gsutil in PATH.

    Instead of a Python library, gsutil binary is used to avoid packaging GCS
    PIP package as part of VTS HC (Host Controller).

    Returns:
        The gsutil file path if found; None otherwise.
    """
    sh_stdout, sh_stderr, ret_code = cmd_utils.ExecuteOneShellCommand(
        "which gsutil")
    if ret_code == 0:
        return sh_stdout.strip()
    else:
        logging.fatal("`gsutil` doesn't exist on the host; "
                      "please install Google Cloud SDK before retrying.")
        return None


def IsGcsFile(gsutil_path, url):
    """Checks whether a given path is for a GCS file.

    Args:
        gsutil_path: string, the path of a gsutil binary.
        url: string, the GCS URL. e.g., gs://<bucket>/<file>.

    Returns:
        True if url is a file, False otherwise.
    """
    check_command = "%s stat %s" % (gsutil_path, url)
    _, _, ret_code = cmd_utils.ExecuteOneShellCommand(check_command)
    return ret_code == 0


def Copy(gsutil_path, src_urls, dst_url):
    """Copies files between local file system and GCS.

    Args:
        gsutil_path: string, the path of a gsutil binary.
        src_urls: string, the source paths or GCS URLs separated by spaces.
        dst_url: string, the destination path or GCS URL.

    Returns:
        True if the command succeeded, False otherwise.
    """
    copy_command = "%s cp %s %s" % (gsutil_path, src_urls, dst_url)
    _, _, ret_code = cmd_utils.ExecuteOneShellCommand(copy_command)
    return ret_code == 0


def List(gsutil_path, url):
    """Lists a directory or file on GCS.

    Args:
        gsutil_path: string, the path of a gsutil binary.
        url: string, the GCS URL of the directory or file.

    Returns:
        list of strings, the GCS URLs of the listed files.
    """
    ls_command = "%s ls %s" % (gsutil_path, url)
    stdout, _, ret_code = cmd_utils.ExecuteOneShellCommand(ls_command)
    return stdout.strip("\n").split("\n") if ret_code == 0 else []


def Remove(gsutil_path, url, recursive=False):
    """Removes a directory or file on GCS.

    Args:
        gsutil_path: string, the path of a gsutil binary.
        url: string, the GCS URL of the directory or file.
        recursive: boolean, whether to remove the directory recursively.

    Returns:
        True if the command succeeded, False otherwise.
    """
    if "/" not in url.lstrip("gs://").rstrip("/"):
        logging.error("Cannot remove bucket %s", url)
        return False
    rm_command = "%s rm -f%s %s" % (
        gsutil_path, ("r" if recursive else ""), url)
    ret_code = cmd_utils.ExecuteOneShellCommand(rm_command)
    return ret_code == 0