# Path: download_from_google_storage.py
# class Gsutil(object):
#   """Call gsutil with some predefined settings.  This is a convenience object,
#   and is also immutable."""
#   def __init__(self, path, boto_path, timeout=None, bypass_prodaccess=False):
#     if not os.path.exists(path):
#       raise FileNotFoundError('GSUtil not found in %s' % path)
#     self.path = path
#     self.timeout = timeout
#     self.boto_path = boto_path
#     self.bypass_prodaccess = bypass_prodaccess
# 
#   def get_sub_env(self):
#     env = os.environ.copy()
#     if self.boto_path == os.devnull:
#       env['AWS_CREDENTIAL_FILE'] = ''
#       env['BOTO_CONFIG'] = ''
#     elif self.boto_path:
#       env['AWS_CREDENTIAL_FILE'] = self.boto_path
#       env['BOTO_CONFIG'] = self.boto_path
#     else:
#       custompath = env.get('AWS_CREDENTIAL_FILE', '~/.boto') + '.depot_tools'
#       custompath = os.path.expanduser(custompath)
#       if os.path.exists(custompath):
#         env['AWS_CREDENTIAL_FILE'] = custompath
# 
#     return env
# 
#   def call(self, *args):
#     cmd = [sys.executable, self.path]
#     if self.bypass_prodaccess:
#       cmd.append('--bypass_prodaccess')
#     cmd.extend(args)
#     return subprocess2.call(cmd, env=self.get_sub_env(), timeout=self.timeout)
# 
#   def check_call(self, *args):
#     cmd = [sys.executable, self.path]
#     if self.bypass_prodaccess:
#       cmd.append('--bypass_prodaccess')
#     cmd.extend(args)
#     ((out, err), code) = subprocess2.communicate(
#         cmd,
#         stdout=subprocess2.PIPE,
#         stderr=subprocess2.PIPE,
#         env=self.get_sub_env(),
#         timeout=self.timeout)
# 
#     # Parse output.
#     status_code_match = re.search('status=([0-9]+)', err)
#     if status_code_match:
#       return (int(status_code_match.group(1)), out, err)
#     if ('You are attempting to access protected data with '
#           'no configured credentials.' in err):
#       return (403, out, err)
#     if 'No such object' in err:
#       return (404, out, err)
#     return (code, out, err)


import errno
import logging
import optparse
import os
import re
import tempfile
import time
import subprocess
import sys
import urlparse
import zipfile
import gclient_utils
import subcommand
from download_from_google_storage import Gsutil

  def config(self, cwd=None):
    if cwd is None:
      cwd = self.mirror_path
    self.RunGit(['config', 'core.deltaBaseCacheLimit',
                 gclient_utils.DefaultDeltaBaseCacheLimit()], cwd=cwd)
    self.RunGit(['config', 'remote.origin.url', self.url], cwd=cwd)
    self.RunGit(['config', '--replace-all', 'remote.origin.fetch',
                 '+refs/heads/*:refs/heads/*'], cwd=cwd)
    for ref in self.refs:
      ref = ref.lstrip('+').rstrip('/')
      if ref.startswith('refs/'):
        refspec = '+%s:%s' % (ref, ref)
      else:
        refspec = '+refs/%s/*:refs/%s/*' % (ref, ref)
      self.RunGit(['config', '--add', 'remote.origin.fetch', refspec], cwd=cwd)

  def bootstrap_repo(self, directory):
    """Bootstrap the repo from Google Stroage if possible."""

    python_fallback = False
    if sys.platform.startswith('win') and not self.FindExecutable('7z'):
      python_fallback = True
    elif sys.platform.startswith('darwin'):
      # The OSX version of unzip doesn't support zip64.
      python_fallback = True
    elif not self.FindExecutable('unzip'):
      python_fallback = True

    gs_folder = 'gs://%s/%s' % (self.bootstrap_bucket, self.basedir)

