Flutter Engine
The Flutter Engine
download_fuchsia_sdk.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright 2013 The Flutter Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# The return code of this script will always be 0, even if there is an error,
7# unless the --fail-loudly flag is passed.
8
9import argparse
10import tarfile
11import json
12import os
13import shutil
14import subprocess
15import sys
16
17SRC_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
18FUCHSIA_SDK_DIR = os.path.join(SRC_ROOT, 'fuchsia', 'sdk')
19FLUTTER_DIR = os.path.join(SRC_ROOT, 'flutter')
20
21
22# Prints to stderr.
23def eprint(*args, **kwargs):
24 print(*args, file=sys.stderr, **kwargs)
25
26
27def FileNameForSdkPath(sdk_path):
28 return sdk_path.split('/')[-1]
29
30
31def DownloadFuchsiaSDKFromGCS(sdk_path, verbose):
32 file = FileNameForSdkPath(sdk_path)
33 url = 'https://storage.googleapis.com/fuchsia-artifacts/{}'.format(sdk_path)
34 dest = os.path.join(FUCHSIA_SDK_DIR, file)
35
36 if verbose:
37 print('Fuchsia SDK url: "%s"' % url)
38 print('Fuchsia SDK destination path: "%s"' % dest)
39
40 if os.path.isfile(dest):
41 os.unlink(dest)
42
43 # Ensure destination folder exists.
44 os.makedirs(FUCHSIA_SDK_DIR, exist_ok=True)
45 curl_command = [
46 'curl',
47 '--retry',
48 '3',
49 '--continue-at',
50 '-',
51 '--location',
52 '--output',
53 dest,
54 url,
55 ]
56 if verbose:
57 print('Running: "%s"' % (' '.join(curl_command)))
58 curl_result = subprocess.run(
59 curl_command,
60 stdout=subprocess.PIPE,
61 stderr=subprocess.PIPE,
62 universal_newlines=True,
63 )
64 if curl_result.returncode == 0 and verbose:
65 print('curl output:stdout:\n{}\nstderr:\n{}'.format(
66 curl_result.stdout,
67 curl_result.stderr,
68 ))
69 elif curl_result.returncode != 0:
70 eprint(
71 'Failed to download: stdout:\n{}\nstderr:\n{}'.format(
72 curl_result.stdout,
73 curl_result.stderr,
74 )
75 )
76 return None
77
78 return dest
79
80
81def OnErrorRmTree(func, path, exc_info):
82 """
83 Error handler for ``shutil.rmtree``.
84
85 If the error is due to an access error (read only file)
86 it attempts to add write permission and then retries.
87 If the error is for another reason it re-raises the error.
88
89 Usage : ``shutil.rmtree(path, onerror=onerror)``
90 """
91 import stat
92 # Is the error an access error?
93 if not os.access(path, os.W_OK):
94 os.chmod(path, stat.S_IWUSR)
95 func(path)
96 else:
97 raise
98
99
100def ExtractGzipArchive(archive, host_os, verbose):
101 sdk_dest = os.path.join(FUCHSIA_SDK_DIR, host_os)
102 if os.path.isdir(sdk_dest):
103 shutil.rmtree(sdk_dest, onerror=OnErrorRmTree)
104
105 extract_dest = os.path.join(FUCHSIA_SDK_DIR, 'temp')
106 if os.path.isdir(extract_dest):
107 shutil.rmtree(extract_dest, onerror=OnErrorRmTree)
108 os.makedirs(extract_dest, exist_ok=True)
109
110 if verbose:
111 print('Extracting "%s" to "%s"' % (archive, extract_dest))
112
113 with tarfile.open(archive, 'r') as z:
114 z.extractall(extract_dest)
115
116 shutil.move(extract_dest, sdk_dest)
117
118
119def Main():
120 parser = argparse.ArgumentParser()
121 parser.add_argument(
122 '--fail-loudly',
123 action='store_true',
124 default=False,
125 help="Return an error code if a prebuilt couldn't be fetched and extracted"
126 )
127
128 parser.add_argument(
129 '--verbose',
130 action='store_true',
131 default='LUCI_CONTEXT' in os.environ,
132 help='Emit verbose output'
133 )
134
135 parser.add_argument('--host-os', help='The host os')
136
137 parser.add_argument('--fuchsia-sdk-path', help='The path in gcs to the fuchsia sdk to download')
138
139 args = parser.parse_args()
140 fail_loudly = 1 if args.fail_loudly else 0
141 verbose = args.verbose
142 host_os = args.host_os
143 fuchsia_sdk_path = args.fuchsia_sdk_path
144
145 if fuchsia_sdk_path is None:
146 eprint('sdk_path can not be empty')
147 return fail_loudly
148
149 archive = DownloadFuchsiaSDKFromGCS(fuchsia_sdk_path, verbose)
150 if archive is None:
151 eprint('Failed to download SDK from %s' % fuchsia_sdk_path)
152 return fail_loudly
153
154 ExtractGzipArchive(archive, host_os, verbose)
155
156 success = True
157 return 0 if success else fail_loudly
158
159
160if __name__ == '__main__':
161 sys.exit(Main())
uint32_t uint32_t * format
def ExtractGzipArchive(archive, host_os, verbose)
def FileNameForSdkPath(sdk_path)
def eprint(*args, **kwargs)
def OnErrorRmTree(func, path, exc_info)
def DownloadFuchsiaSDKFromGCS(sdk_path, verbose)
def print(*args, **kwargs)
Definition: run_tests.py:49
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741