| 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 re | 13 import re |
| 14 import tempfile | 14 import tempfile |
| 15 import threading |
| 15 import time | 16 import time |
| 16 import shutil | 17 import shutil |
| 17 import subprocess | 18 import subprocess |
| 18 import sys | 19 import sys |
| 19 import urlparse | 20 import urlparse |
| 20 import zipfile | 21 import zipfile |
| 21 | 22 |
| 22 from download_from_google_storage import Gsutil | 23 from download_from_google_storage import Gsutil |
| 23 import gclient_utils | 24 import gclient_utils |
| 24 import subcommand | 25 import subcommand |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 """Test if the file is locked by this process.""" | 138 """Test if the file is locked by this process.""" |
| 138 return self.is_locked() and self.pid == self._read_pid() | 139 return self.is_locked() and self.pid == self._read_pid() |
| 139 | 140 |
| 140 | 141 |
| 141 class Mirror(object): | 142 class Mirror(object): |
| 142 | 143 |
| 143 git_exe = 'git.bat' if sys.platform.startswith('win') else 'git' | 144 git_exe = 'git.bat' if sys.platform.startswith('win') else 'git' |
| 144 gsutil_exe = os.path.join( | 145 gsutil_exe = os.path.join( |
| 145 os.path.dirname(os.path.abspath(__file__)), | 146 os.path.dirname(os.path.abspath(__file__)), |
| 146 'third_party', 'gsutil', 'gsutil') | 147 'third_party', 'gsutil', 'gsutil') |
| 148 cachepath_lock = threading.Lock() |
| 147 | 149 |
| 148 def __init__(self, url, refs=None, print_func=None): | 150 def __init__(self, url, refs=None, print_func=None): |
| 149 self.url = url | 151 self.url = url |
| 150 self.refs = refs or [] | 152 self.refs = refs or [] |
| 151 self.basedir = self.UrlToCacheDir(url) | 153 self.basedir = self.UrlToCacheDir(url) |
| 152 self.mirror_path = os.path.join(self.GetCachePath(), self.basedir) | 154 self.mirror_path = os.path.join(self.GetCachePath(), self.basedir) |
| 153 self.print = print_func or print | 155 self.print = print_func or print |
| 154 | 156 |
| 155 @property | 157 @property |
| 156 def bootstrap_bucket(self): | 158 def bootstrap_bucket(self): |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 if os.path.isfile(alt_target) and os.access(alt_target, os.X_OK): | 197 if os.path.isfile(alt_target) and os.access(alt_target, os.X_OK): |
| 196 return alt_target | 198 return alt_target |
| 197 return None | 199 return None |
| 198 | 200 |
| 199 @classmethod | 201 @classmethod |
| 200 def SetCachePath(cls, cachepath): | 202 def SetCachePath(cls, cachepath): |
| 201 setattr(cls, 'cachepath', cachepath) | 203 setattr(cls, 'cachepath', cachepath) |
| 202 | 204 |
| 203 @classmethod | 205 @classmethod |
| 204 def GetCachePath(cls): | 206 def GetCachePath(cls): |
| 207 cls.cachepath_lock.acquire() |
| 205 if not hasattr(cls, 'cachepath'): | 208 if not hasattr(cls, 'cachepath'): |
| 206 try: | 209 try: |
| 207 cachepath = subprocess.check_output( | 210 cachepath = subprocess.check_output( |
| 208 [cls.git_exe, 'config', '--global', 'cache.cachepath']).strip() | 211 [cls.git_exe, 'config', '--global', 'cache.cachepath']).strip() |
| 209 except subprocess.CalledProcessError: | 212 except subprocess.CalledProcessError: |
| 210 cachepath = None | 213 cachepath = None |
| 211 if not cachepath: | 214 if not cachepath: |
| 215 cls.cachepath_lock.release() |
| 212 raise RuntimeError('No global cache.cachepath git configuration found.') | 216 raise RuntimeError('No global cache.cachepath git configuration found.') |
| 213 setattr(cls, 'cachepath', cachepath) | 217 setattr(cls, 'cachepath', cachepath) |
| 218 cls.cachepath_lock.release() |
| 214 return getattr(cls, 'cachepath') | 219 return getattr(cls, 'cachepath') |
| 215 | 220 |
| 216 def RunGit(self, cmd, **kwargs): | 221 def RunGit(self, cmd, **kwargs): |
| 217 """Run git in a subprocess.""" | 222 """Run git in a subprocess.""" |
| 218 cwd = kwargs.setdefault('cwd', self.mirror_path) | 223 cwd = kwargs.setdefault('cwd', self.mirror_path) |
| 219 kwargs.setdefault('print_stdout', False) | 224 kwargs.setdefault('print_stdout', False) |
| 220 kwargs.setdefault('filter_fn', self.print) | 225 kwargs.setdefault('filter_fn', self.print) |
| 221 env = kwargs.get('env') or kwargs.setdefault('env', os.environ.copy()) | 226 env = kwargs.get('env') or kwargs.setdefault('env', os.environ.copy()) |
| 222 env.setdefault('GIT_ASKPASS', 'true') | 227 env.setdefault('GIT_ASKPASS', 'true') |
| 223 env.setdefault('SSH_ASKPASS', 'true') | 228 env.setdefault('SSH_ASKPASS', 'true') |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 return options, args | 690 return options, args |
| 686 | 691 |
| 687 | 692 |
| 688 def main(argv): | 693 def main(argv): |
| 689 dispatcher = subcommand.CommandDispatcher(__name__) | 694 dispatcher = subcommand.CommandDispatcher(__name__) |
| 690 return dispatcher.execute(OptionParser(), argv) | 695 return dispatcher.execute(OptionParser(), argv) |
| 691 | 696 |
| 692 | 697 |
| 693 if __name__ == '__main__': | 698 if __name__ == '__main__': |
| 694 sys.exit(main(sys.argv[1:])) | 699 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |