Josip Sokcevic | fb12b3f | 2021-04-19 18:09:50 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 2 | # Copyright 2014 The Chromium 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 | """Run a pinned gsutil.""" |
| 7 | |
| 8 | |
| 9 | import argparse |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 10 | import base64 |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 11 | import contextlib |
primiano@chromium.org | df35176 | 2014-12-18 11:12:34 +0000 | [diff] [blame] | 12 | import hashlib |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 13 | import json |
primiano@chromium.org | df35176 | 2014-12-18 11:12:34 +0000 | [diff] [blame] | 14 | import os |
| 15 | import shutil |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 16 | import subprocess |
primiano@chromium.org | df35176 | 2014-12-18 11:12:34 +0000 | [diff] [blame] | 17 | import sys |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 18 | import tempfile |
| 19 | import time |
Raul Tambre | b946b23 | 2019-03-26 14:48:46 +0000 | [diff] [blame] | 20 | |
| 21 | try: |
| 22 | import urllib2 as urllib |
| 23 | except ImportError: # For Py3 compatibility |
| 24 | import urllib.request as urllib |
| 25 | |
primiano@chromium.org | df35176 | 2014-12-18 11:12:34 +0000 | [diff] [blame] | 26 | import zipfile |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | GSUTIL_URL = 'https://storage.googleapis.com/pub/' |
| 30 | API_URL = 'https://www.googleapis.com/storage/v1/b/pub/o/' |
| 31 | |
| 32 | THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 33 | DEFAULT_BIN_DIR = os.path.join(THIS_DIR, 'external_bin', 'gsutil') |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 34 | |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 35 | IS_WINDOWS = os.name == 'nt' |
| 36 | |
Josip Sokcevic | 1909696 | 2022-03-10 17:56:09 +0000 | [diff] [blame] | 37 | VERSION = '4.68' |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 38 | |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 39 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 40 | class InvalidGsutilError(Exception): |
| 41 | pass |
| 42 | |
| 43 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 44 | def download_gsutil(version, target_dir): |
| 45 | """Downloads gsutil into the target_dir.""" |
| 46 | filename = 'gsutil_%s.zip' % version |
| 47 | target_filename = os.path.join(target_dir, filename) |
| 48 | |
| 49 | # Check if the target exists already. |
| 50 | if os.path.exists(target_filename): |
| 51 | md5_calc = hashlib.md5() |
| 52 | with open(target_filename, 'rb') as f: |
| 53 | while True: |
| 54 | buf = f.read(4096) |
| 55 | if not buf: |
| 56 | break |
| 57 | md5_calc.update(buf) |
| 58 | local_md5 = md5_calc.hexdigest() |
| 59 | |
| 60 | metadata_url = '%s%s' % (API_URL, filename) |
Raul Tambre | b946b23 | 2019-03-26 14:48:46 +0000 | [diff] [blame] | 61 | metadata = json.load(urllib.urlopen(metadata_url)) |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame] | 62 | remote_md5 = base64.b64decode(metadata['md5Hash']).decode('utf-8') |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 63 | |
| 64 | if local_md5 == remote_md5: |
| 65 | return target_filename |
| 66 | os.remove(target_filename) |
| 67 | |
| 68 | # Do the download. |
| 69 | url = '%s%s' % (GSUTIL_URL, filename) |
Raul Tambre | b946b23 | 2019-03-26 14:48:46 +0000 | [diff] [blame] | 70 | u = urllib.urlopen(url) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 71 | with open(target_filename, 'wb') as f: |
| 72 | while True: |
| 73 | buf = u.read(4096) |
| 74 | if not buf: |
| 75 | break |
| 76 | f.write(buf) |
| 77 | return target_filename |
| 78 | |
| 79 | |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 80 | @contextlib.contextmanager |
| 81 | def temporary_directory(base): |
Takuto Ikuta | 8daf244 | 2021-10-28 05:51:18 +0000 | [diff] [blame] | 82 | tmpdir = tempfile.mkdtemp(prefix='t', dir=base) |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 83 | try: |
| 84 | yield tmpdir |
| 85 | finally: |
| 86 | if os.path.isdir(tmpdir): |
| 87 | shutil.rmtree(tmpdir) |
| 88 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 89 | |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 90 | def ensure_gsutil(version, target, clean): |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 91 | bin_dir = os.path.join(target, 'gsutil_%s' % version) |
| 92 | gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil') |
Ryan Tseng | 83fd81f | 2017-10-23 11:13:48 -0700 | [diff] [blame] | 93 | gsutil_flag = os.path.join(bin_dir, 'gsutil', 'install.flag') |
| 94 | # We assume that if gsutil_flag exists, then we have a good version |
| 95 | # of the gsutil package. |
| 96 | if not clean and os.path.isfile(gsutil_flag): |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 97 | # Everything is awesome! we're all done here. |
| 98 | return gsutil_bin |
| 99 | |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 100 | if not os.path.exists(target): |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 101 | try: |
| 102 | os.makedirs(target) |
| 103 | except FileExistsError: |
| 104 | # Another process is prepping workspace, so let's check if gsutil_bin is |
| 105 | # present. If after several checks it's still not, continue with |
| 106 | # downloading gsutil. |
| 107 | delay = 2 # base delay, in seconds |
| 108 | for _ in range(3): # make N attempts |
| 109 | # sleep first as it's not expected to have file ready just yet. |
| 110 | time.sleep(delay) |
| 111 | delay *= 1.5 # next delay increased by that factor |
| 112 | if os.path.isfile(gsutil_bin): |
| 113 | return gsutil_bin |
| 114 | |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 115 | with temporary_directory(target) as instance_dir: |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 116 | # Clean up if we're redownloading a corrupted gsutil. |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 117 | cleanup_path = os.path.join(instance_dir, 'clean') |
| 118 | try: |
| 119 | os.rename(bin_dir, cleanup_path) |
| 120 | except (OSError, IOError): |
| 121 | cleanup_path = None |
| 122 | if cleanup_path: |
| 123 | shutil.rmtree(cleanup_path) |
| 124 | |
Takuto Ikuta | 8daf244 | 2021-10-28 05:51:18 +0000 | [diff] [blame] | 125 | download_dir = os.path.join(instance_dir, 'd') |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 126 | target_zip_filename = download_gsutil(version, instance_dir) |
| 127 | with zipfile.ZipFile(target_zip_filename, 'r') as target_zip: |
| 128 | target_zip.extractall(download_dir) |
| 129 | |
Joanna Wang | 07d6e69 | 2022-09-08 01:49:38 +0000 | [diff] [blame] | 130 | shutil.move(download_dir, bin_dir) |
Ryan Tseng | 83fd81f | 2017-10-23 11:13:48 -0700 | [diff] [blame] | 131 | # Final check that the gsutil bin exists. This should never fail. |
| 132 | if not os.path.isfile(gsutil_bin): |
| 133 | raise InvalidGsutilError() |
| 134 | # Drop a flag file. |
| 135 | with open(gsutil_flag, 'w') as f: |
| 136 | f.write('This flag file is dropped by gsutil.py') |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 137 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 138 | return gsutil_bin |
| 139 | |
| 140 | |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 141 | def run_gsutil(target, args, clean=False): |
| 142 | gsutil_bin = ensure_gsutil(VERSION, target, clean) |
| 143 | args_opt = ['-o', 'GSUtil:software_update_check_period=0'] |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 144 | |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 145 | if sys.platform == 'darwin': |
| 146 | # We are experiencing problems with multiprocessing on MacOS where gsutil.py |
| 147 | # may hang. |
| 148 | # This behavior is documented in gsutil codebase, and recommendation is to |
| 149 | # set GSUtil:parallel_process_count=1. |
| 150 | # https://github.com/GoogleCloudPlatform/gsutil/blob/06efc9dc23719fab4fd5fadb506d252bbd3fe0dd/gslib/command.py#L1331 |
| 151 | # https://github.com/GoogleCloudPlatform/gsutil/issues/1100 |
| 152 | args_opt.extend(['-o', 'GSUtil:parallel_process_count=1']) |
Chris Nardi | ab816ce | 2017-10-31 15:45:05 -0400 | [diff] [blame] | 153 | if sys.platform == 'cygwin': |
| 154 | # This script requires Windows Python, so invoke with depot_tools' |
| 155 | # Python. |
| 156 | def winpath(path): |
Edward Lesmes | 94d6f48 | 2019-11-04 20:55:09 +0000 | [diff] [blame] | 157 | stdout = subprocess.check_output(['cygpath', '-w', path]) |
| 158 | return stdout.strip().decode('utf-8', 'replace') |
Chris Nardi | ab816ce | 2017-10-31 15:45:05 -0400 | [diff] [blame] | 159 | cmd = ['python.bat', winpath(__file__)] |
| 160 | cmd.extend(args) |
| 161 | sys.exit(subprocess.call(cmd)) |
| 162 | assert sys.platform != 'cygwin' |
| 163 | |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 164 | cmd = [ |
Josip Sokcevic | 1909696 | 2022-03-10 17:56:09 +0000 | [diff] [blame] | 165 | 'vpython3', |
| 166 | '-vpython-spec', os.path.join(THIS_DIR, 'gsutil.vpython3'), |
Gavin Mak | 37db69d | 2022-03-10 00:54:39 +0000 | [diff] [blame] | 167 | '--', |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 168 | gsutil_bin |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 169 | ] + args_opt + args |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 170 | return subprocess.call(cmd, shell=IS_WINDOWS) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 171 | |
| 172 | |
| 173 | def parse_args(): |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 174 | bin_dir = os.environ.get('DEPOT_TOOLS_GSUTIL_BIN_DIR', DEFAULT_BIN_DIR) |
| 175 | |
Josip Sokcevic | c1fd44b | 2021-09-20 22:31:37 +0000 | [diff] [blame] | 176 | # Help is disabled as it conflicts with gsutil -h, which controls headers. |
| 177 | parser = argparse.ArgumentParser(add_help=False) |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 178 | |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 179 | parser.add_argument('--clean', action='store_true', |
| 180 | help='Clear any existing gsutil package, forcing a new download.') |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 181 | parser.add_argument('--target', default=bin_dir, |
| 182 | help='The target directory to download/store a gsutil version in. ' |
| 183 | '(default is %(default)s).') |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 184 | |
| 185 | # These two args exist for backwards-compatibility but are no-ops. |
| 186 | parser.add_argument('--force-version', default=VERSION, |
| 187 | help='(deprecated, this flag has no effect)') |
| 188 | parser.add_argument('--fallback', |
| 189 | help='(deprecated, this flag has no effect)') |
| 190 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 191 | parser.add_argument('args', nargs=argparse.REMAINDER) |
| 192 | |
hinoka@chromium.org | c13b054 | 2014-12-18 01:06:20 +0000 | [diff] [blame] | 193 | args, extras = parser.parse_known_args() |
| 194 | if args.args and args.args[0] == '--': |
| 195 | args.args.pop(0) |
| 196 | if extras: |
| 197 | args.args = extras + args.args |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 198 | return args |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 199 | |
| 200 | |
| 201 | def main(): |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 202 | args = parse_args() |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 203 | return run_gsutil(args.target, args.args, clean=args.clean) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 204 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 205 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 206 | if __name__ == '__main__': |
| 207 | sys.exit(main()) |