[M120 Migration] Implement ewk_view_is_video_playing api
[platform/framework/web/chromium-efl.git] / build / fuchsia / gcs_download.py
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
5 import logging
6 import os
7 import subprocess
8 import sys
9 import tarfile
10 import tempfile
11
12 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
13                                              'test')))
14
15 from common import DIR_SRC_ROOT
16
17 sys.path.append(os.path.join(DIR_SRC_ROOT, 'build'))
18 import find_depot_tools
19
20
21 def 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