# Path: download_from_google_storage.py
# class Gsutil(object):
#   """Call gsutil with some predefined settings.  This is a convenience object,
#   and is also immutable."""
#   def __init__(self, path, boto_path, timeout=None, bypass_prodaccess=False):
#     if not os.path.exists(path):
#       raise FileNotFoundError('GSUtil not found in %s' % path)
#     self.path = path
#     self.timeout = timeout
#     self.boto_path = boto_path
#     self.bypass_prodaccess = bypass_prodaccess
# 
#   def get_sub_env(self):
#     env = os.environ.copy()
#     if self.boto_path == os.devnull:
#       env['AWS_CREDENTIAL_FILE'] = ''
#       env['BOTO_CONFIG'] = ''
#     elif self.boto_path:
#       env['AWS_CREDENTIAL_FILE'] = self.boto_path
#       env['BOTO_CONFIG'] = self.boto_path
#     else:
#       custompath = env.get('AWS_CREDENTIAL_FILE', '~/.boto') + '.depot_tools'
#       custompath = os.path.expanduser(custompath)
#       if os.path.exists(custompath):
#         env['AWS_CREDENTIAL_FILE'] = custompath
# 
#     return env
# 
#   def call(self, *args):
#     cmd = [sys.executable, self.path]
#     if self.bypass_prodaccess:
#       cmd.append('--bypass_prodaccess')
#     cmd.extend(args)
#     return subprocess2.call(cmd, env=self.get_sub_env(), timeout=self.timeout)
# 
#   def check_call(self, *args):
#     cmd = [sys.executable, self.path]
#     if self.bypass_prodaccess:
#       cmd.append('--bypass_prodaccess')
#     cmd.extend(args)
#     ((out, err), code) = subprocess2.communicate(
#         cmd,
#         stdout=subprocess2.PIPE,
#         stderr=subprocess2.PIPE,
#         env=self.get_sub_env(),
#         timeout=self.timeout)
# 
#     # Parse output.
#     status_code_match = re.search('status=([0-9]+)', err)
#     if status_code_match:
#       return (int(status_code_match.group(1)), out, err)
#     if ('You are attempting to access protected data with '
#           'no configured credentials.' in err):
#       return (403, out, err)
#     if 'No such object' in err:
#       return (404, out, err)
#     return (code, out, err)


# Path: git_cache.py
import errno
import logging
import optparse
import os
import re
import tempfile
import time
import subprocess
import sys
import urlparse
import zipfile
import gclient_utils
import subcommand
from download_from_google_storage import Gsutil


  def config(self, cwd=None):
    if cwd is None:
      cwd = self.mirror_path
    self.RunGit(['config', 'core.deltaBaseCacheLimit',
                 gclient_utils.DefaultDeltaBaseCacheLimit()], cwd=cwd)
    self.RunGit(['config', 'remote.origin.url', self.url], cwd=cwd)
    self.RunGit(['config', '--replace-all', 'remote.origin.fetch',
                 '+refs/heads/*:refs/heads/*'], cwd=cwd)
    for ref in self.refs:
      ref = ref.lstrip('+').rstrip('/')
      if ref.startswith('refs/'):
        refspec = '+%s:%s' % (ref, ref)
      else:
        refspec = '+refs/%s/*:refs/%s/*' % (ref, ref)
      self.RunGit(['config', '--add', 'remote.origin.fetch', refspec], cwd=cwd)

  def bootstrap_repo(self, directory):
    """Bootstrap the repo from Google Stroage if possible."""

    python_fallback = False
    if sys.platform.startswith('win') and not self.FindExecutable('7z'):
      python_fallback = True
    elif sys.platform.startswith('darwin'):
      # The OSX version of unzip doesn't support zip64.
      python_fallback = True
    elif not self.FindExecutable('unzip'):
      python_fallback = True

    gs_folder = 'gs://%s/%s' % (self.bootstrap_bucket, self.basedir)

    gsutil = Gsutil(