blob: 64e4b5cbc9b8d674044f6526fc898976c95c7ff5 [file] [log] [blame]
hinoka@chromium.org7a790542014-12-10 02:04:39 +00001#!/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
9import argparse
hinoka@chromium.org7a790542014-12-10 02:04:39 +000010import base64
dnj@chromium.org605d81d2015-09-18 22:33:53 +000011import contextlib
primiano@chromium.orgdf351762014-12-18 11:12:34 +000012import hashlib
hinoka@chromium.org7a790542014-12-10 02:04:39 +000013import json
primiano@chromium.orgdf351762014-12-18 11:12:34 +000014import os
15import shutil
hinoka@chromium.org7a790542014-12-10 02:04:39 +000016import subprocess
primiano@chromium.orgdf351762014-12-18 11:12:34 +000017import sys
dnj@chromium.org605d81d2015-09-18 22:33:53 +000018import tempfile
19import time
Raul Tambreb946b232019-03-26 14:48:46 +000020
21try:
22 import urllib2 as urllib
23except ImportError: # For Py3 compatibility
24 import urllib.request as urllib
25
primiano@chromium.orgdf351762014-12-18 11:12:34 +000026import zipfile
hinoka@chromium.org7a790542014-12-10 02:04:39 +000027
28
29GSUTIL_URL = 'https://storage.googleapis.com/pub/'
30API_URL = 'https://www.googleapis.com/storage/v1/b/pub/o/'
31
32THIS_DIR = os.path.dirname(os.path.abspath(__file__))
33DEFAULT_BIN_DIR = os.path.join(THIS_DIR, 'external_bin', 'gsutil')
34DEFAULT_FALLBACK_GSUTIL = os.path.join(
35 THIS_DIR, 'third_party', 'gsutil', 'gsutil')
36
Dan Jacques509776e2017-09-07 18:01:08 -070037IS_WINDOWS = os.name == 'nt'
38
39
hinoka@chromium.org7a790542014-12-10 02:04:39 +000040class InvalidGsutilError(Exception):
41 pass
42
43
hinoka@chromium.org7a790542014-12-10 02:04:39 +000044def 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 Tambreb946b232019-03-26 14:48:46 +000061 metadata = json.load(urllib.urlopen(metadata_url))
hinoka@chromium.org7a790542014-12-10 02:04:39 +000062 remote_md5 = base64.b64decode(metadata['md5Hash'])
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 Tambreb946b232019-03-26 14:48:46 +000070 u = urllib.urlopen(url)
hinoka@chromium.org7a790542014-12-10 02:04:39 +000071 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.org605d81d2015-09-18 22:33:53 +000080@contextlib.contextmanager
81def 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 Yearsleyd9cbe7a2019-09-03 16:49:11 +000089
dnj@chromium.org605d81d2015-09-18 22:33:53 +000090def ensure_gsutil(version, target, clean):
hinoka@chromium.org7a790542014-12-10 02:04:39 +000091 bin_dir = os.path.join(target, 'gsutil_%s' % version)
92 gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil')
Ryan Tseng83fd81f2017-10-23 11:13:48 -070093 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.org7a790542014-12-10 02:04:39 +000097 # Everything is awesome! we're all done here.
98 return gsutil_bin
99
dnj@chromium.org605d81d2015-09-18 22:33:53 +0000100 if not os.path.exists(target):
101 os.makedirs(target)
102 with temporary_directory(target) as instance_dir:
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000103 # Clean up if we're redownloading a corrupted gsutil.
dnj@chromium.org605d81d2015-09-18 22:33:53 +0000104 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 Tseng83fd81f2017-10-23 11:13:48 -0700122 # 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.org7a790542014-12-10 02:04:39 +0000128
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000129 return gsutil_bin
130
131
dnj@chromium.org605d81d2015-09-18 22:33:53 +0000132def run_gsutil(force_version, fallback, target, args, clean=False):
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000133 if force_version:
dnj@chromium.org605d81d2015-09-18 22:33:53 +0000134 gsutil_bin = ensure_gsutil(force_version, target, clean)
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000135 else:
136 gsutil_bin = fallback
hinoka@chromium.orgfdb9ce32016-04-05 23:57:12 +0000137 disable_update = ['-o', 'GSUtil:software_update_check_period=0']
Dan Jacques509776e2017-09-07 18:01:08 -0700138
Chris Nardiab816ce2017-10-31 15:45:05 -0400139 if sys.platform == 'cygwin':
140 # This script requires Windows Python, so invoke with depot_tools'
141 # Python.
142 def winpath(path):
143 return subprocess.check_output(['cygpath', '-w', path]).strip()
144 cmd = ['python.bat', winpath(__file__)]
145 cmd.extend(args)
146 sys.exit(subprocess.call(cmd))
147 assert sys.platform != 'cygwin'
148
Dan Jacques509776e2017-09-07 18:01:08 -0700149 # Run "gsutil" through "vpython". We need to do this because on GCE instances,
150 # expectations are made about Python having access to "google-compute-engine"
151 # and "boto" packages that are not met with non-system Python (e.g., bundles).
152 cmd = [
153 'vpython',
154 '-vpython-spec', os.path.join(THIS_DIR, 'gsutil.vpython'),
155 '--',
156 gsutil_bin
157 ] + disable_update + args
158 return subprocess.call(cmd, shell=IS_WINDOWS)
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000159
160
161def parse_args():
dnj@chromium.org605d81d2015-09-18 22:33:53 +0000162 bin_dir = os.environ.get('DEPOT_TOOLS_GSUTIL_BIN_DIR', DEFAULT_BIN_DIR)
163
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000164 parser = argparse.ArgumentParser()
Caleb Rouleau63e216c2018-03-29 14:20:37 -0700165 parser.add_argument('--force-version', default='4.30')
dnj@chromium.org605d81d2015-09-18 22:33:53 +0000166 parser.add_argument('--clean', action='store_true',
167 help='Clear any existing gsutil package, forcing a new download.')
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000168 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL)
dnj@chromium.org605d81d2015-09-18 22:33:53 +0000169 parser.add_argument('--target', default=bin_dir,
170 help='The target directory to download/store a gsutil version in. '
171 '(default is %(default)s).')
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000172 parser.add_argument('args', nargs=argparse.REMAINDER)
173
hinoka@chromium.orgc13b0542014-12-18 01:06:20 +0000174 args, extras = parser.parse_known_args()
175 if args.args and args.args[0] == '--':
176 args.args.pop(0)
177 if extras:
178 args.args = extras + args.args
dnj@chromium.org605d81d2015-09-18 22:33:53 +0000179 return args
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000180
181
182def main():
dnj@chromium.org605d81d2015-09-18 22:33:53 +0000183 args = parse_args()
184 return run_gsutil(args.force_version, args.fallback, args.target, args.args,
185 clean=args.clean)
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000186
Quinten Yearsleyd9cbe7a2019-09-03 16:49:11 +0000187
hinoka@chromium.org7a790542014-12-10 02:04:39 +0000188if __name__ == '__main__':
189 sys.exit(main())