[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 10 | import base64 |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 11 | import contextlib |
[email protected] | df35176 | 2014-12-18 11:12:34 | [diff] [blame] | 12 | import hashlib |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 13 | import json |
[email protected] | df35176 | 2014-12-18 11:12:34 | [diff] [blame] | 14 | import os |
| 15 | import shutil |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 16 | import subprocess |
[email protected] | df35176 | 2014-12-18 11:12:34 | [diff] [blame] | 17 | import sys |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 18 | import tempfile |
| 19 | import time |
[email protected] | df35176 | 2014-12-18 11:12:34 | [diff] [blame] | 20 | import urllib2 |
| 21 | import zipfile |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 22 | |
| 23 | |
| 24 | GSUTIL_URL = 'https://storage.googleapis.com/pub/' |
| 25 | API_URL = 'https://www.googleapis.com/storage/v1/b/pub/o/' |
| 26 | |
| 27 | THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 28 | DEFAULT_BIN_DIR = os.path.join(THIS_DIR, 'external_bin', 'gsutil') |
| 29 | DEFAULT_FALLBACK_GSUTIL = os.path.join( |
| 30 | THIS_DIR, 'third_party', 'gsutil', 'gsutil') |
| 31 | |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 32 | class InvalidGsutilError(Exception): |
| 33 | pass |
| 34 | |
| 35 | |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 36 | def download_gsutil(version, target_dir): |
| 37 | """Downloads gsutil into the target_dir.""" |
| 38 | filename = 'gsutil_%s.zip' % version |
| 39 | target_filename = os.path.join(target_dir, filename) |
| 40 | |
| 41 | # Check if the target exists already. |
| 42 | if os.path.exists(target_filename): |
| 43 | md5_calc = hashlib.md5() |
| 44 | with open(target_filename, 'rb') as f: |
| 45 | while True: |
| 46 | buf = f.read(4096) |
| 47 | if not buf: |
| 48 | break |
| 49 | md5_calc.update(buf) |
| 50 | local_md5 = md5_calc.hexdigest() |
| 51 | |
| 52 | metadata_url = '%s%s' % (API_URL, filename) |
[email protected] | df35176 | 2014-12-18 11:12:34 | [diff] [blame] | 53 | metadata = json.load(urllib2.urlopen(metadata_url)) |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 54 | remote_md5 = base64.b64decode(metadata['md5Hash']) |
| 55 | |
| 56 | if local_md5 == remote_md5: |
| 57 | return target_filename |
| 58 | os.remove(target_filename) |
| 59 | |
| 60 | # Do the download. |
| 61 | url = '%s%s' % (GSUTIL_URL, filename) |
[email protected] | df35176 | 2014-12-18 11:12:34 | [diff] [blame] | 62 | u = urllib2.urlopen(url) |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 63 | with open(target_filename, 'wb') as f: |
| 64 | while True: |
| 65 | buf = u.read(4096) |
| 66 | if not buf: |
| 67 | break |
| 68 | f.write(buf) |
| 69 | return target_filename |
| 70 | |
| 71 | |
| 72 | def check_gsutil(gsutil_bin): |
| 73 | """Run gsutil version and make sure it runs.""" |
[email protected] | 5e879a4 | 2015-01-24 00:55:46 | [diff] [blame] | 74 | return subprocess.call( |
| 75 | [sys.executable, gsutil_bin, 'version'], |
| 76 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) == 0 |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 77 | |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 78 | @contextlib.contextmanager |
| 79 | def temporary_directory(base): |
| 80 | tmpdir = tempfile.mkdtemp(prefix='gsutil_py', dir=base) |
| 81 | try: |
| 82 | yield tmpdir |
| 83 | finally: |
| 84 | if os.path.isdir(tmpdir): |
| 85 | shutil.rmtree(tmpdir) |
| 86 | |
| 87 | def ensure_gsutil(version, target, clean): |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 88 | bin_dir = os.path.join(target, 'gsutil_%s' % version) |
| 89 | gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil') |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 90 | if not clean and os.path.isfile(gsutil_bin) and check_gsutil(gsutil_bin): |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 91 | # Everything is awesome! we're all done here. |
| 92 | return gsutil_bin |
| 93 | |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 94 | if not os.path.exists(target): |
| 95 | os.makedirs(target) |
| 96 | with temporary_directory(target) as instance_dir: |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 97 | # Clean up if we're redownloading a corrupted gsutil. |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 98 | cleanup_path = os.path.join(instance_dir, 'clean') |
| 99 | try: |
| 100 | os.rename(bin_dir, cleanup_path) |
| 101 | except (OSError, IOError): |
| 102 | cleanup_path = None |
| 103 | if cleanup_path: |
| 104 | shutil.rmtree(cleanup_path) |
| 105 | |
| 106 | download_dir = os.path.join(instance_dir, 'download') |
| 107 | target_zip_filename = download_gsutil(version, instance_dir) |
| 108 | with zipfile.ZipFile(target_zip_filename, 'r') as target_zip: |
| 109 | target_zip.extractall(download_dir) |
| 110 | |
| 111 | try: |
| 112 | os.rename(download_dir, bin_dir) |
| 113 | except (OSError, IOError): |
| 114 | # Something else did this in parallel. |
| 115 | pass |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 116 | |
| 117 | # Final check that the gsutil bin is okay. This should never fail. |
| 118 | if not check_gsutil(gsutil_bin): |
| 119 | raise InvalidGsutilError() |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 120 | return gsutil_bin |
| 121 | |
| 122 | |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 123 | def run_gsutil(force_version, fallback, target, args, clean=False): |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 124 | if force_version: |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 125 | gsutil_bin = ensure_gsutil(force_version, target, clean) |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 126 | else: |
| 127 | gsutil_bin = fallback |
[email protected] | fdb9ce3 | 2016-04-05 23:57:12 | [diff] [blame] | 128 | disable_update = ['-o', 'GSUtil:software_update_check_period=0'] |
| 129 | cmd = [sys.executable, gsutil_bin] + disable_update + args |
[email protected] | 5e879a4 | 2015-01-24 00:55:46 | [diff] [blame] | 130 | return subprocess.call(cmd) |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 131 | |
| 132 | |
| 133 | def parse_args(): |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 134 | bin_dir = os.environ.get('DEPOT_TOOLS_GSUTIL_BIN_DIR', DEFAULT_BIN_DIR) |
| 135 | |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 136 | parser = argparse.ArgumentParser() |
[email protected] | 493270e | 2015-07-15 19:37:31 | [diff] [blame] | 137 | parser.add_argument('--force-version', default='4.13') |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 138 | parser.add_argument('--clean', action='store_true', |
| 139 | help='Clear any existing gsutil package, forcing a new download.') |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 140 | parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL) |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 141 | parser.add_argument('--target', default=bin_dir, |
| 142 | help='The target directory to download/store a gsutil version in. ' |
| 143 | '(default is %(default)s).') |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 144 | parser.add_argument('args', nargs=argparse.REMAINDER) |
| 145 | |
[email protected] | c13b054 | 2014-12-18 01:06:20 | [diff] [blame] | 146 | args, extras = parser.parse_known_args() |
| 147 | if args.args and args.args[0] == '--': |
| 148 | args.args.pop(0) |
| 149 | if extras: |
| 150 | args.args = extras + args.args |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 151 | return args |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 152 | |
| 153 | |
| 154 | def main(): |
[email protected] | 605d81d | 2015-09-18 22:33:53 | [diff] [blame] | 155 | args = parse_args() |
| 156 | return run_gsutil(args.force_version, args.fallback, args.target, args.args, |
| 157 | clean=args.clean) |
[email protected] | 7a79054 | 2014-12-10 02:04:39 | [diff] [blame] | 158 | |
| 159 | if __name__ == '__main__': |
| 160 | sys.exit(main()) |