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') |
Josip Sokcevic | ff5a286 | 2021-07-12 16:41:13 +0000 | [diff] [blame] | 34 | DEFAULT_FALLBACK_GSUTIL = os.path.join( |
| 35 | THIS_DIR, 'third_party', 'gsutil', 'gsutil') |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 36 | |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 37 | IS_WINDOWS = os.name == 'nt' |
| 38 | |
| 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): |
| 82 | tmpdir = tempfile.mkdtemp(prefix='gsutil_py', dir=base) |
| 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 | ff5a286 | 2021-07-12 16:41:13 +0000 | [diff] [blame] | 101 | os.makedirs(target) |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 102 | with temporary_directory(target) as instance_dir: |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 103 | # Clean up if we're redownloading a corrupted gsutil. |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 104 | cleanup_path = os.path.join(instance_dir, 'clean') |
| 105 | try: |
| 106 | os.rename(bin_dir, cleanup_path) |
| 107 | except (OSError, IOError): |
| 108 | cleanup_path = None |
| 109 | if cleanup_path: |
| 110 | shutil.rmtree(cleanup_path) |
| 111 | |
| 112 | download_dir = os.path.join(instance_dir, 'download') |
| 113 | target_zip_filename = download_gsutil(version, instance_dir) |
| 114 | with zipfile.ZipFile(target_zip_filename, 'r') as target_zip: |
| 115 | target_zip.extractall(download_dir) |
| 116 | |
| 117 | try: |
| 118 | os.rename(download_dir, bin_dir) |
| 119 | except (OSError, IOError): |
| 120 | # Something else did this in parallel. |
| 121 | pass |
Ryan Tseng | 83fd81f | 2017-10-23 11:13:48 -0700 | [diff] [blame] | 122 | # Final check that the gsutil bin exists. This should never fail. |
| 123 | if not os.path.isfile(gsutil_bin): |
| 124 | raise InvalidGsutilError() |
| 125 | # Drop a flag file. |
| 126 | with open(gsutil_flag, 'w') as f: |
| 127 | f.write('This flag file is dropped by gsutil.py') |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 128 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 129 | return gsutil_bin |
| 130 | |
| 131 | |
Josip Sokcevic | ff5a286 | 2021-07-12 16:41:13 +0000 | [diff] [blame] | 132 | def run_gsutil(force_version, fallback, target, args, clean=False): |
| 133 | if force_version: |
| 134 | gsutil_bin = ensure_gsutil(force_version, target, clean) |
| 135 | else: |
| 136 | gsutil_bin = fallback |
| 137 | disable_update = ['-o', 'GSUtil:software_update_check_period=0'] |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 138 | |
Chris Nardi | ab816ce | 2017-10-31 15:45:05 -0400 | [diff] [blame] | 139 | if sys.platform == 'cygwin': |
| 140 | # This script requires Windows Python, so invoke with depot_tools' |
| 141 | # Python. |
| 142 | def winpath(path): |
Edward Lesmes | 94d6f48 | 2019-11-04 20:55:09 +0000 | [diff] [blame] | 143 | stdout = subprocess.check_output(['cygpath', '-w', path]) |
| 144 | return stdout.strip().decode('utf-8', 'replace') |
Chris Nardi | ab816ce | 2017-10-31 15:45:05 -0400 | [diff] [blame] | 145 | cmd = ['python.bat', winpath(__file__)] |
| 146 | cmd.extend(args) |
| 147 | sys.exit(subprocess.call(cmd)) |
| 148 | assert sys.platform != 'cygwin' |
| 149 | |
Josip Sokcevic | ff5a286 | 2021-07-12 16:41:13 +0000 | [diff] [blame] | 150 | # Run "gsutil" through "vpython". We need to do this because on GCE instances, |
| 151 | # expectations are made about Python having access to "google-compute-engine" |
| 152 | # and "boto" packages that are not met with non-system Python (e.g., bundles). |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 153 | cmd = [ |
Josip Sokcevic | ff5a286 | 2021-07-12 16:41:13 +0000 | [diff] [blame] | 154 | 'vpython', |
| 155 | '-vpython-spec', os.path.join(THIS_DIR, 'gsutil.vpython'), |
| 156 | '--', |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 157 | gsutil_bin |
Josip Sokcevic | ff5a286 | 2021-07-12 16:41:13 +0000 | [diff] [blame] | 158 | ] + disable_update + args |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 159 | return subprocess.call(cmd, shell=IS_WINDOWS) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 160 | |
| 161 | |
| 162 | def parse_args(): |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 163 | bin_dir = os.environ.get('DEPOT_TOOLS_GSUTIL_BIN_DIR', DEFAULT_BIN_DIR) |
| 164 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 165 | parser = argparse.ArgumentParser() |
Josip Sokcevic | ff5a286 | 2021-07-12 16:41:13 +0000 | [diff] [blame] | 166 | parser.add_argument('--force-version', default='4.30') |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 167 | parser.add_argument('--clean', action='store_true', |
| 168 | help='Clear any existing gsutil package, forcing a new download.') |
Josip Sokcevic | ff5a286 | 2021-07-12 16:41:13 +0000 | [diff] [blame] | 169 | parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL) |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 170 | parser.add_argument('--target', default=bin_dir, |
| 171 | help='The target directory to download/store a gsutil version in. ' |
| 172 | '(default is %(default)s).') |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 173 | parser.add_argument('args', nargs=argparse.REMAINDER) |
| 174 | |
hinoka@chromium.org | c13b054 | 2014-12-18 01:06:20 +0000 | [diff] [blame] | 175 | args, extras = parser.parse_known_args() |
| 176 | if args.args and args.args[0] == '--': |
| 177 | args.args.pop(0) |
| 178 | if extras: |
| 179 | args.args = extras + args.args |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 180 | return args |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | def main(): |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 184 | args = parse_args() |
Josip Sokcevic | ff5a286 | 2021-07-12 16:41:13 +0000 | [diff] [blame] | 185 | return run_gsutil(args.force_version, args.fallback, args.target, args.args, |
| 186 | clean=args.clean) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 187 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 188 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 189 | if __name__ == '__main__': |
| 190 | sys.exit(main()) |