Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """A git command for managing a local cache of git repositories.""" | 6 """A git command for managing a local cache of git repositories.""" |
| 7 | 7 |
| 8 from __future__ import print_function | 8 from __future__ import print_function |
| 9 import errno | 9 import errno |
| 10 import logging | 10 import logging |
| 11 import optparse | 11 import optparse |
| 12 import os | 12 import os |
| 13 import tempfile | 13 import tempfile |
| 14 import time | 14 import time |
| 15 import subprocess | 15 import subprocess |
| 16 import sys | 16 import sys |
| 17 import urlparse | 17 import urlparse |
| 18 import zipfile | |
| 18 | 19 |
| 19 from download_from_google_storage import Gsutil | 20 from download_from_google_storage import Gsutil |
| 20 import gclient_utils | 21 import gclient_utils |
| 21 import subcommand | 22 import subcommand |
| 22 | 23 |
| 23 try: | 24 try: |
| 24 # pylint: disable=E0602 | 25 # pylint: disable=E0602 |
| 25 WinErr = WindowsError | 26 WinErr = WindowsError |
| 26 except NameError: | 27 except NameError: |
| 27 class WinErr(Exception): | 28 class WinErr(Exception): |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 '+refs/heads/*:refs/heads/*'], cwd=cwd) | 216 '+refs/heads/*:refs/heads/*'], cwd=cwd) |
| 216 for ref in self.refs: | 217 for ref in self.refs: |
| 217 ref = ref.lstrip('+').rstrip('/') | 218 ref = ref.lstrip('+').rstrip('/') |
| 218 if ref.startswith('refs/'): | 219 if ref.startswith('refs/'): |
| 219 refspec = '+%s:%s' % (ref, ref) | 220 refspec = '+%s:%s' % (ref, ref) |
| 220 else: | 221 else: |
| 221 refspec = '+refs/%s/*:refs/%s/*' % (ref, ref) | 222 refspec = '+refs/%s/*:refs/%s/*' % (ref, ref) |
| 222 self.RunGit(['config', '--add', 'remote.origin.fetch', refspec], cwd=cwd) | 223 self.RunGit(['config', '--add', 'remote.origin.fetch', refspec], cwd=cwd) |
| 223 | 224 |
| 224 def bootstrap_repo(self, directory): | 225 def bootstrap_repo(self, directory): |
| 225 """Bootstrap the repo from Google Stroage if possible. | 226 """Bootstrap the repo from Google Stroage if possible.""" |
| 226 | 227 |
| 227 Requires 7z on Windows and Unzip on Linux/Mac. | 228 python_fallback = False |
| 228 """ | |
| 229 if sys.platform.startswith('win'): | 229 if sys.platform.startswith('win'): |
|
agable
2014/04/25 05:41:39
sys.platform.... and not self.FindExec...
Ryan Tseng
2014/04/25 08:49:47
Done.
| |
| 230 if not self.FindExecutable('7z'): | 230 if not self.FindExecutable('7z'): |
| 231 self.print(''' | 231 python_fallback = True |
| 232 Cannot find 7z in the path. If you want git cache to be able to bootstrap from | 232 elif sys.platform.startswith('darwin'): |
| 233 Google Storage, please install 7z from: | 233 # The OSX version of unzip doesn't support zip64. |
| 234 | 234 python_fallback = True |
| 235 http://www.7-zip.org/download.html | 235 elif not self.FindExecutable('unzip'): |
| 236 ''') | 236 python_fallback = True |
| 237 return False | |
| 238 else: | |
| 239 if not self.FindExecutable('unzip'): | |
| 240 self.print(''' | |
| 241 Cannot find unzip in the path. If you want git cache to be able to bootstrap | |
| 242 from Google Storage, please ensure unzip is present on your system. | |
| 243 ''') | |
| 244 return False | |
| 245 | 237 |
| 246 gs_folder = 'gs://%s/%s' % (self.bootstrap_bucket, self.basedir) | 238 gs_folder = 'gs://%s/%s' % (self.bootstrap_bucket, self.basedir) |
| 247 gsutil = Gsutil( | 239 gsutil = Gsutil( |
| 248 self.gsutil_exe, boto_path=os.devnull, bypass_prodaccess=True) | 240 self.gsutil_exe, boto_path=os.devnull, bypass_prodaccess=True) |
| 249 # Get the most recent version of the zipfile. | 241 # Get the most recent version of the zipfile. |
| 250 _, ls_out, _ = gsutil.check_call('ls', gs_folder) | 242 _, ls_out, _ = gsutil.check_call('ls', gs_folder) |
| 251 ls_out_sorted = sorted(ls_out.splitlines()) | 243 ls_out_sorted = sorted(ls_out.splitlines()) |
| 252 if not ls_out_sorted: | 244 if not ls_out_sorted: |
| 253 # This repo is not on Google Storage. | 245 # This repo is not on Google Storage. |
| 254 return False | 246 return False |
| 255 latest_checkout = ls_out_sorted[-1] | 247 latest_checkout = ls_out_sorted[-1] |
| 256 | 248 |
| 257 # Download zip file to a temporary directory. | 249 # Download zip file to a temporary directory. |
| 258 try: | 250 try: |
| 259 tempdir = tempfile.mkdtemp() | 251 tempdir = tempfile.mkdtemp() |
| 260 self.print('Downloading %s' % latest_checkout) | 252 self.print('Downloading %s' % latest_checkout) |
| 261 code, out, err = gsutil.check_call('cp', latest_checkout, tempdir) | 253 code, out, err = gsutil.check_call('cp', latest_checkout, tempdir) |
| 262 if code: | 254 if code: |
| 263 self.print('%s\n%s' % (out, err)) | 255 self.print('%s\n%s' % (out, err)) |
| 264 return False | 256 return False |
| 265 filename = os.path.join(tempdir, latest_checkout.split('/')[-1]) | 257 filename = os.path.join(tempdir, latest_checkout.split('/')[-1]) |
| 266 | 258 |
| 267 # Unpack the file with 7z on Windows, or unzip everywhere else. | 259 # Unpack the file with 7z on Windows, unzip on linux, or fallback. |
| 268 if sys.platform.startswith('win'): | 260 if python_fallback: |
|
agable
2014/04/25 05:41:39
Flop these. Start with "if not python_fallback:"..
Ryan Tseng
2014/04/25 08:49:47
Done.
| |
| 269 cmd = ['7z', 'x', '-o%s' % directory, '-tzip', filename] | 261 try: |
| 262 with zipfile.ZipFile(filename, 'r') as f: | |
| 263 f.printdir() | |
|
agable
2014/04/25 05:41:39
Is this necessary or is it debugging?
Ryan Tseng
2014/04/25 08:49:47
Its not, but its there to match the behavior of 7z
| |
| 264 f.extractall(directory) | |
| 265 except Exception as e: | |
| 266 self.print('Encountered error: %s' % str(e), file=sys.stderr) | |
| 267 retcode = 1 | |
| 268 else: | |
| 269 retcode = 0 | |
| 270 else: | 270 else: |
| 271 cmd = ['unzip', filename, '-d', directory] | 271 if sys.platform.startswith('win'): |
| 272 retcode = subprocess.call(cmd) | 272 cmd = ['7z', 'x', '-o%s' % directory, '-tzip', filename] |
| 273 else: | |
| 274 cmd = ['unzip', filename, '-d', directory] | |
| 275 retcode = subprocess.call(cmd) | |
| 273 finally: | 276 finally: |
| 274 # Clean up the downloaded zipfile. | 277 # Clean up the downloaded zipfile. |
| 275 gclient_utils.rmtree(tempdir) | 278 gclient_utils.rmtree(tempdir) |
| 276 | 279 |
| 277 if retcode: | 280 if retcode: |
| 278 self.print( | 281 self.print( |
| 279 'Extracting bootstrap zipfile %s failed.\n' | 282 'Extracting bootstrap zipfile %s failed.\n' |
| 280 'Resuming normal operations.' % filename) | 283 'Resuming normal operations.' % filename) |
| 281 return False | 284 return False |
| 282 return True | 285 return True |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 505 return options, args | 508 return options, args |
| 506 | 509 |
| 507 | 510 |
| 508 def main(argv): | 511 def main(argv): |
| 509 dispatcher = subcommand.CommandDispatcher(__name__) | 512 dispatcher = subcommand.CommandDispatcher(__name__) |
| 510 return dispatcher.execute(OptionParser(), argv) | 513 return dispatcher.execute(OptionParser(), argv) |
| 511 | 514 |
| 512 | 515 |
| 513 if __name__ == '__main__': | 516 if __name__ == '__main__': |
| 514 sys.exit(main(sys.argv[1:])) | 517 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |