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. |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 5 | """Run a pinned gsutil.""" |
| 6 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 7 | import argparse |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 8 | import base64 |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 9 | import contextlib |
primiano@chromium.org | df35176 | 2014-12-18 11:12:34 +0000 | [diff] [blame] | 10 | import hashlib |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 11 | import json |
primiano@chromium.org | df35176 | 2014-12-18 11:12:34 +0000 | [diff] [blame] | 12 | import os |
| 13 | import shutil |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 14 | import subprocess |
primiano@chromium.org | df35176 | 2014-12-18 11:12:34 +0000 | [diff] [blame] | 15 | import sys |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 16 | import tempfile |
| 17 | import time |
Gavin Mak | 1c1cc06 | 2023-08-30 15:33:44 +0000 | [diff] [blame] | 18 | import urllib.request |
Raul Tambre | b946b23 | 2019-03-26 14:48:46 +0000 | [diff] [blame] | 19 | |
primiano@chromium.org | df35176 | 2014-12-18 11:12:34 +0000 | [diff] [blame] | 20 | import zipfile |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 21 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 22 | GSUTIL_URL = 'https://storage.googleapis.com/pub/' |
| 23 | API_URL = 'https://www.googleapis.com/storage/v1/b/pub/o/' |
| 24 | |
| 25 | THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 26 | DEFAULT_BIN_DIR = os.path.join(THIS_DIR, 'external_bin', 'gsutil') |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 27 | |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 28 | IS_WINDOWS = os.name == 'nt' |
| 29 | |
Josip Sokcevic | 1909696 | 2022-03-10 17:56:09 +0000 | [diff] [blame] | 30 | VERSION = '4.68' |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 31 | |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 32 | # Google OAuth Context required by gsutil. |
| 33 | LUCI_AUTH_SCOPES = [ |
| 34 | 'https://www.googleapis.com/auth/devstorage.full_control', |
| 35 | 'https://www.googleapis.com/auth/userinfo.email', |
| 36 | ] |
| 37 | |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 38 | |
Aravind Vasudevan | a413ee7 | 2023-11-01 22:19:25 +0000 | [diff] [blame] | 39 | # Platforms unsupported by luci-auth. |
| 40 | LUCI_AUTH_UNSUPPORTED_PLATFORMS = ['aix'] |
| 41 | |
| 42 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 43 | class InvalidGsutilError(Exception): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 44 | pass |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 45 | |
| 46 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 47 | def download_gsutil(version, target_dir): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 48 | """Downloads gsutil into the target_dir.""" |
| 49 | filename = 'gsutil_%s.zip' % version |
| 50 | target_filename = os.path.join(target_dir, filename) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 51 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 52 | # Check if the target exists already. |
| 53 | if os.path.exists(target_filename): |
| 54 | md5_calc = hashlib.md5() |
| 55 | with open(target_filename, 'rb') as f: |
| 56 | while True: |
| 57 | buf = f.read(4096) |
| 58 | if not buf: |
| 59 | break |
| 60 | md5_calc.update(buf) |
| 61 | local_md5 = md5_calc.hexdigest() |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 62 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 63 | metadata_url = '%s%s' % (API_URL, filename) |
| 64 | metadata = json.load(urllib.request.urlopen(metadata_url)) |
| 65 | remote_md5 = base64.b64decode(metadata['md5Hash']).decode('utf-8') |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 66 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 67 | if local_md5 == remote_md5: |
| 68 | return target_filename |
| 69 | os.remove(target_filename) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 70 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 71 | # Do the download. |
| 72 | url = '%s%s' % (GSUTIL_URL, filename) |
| 73 | u = urllib.request.urlopen(url) |
| 74 | with open(target_filename, 'wb') as f: |
| 75 | while True: |
| 76 | buf = u.read(4096) |
| 77 | if not buf: |
| 78 | break |
| 79 | f.write(buf) |
| 80 | return target_filename |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 81 | |
| 82 | |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 83 | @contextlib.contextmanager |
| 84 | def temporary_directory(base): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 85 | tmpdir = tempfile.mkdtemp(prefix='t', dir=base) |
| 86 | try: |
| 87 | yield tmpdir |
| 88 | finally: |
| 89 | if os.path.isdir(tmpdir): |
| 90 | shutil.rmtree(tmpdir) |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 91 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 92 | |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 93 | def ensure_gsutil(version, target, clean): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 94 | bin_dir = os.path.join(target, 'gsutil_%s' % version) |
| 95 | gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil') |
| 96 | gsutil_flag = os.path.join(bin_dir, 'gsutil', 'install.flag') |
| 97 | # We assume that if gsutil_flag exists, then we have a good version |
| 98 | # of the gsutil package. |
| 99 | if not clean and os.path.isfile(gsutil_flag): |
| 100 | # Everything is awesome! we're all done here. |
| 101 | return gsutil_bin |
| 102 | |
| 103 | if not os.path.exists(target): |
| 104 | try: |
| 105 | os.makedirs(target) |
| 106 | except FileExistsError: |
| 107 | # Another process is prepping workspace, so let's check if |
| 108 | # gsutil_bin is present. If after several checks it's still not, |
| 109 | # continue with downloading gsutil. |
| 110 | delay = 2 # base delay, in seconds |
| 111 | for _ in range(3): # make N attempts |
| 112 | # sleep first as it's not expected to have file ready just yet. |
| 113 | time.sleep(delay) |
| 114 | delay *= 1.5 # next delay increased by that factor |
| 115 | if os.path.isfile(gsutil_bin): |
| 116 | return gsutil_bin |
| 117 | |
| 118 | with temporary_directory(target) as instance_dir: |
| 119 | # Clean up if we're redownloading a corrupted gsutil. |
| 120 | cleanup_path = os.path.join(instance_dir, 'clean') |
| 121 | try: |
| 122 | os.rename(bin_dir, cleanup_path) |
| 123 | except (OSError, IOError): |
| 124 | cleanup_path = None |
| 125 | if cleanup_path: |
| 126 | shutil.rmtree(cleanup_path) |
| 127 | |
| 128 | download_dir = os.path.join(instance_dir, 'd') |
| 129 | target_zip_filename = download_gsutil(version, instance_dir) |
| 130 | with zipfile.ZipFile(target_zip_filename, 'r') as target_zip: |
| 131 | target_zip.extractall(download_dir) |
| 132 | |
| 133 | shutil.move(download_dir, bin_dir) |
| 134 | # Final check that the gsutil bin exists. This should never fail. |
| 135 | if not os.path.isfile(gsutil_bin): |
| 136 | raise InvalidGsutilError() |
| 137 | # Drop a flag file. |
| 138 | with open(gsutil_flag, 'w') as f: |
| 139 | f.write('This flag file is dropped by gsutil.py') |
| 140 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 141 | return gsutil_bin |
| 142 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 143 | |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 144 | def _is_luci_context(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 145 | """Returns True if the script is run within luci-context""" |
| 146 | if os.getenv('SWARMING_HEADLESS') == '1': |
| 147 | return True |
Aravind Vasudevan | 3879bd8 | 2023-02-16 23:09:33 +0000 | [diff] [blame] | 148 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 149 | luci_context_env = os.getenv('LUCI_CONTEXT') |
| 150 | if not luci_context_env: |
| 151 | return False |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 152 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 153 | try: |
| 154 | with open(luci_context_env) as f: |
| 155 | luci_context_json = json.load(f) |
| 156 | return 'local_auth' in luci_context_json |
| 157 | except (ValueError, FileNotFoundError): |
| 158 | return False |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 159 | |
| 160 | |
Aravind Vasudevan | a413ee7 | 2023-11-01 22:19:25 +0000 | [diff] [blame] | 161 | def _is_luci_auth_supported_platform(): |
| 162 | """Returns True if luci-auth is supported in the current platform.""" |
| 163 | return not any(map(sys.platform.startswith, |
| 164 | LUCI_AUTH_UNSUPPORTED_PLATFORMS)) |
| 165 | |
| 166 | |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 167 | def luci_context(cmd): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 168 | """Helper to call`luci-auth context`.""" |
| 169 | p = _luci_auth_cmd('context', wrapped_cmds=cmd) |
Aravind Vasudevan | effdecd | 2023-01-30 17:02:17 +0000 | [diff] [blame] | 170 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 171 | # If luci-auth is not logged in, fallback to normal execution. |
| 172 | if b'Not logged in.' in p.stderr: |
| 173 | return _run_subprocess(cmd, interactive=True) |
Aravind Vasudevan | effdecd | 2023-01-30 17:02:17 +0000 | [diff] [blame] | 174 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 175 | _print_subprocess_result(p) |
| 176 | return p |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 177 | |
| 178 | |
| 179 | def luci_login(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 180 | """Helper to run `luci-auth login`.""" |
| 181 | # luci-auth requires interactive shell. |
| 182 | return _luci_auth_cmd('login', interactive=True) |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 183 | |
| 184 | |
Aravind Vasudevan | 1757677 | 2023-01-13 19:50:51 +0000 | [diff] [blame] | 185 | def _luci_auth_cmd(luci_cmd, wrapped_cmds=None, interactive=False): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 186 | """Helper to call luci-auth command.""" |
| 187 | cmd = ['luci-auth', luci_cmd, '-scopes', ' '.join(LUCI_AUTH_SCOPES)] |
| 188 | if wrapped_cmds: |
| 189 | cmd += ['--'] + wrapped_cmds |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 190 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 191 | return _run_subprocess(cmd, interactive) |
Aravind Vasudevan | b7d8efd | 2023-01-27 18:46:40 +0000 | [diff] [blame] | 192 | |
| 193 | |
Aravind Vasudevan | 1757677 | 2023-01-13 19:50:51 +0000 | [diff] [blame] | 194 | def _run_subprocess(cmd, interactive=False, env=None): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 195 | """Wrapper to run the given command within a subprocess.""" |
| 196 | kwargs = {'shell': IS_WINDOWS} |
Aravind Vasudevan | 1757677 | 2023-01-13 19:50:51 +0000 | [diff] [blame] | 197 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 198 | if env: |
| 199 | kwargs['env'] = dict(os.environ, **env) |
Aravind Vasudevan | 1757677 | 2023-01-13 19:50:51 +0000 | [diff] [blame] | 200 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 201 | if not interactive: |
| 202 | kwargs['stdout'] = subprocess.PIPE |
| 203 | kwargs['stderr'] = subprocess.PIPE |
Aravind Vasudevan | 1757677 | 2023-01-13 19:50:51 +0000 | [diff] [blame] | 204 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 205 | return subprocess.run(cmd, **kwargs) |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 206 | |
| 207 | |
Aravind Vasudevan | 3879bd8 | 2023-02-16 23:09:33 +0000 | [diff] [blame] | 208 | def _print_subprocess_result(p): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 209 | """Prints the subprocess result to stdout & stderr.""" |
| 210 | if p.stdout: |
| 211 | sys.stdout.buffer.write(p.stdout) |
Aravind Vasudevan | 3879bd8 | 2023-02-16 23:09:33 +0000 | [diff] [blame] | 212 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 213 | if p.stderr: |
| 214 | sys.stderr.buffer.write(p.stderr) |
Aravind Vasudevan | 3879bd8 | 2023-02-16 23:09:33 +0000 | [diff] [blame] | 215 | |
| 216 | |
Aravind Vasudevan | 7b1f98e | 2023-01-18 17:34:15 +0000 | [diff] [blame] | 217 | def is_boto_present(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 218 | """Returns true if the .boto file is present in the default path.""" |
| 219 | return os.getenv('BOTO_CONFIG') or os.getenv( |
| 220 | 'AWS_CREDENTIAL_FILE') or os.path.isfile( |
| 221 | os.path.join(os.path.expanduser('~'), '.boto')) |
Aravind Vasudevan | 7b1f98e | 2023-01-18 17:34:15 +0000 | [diff] [blame] | 222 | |
| 223 | |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 224 | def run_gsutil(target, args, clean=False): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 225 | # Redirect gsutil config calls to luci-auth. |
| 226 | if 'config' in args: |
| 227 | return luci_login().returncode |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 228 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 229 | gsutil_bin = ensure_gsutil(VERSION, target, clean) |
| 230 | args_opt = ['-o', 'GSUtil:software_update_check_period=0'] |
Dan Jacques | 509776e | 2017-09-07 18:01:08 -0700 | [diff] [blame] | 231 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 232 | if sys.platform == 'darwin': |
| 233 | # We are experiencing problems with multiprocessing on MacOS where |
| 234 | # gsutil.py may hang. This behavior is documented in gsutil codebase, |
| 235 | # and recommendation is to set GSUtil:parallel_process_count=1. |
| 236 | # https://github.com/GoogleCloudPlatform/gsutil/blob/06efc9dc23719fab4fd5fadb506d252bbd3fe0dd/gslib/command.py#L1331 |
| 237 | # https://github.com/GoogleCloudPlatform/gsutil/issues/1100 |
| 238 | args_opt.extend(['-o', 'GSUtil:parallel_process_count=1']) |
| 239 | if sys.platform == 'cygwin': |
| 240 | # This script requires Windows Python, so invoke with depot_tools' |
| 241 | # Python. |
| 242 | def winpath(path): |
| 243 | stdout = subprocess.check_output(['cygpath', '-w', path]) |
| 244 | return stdout.strip().decode('utf-8', 'replace') |
Chris Nardi | ab816ce | 2017-10-31 15:45:05 -0400 | [diff] [blame] | 245 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 246 | cmd = ['python.bat', winpath(__file__)] |
| 247 | cmd.extend(args) |
| 248 | sys.exit(subprocess.call(cmd)) |
| 249 | assert sys.platform != 'cygwin' |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 250 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 251 | cmd = [ |
| 252 | 'vpython3', '-vpython-spec', |
| 253 | os.path.join(THIS_DIR, 'gsutil.vpython3'), '--', gsutil_bin |
| 254 | ] + args_opt + args |
Aravind Vasudevan | 3879bd8 | 2023-02-16 23:09:33 +0000 | [diff] [blame] | 255 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 256 | # When .boto is present, try without additional wrappers and handle specific |
| 257 | # errors. |
| 258 | if is_boto_present(): |
| 259 | p = _run_subprocess(cmd) |
Aravind Vasudevan | 3879bd8 | 2023-02-16 23:09:33 +0000 | [diff] [blame] | 260 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 261 | # Notify user that their .boto file might be outdated. |
| 262 | if b'Your credentials are invalid.' in p.stderr: |
| 263 | # Make sure this error message is visible when invoked by gclient |
| 264 | # runhooks |
| 265 | separator = '*' * 80 |
| 266 | print( |
| 267 | '\n' + separator + '\n' + |
| 268 | 'Warning: You might have an outdated .boto file. If this issue ' |
| 269 | 'persists after running `gsutil.py config`, try removing your ' |
| 270 | '.boto, usually located in your home directory.\n' + separator + |
| 271 | '\n', |
| 272 | file=sys.stderr) |
Aravind Vasudevan | 3879bd8 | 2023-02-16 23:09:33 +0000 | [diff] [blame] | 273 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 274 | _print_subprocess_result(p) |
| 275 | return p.returncode |
Aravind Vasudevan | 7af6169 | 2023-01-09 23:15:15 +0000 | [diff] [blame] | 276 | |
Aravind Vasudevan | a413ee7 | 2023-11-01 22:19:25 +0000 | [diff] [blame] | 277 | # Skip wrapping commands if luci-auth is already being used or if the |
| 278 | # platform is unsupported by luci-auth. |
| 279 | if _is_luci_context() or not _is_luci_auth_supported_platform(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 280 | return _run_subprocess(cmd, interactive=True).returncode |
| 281 | |
| 282 | # Wrap gsutil with luci-auth context. |
| 283 | return luci_context(cmd).returncode |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 284 | |
| 285 | |
| 286 | def parse_args(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 287 | bin_dir = os.environ.get('DEPOT_TOOLS_GSUTIL_BIN_DIR', DEFAULT_BIN_DIR) |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 288 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 289 | # Help is disabled as it conflicts with gsutil -h, which controls headers. |
| 290 | parser = argparse.ArgumentParser(add_help=False) |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 291 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 292 | parser.add_argument( |
| 293 | '--clean', |
| 294 | action='store_true', |
| 295 | help='Clear any existing gsutil package, forcing a new download.') |
| 296 | parser.add_argument( |
| 297 | '--target', |
| 298 | default=bin_dir, |
| 299 | help='The target directory to download/store a gsutil version in. ' |
| 300 | '(default is %(default)s).') |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 301 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 302 | # These two args exist for backwards-compatibility but are no-ops. |
| 303 | parser.add_argument('--force-version', |
| 304 | default=VERSION, |
| 305 | help='(deprecated, this flag has no effect)') |
| 306 | parser.add_argument('--fallback', |
| 307 | help='(deprecated, this flag has no effect)') |
Josip Sokcevic | fa474e8 | 2021-09-17 16:59:49 +0000 | [diff] [blame] | 308 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 309 | parser.add_argument('args', nargs=argparse.REMAINDER) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 310 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 311 | args, extras = parser.parse_known_args() |
| 312 | if args.args and args.args[0] == '--': |
| 313 | args.args.pop(0) |
| 314 | if extras: |
| 315 | args.args = extras + args.args |
| 316 | return args |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | def main(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 320 | args = parse_args() |
| 321 | return run_gsutil(args.target, args.args, clean=args.clean) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 322 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 323 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 324 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 325 | sys.exit(main()) |