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.
12 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
15 from common import DIR_SRC_ROOT
17 sys.path.append(os.path.join(DIR_SRC_ROOT, 'build'))
18 import find_depot_tools
21 def DownloadAndUnpackFromCloudStorage(url, output_dir):
22 """Fetches a tarball from GCS and uncompresses it to |output_dir|."""
24 # Pass the compressed stream directly to 'tarfile'; don't bother writing it
26 tmp_file = 'image.tgz'
27 with tempfile.TemporaryDirectory() as tmp_d:
28 tmp_file_location = os.path.join(tmp_d, tmp_file)
31 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gsutil.py'), 'cp', url,
35 logging.debug('Running "%s"', ' '.join(cmd))
36 task = subprocess.run(cmd,
37 stderr=subprocess.PIPE,
38 stdout=subprocess.PIPE,
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(
50 'Failed to read a tarfile from gsutil.py.\n{}'.format(
51 stderr_data if stderr_data else '')) from exc