xref: /aosp_15_r20/external/cronet/build/fuchsia/gcs_download.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2022 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import os
7import subprocess
8import sys
9import tarfile
10import tempfile
11
12sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
13                                             'test')))
14
15from common import DIR_SRC_ROOT
16
17sys.path.append(os.path.join(DIR_SRC_ROOT, 'build'))
18import find_depot_tools
19
20
21def DownloadAndUnpackFromCloudStorage(url, output_dir):
22  """Fetches a tarball from GCS and uncompresses it to |output_dir|."""
23
24  # Pass the compressed stream directly to 'tarfile'; don't bother writing it
25  # to disk first.
26  tmp_file = 'image.tgz'
27  with tempfile.TemporaryDirectory() as tmp_d:
28    tmp_file_location = os.path.join(tmp_d, tmp_file)
29    cmd = [
30        sys.executable,
31        os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gsutil.py'), 'cp', url,
32        tmp_file_location
33    ]
34
35    logging.debug('Running "%s"', ' '.join(cmd))
36    task = subprocess.run(cmd,
37                          stderr=subprocess.PIPE,
38                          stdout=subprocess.PIPE,
39                          check=True,
40                          encoding='utf-8')
41
42    try:
43      tarfile.open(name=tmp_file_location,
44                   mode='r|gz').extractall(path=output_dir)
45    except tarfile.ReadError as exc:
46      _, stderr_data = task.communicate()
47      stderr_data = stderr_data.decode()
48      raise subprocess.CalledProcessError(
49          task.returncode, cmd,
50          'Failed to read a tarfile from gsutil.py.\n{}'.format(
51              stderr_data if stderr_data else '')) from exc
52