The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (C) 2008 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | magic='--calling-python-from-/bin/sh--' |
Shawn O. Pearce | 7542d66 | 2008-10-21 07:11:36 -0700 | [diff] [blame] | 18 | """exec" python -E "$0" "$@" """#$magic" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | if __name__ == '__main__': |
| 20 | import sys |
| 21 | if sys.argv[-1] == '#%s' % magic: |
| 22 | del sys.argv[-1] |
| 23 | del magic |
| 24 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 25 | import getpass |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 26 | import netrc |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | import optparse |
| 28 | import os |
| 29 | import re |
| 30 | import sys |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 31 | import time |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 32 | import urllib2 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 34 | from trace import SetTrace |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 35 | from git_command import git, GitCommand |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 36 | from git_config import init_ssh, close_ssh |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 37 | from command import InteractiveCommand |
| 38 | from command import MirrorSafeCommand |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 39 | from subcmds.version import Version |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 40 | from editor import Editor |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 41 | from error import DownloadError |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 42 | from error import ManifestInvalidRevisionError |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | from error import NoSuchProjectError |
| 44 | from error import RepoChangedException |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 45 | from manifest_xml import XmlManifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | from pager import RunPager |
| 47 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 48 | from subcmds import all_commands |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | |
| 50 | global_options = optparse.OptionParser( |
| 51 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" |
| 52 | ) |
| 53 | global_options.add_option('-p', '--paginate', |
| 54 | dest='pager', action='store_true', |
| 55 | help='display command output in the pager') |
| 56 | global_options.add_option('--no-pager', |
| 57 | dest='no_pager', action='store_true', |
| 58 | help='disable the pager') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 59 | global_options.add_option('--trace', |
| 60 | dest='trace', action='store_true', |
| 61 | help='trace git command execution') |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 62 | global_options.add_option('--time', |
| 63 | dest='time', action='store_true', |
| 64 | help='time repo command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 65 | global_options.add_option('--version', |
| 66 | dest='show_version', action='store_true', |
| 67 | help='display this version of repo') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 68 | |
| 69 | class _Repo(object): |
| 70 | def __init__(self, repodir): |
| 71 | self.repodir = repodir |
| 72 | self.commands = all_commands |
Mike Lockwood | 2bf9db0 | 2009-07-14 15:23:39 -0400 | [diff] [blame] | 73 | # add 'branch' as an alias for 'branches' |
| 74 | all_commands['branch'] = all_commands['branches'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 75 | |
| 76 | def _Run(self, argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 77 | result = 0 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | name = None |
| 79 | glob = [] |
| 80 | |
| 81 | for i in xrange(0, len(argv)): |
| 82 | if not argv[i].startswith('-'): |
| 83 | name = argv[i] |
| 84 | if i > 0: |
| 85 | glob = argv[:i] |
| 86 | argv = argv[i + 1:] |
| 87 | break |
| 88 | if not name: |
| 89 | glob = argv |
| 90 | name = 'help' |
| 91 | argv = [] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 92 | gopts, _gargs = global_options.parse_args(glob) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 94 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 95 | SetTrace() |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 96 | if gopts.show_version: |
| 97 | if name == 'help': |
| 98 | name = 'version' |
| 99 | else: |
| 100 | print >>sys.stderr, 'fatal: invalid usage of --version' |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 101 | return 1 |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 102 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | try: |
| 104 | cmd = self.commands[name] |
| 105 | except KeyError: |
| 106 | print >>sys.stderr,\ |
| 107 | "repo: '%s' is not a repo command. See 'repo help'."\ |
| 108 | % name |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 109 | return 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | |
| 111 | cmd.repodir = self.repodir |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 112 | cmd.manifest = XmlManifest(cmd.repodir) |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 113 | Editor.globalConfig = cmd.manifest.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 114 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 115 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
| 116 | print >>sys.stderr, \ |
| 117 | "fatal: '%s' requires a working directory"\ |
| 118 | % name |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 119 | return 1 |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 120 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 121 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 122 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 123 | if not gopts.no_pager and not isinstance(cmd, InteractiveCommand): |
| 124 | config = cmd.manifest.globalConfig |
| 125 | if gopts.pager: |
| 126 | use_pager = True |
| 127 | else: |
| 128 | use_pager = config.GetBoolean('pager.%s' % name) |
| 129 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 130 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 131 | if use_pager: |
| 132 | RunPager(config) |
| 133 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 134 | try: |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 135 | start = time.time() |
| 136 | try: |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 137 | result = cmd.Execute(copts, cargs) |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 138 | finally: |
| 139 | elapsed = time.time() - start |
| 140 | hours, remainder = divmod(elapsed, 3600) |
| 141 | minutes, seconds = divmod(remainder, 60) |
| 142 | if gopts.time: |
| 143 | if hours == 0: |
| 144 | print >>sys.stderr, 'real\t%dm%.3fs' \ |
| 145 | % (minutes, seconds) |
| 146 | else: |
| 147 | print >>sys.stderr, 'real\t%dh%dm%.3fs' \ |
| 148 | % (hours, minutes, seconds) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 149 | except DownloadError, e: |
| 150 | print >>sys.stderr, 'error: %s' % str(e) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 151 | return 1 |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 152 | except ManifestInvalidRevisionError, e: |
| 153 | print >>sys.stderr, 'error: %s' % str(e) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 154 | return 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | except NoSuchProjectError, e: |
| 156 | if e.name: |
| 157 | print >>sys.stderr, 'error: project %s not found' % e.name |
| 158 | else: |
| 159 | print >>sys.stderr, 'error: no project in current directory' |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 160 | return 1 |
| 161 | |
| 162 | return result |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 163 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 164 | def _MyRepoPath(): |
| 165 | return os.path.dirname(__file__) |
| 166 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 167 | def _MyWrapperPath(): |
| 168 | return os.path.join(os.path.dirname(__file__), 'repo') |
| 169 | |
| 170 | def _CurrentWrapperVersion(): |
| 171 | VERSION = None |
| 172 | pat = re.compile(r'^VERSION *=') |
| 173 | fd = open(_MyWrapperPath()) |
| 174 | for line in fd: |
| 175 | if pat.match(line): |
| 176 | fd.close() |
| 177 | exec line |
| 178 | return VERSION |
| 179 | raise NameError, 'No VERSION in repo script' |
| 180 | |
| 181 | def _CheckWrapperVersion(ver, repo_path): |
| 182 | if not repo_path: |
| 183 | repo_path = '~/bin/repo' |
| 184 | |
| 185 | if not ver: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 186 | print >>sys.stderr, 'no --wrapper-version argument' |
| 187 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | |
| 189 | exp = _CurrentWrapperVersion() |
| 190 | ver = tuple(map(lambda x: int(x), ver.split('.'))) |
| 191 | if len(ver) == 1: |
| 192 | ver = (0, ver[0]) |
| 193 | |
| 194 | if exp[0] > ver[0] or ver < (0, 4): |
| 195 | exp_str = '.'.join(map(lambda x: str(x), exp)) |
| 196 | print >>sys.stderr, """ |
| 197 | !!! A new repo command (%5s) is available. !!! |
| 198 | !!! You must upgrade before you can continue: !!! |
| 199 | |
| 200 | cp %s %s |
| 201 | """ % (exp_str, _MyWrapperPath(), repo_path) |
| 202 | sys.exit(1) |
| 203 | |
| 204 | if exp > ver: |
| 205 | exp_str = '.'.join(map(lambda x: str(x), exp)) |
| 206 | print >>sys.stderr, """ |
| 207 | ... A new repo command (%5s) is available. |
| 208 | ... You should upgrade soon: |
| 209 | |
| 210 | cp %s %s |
| 211 | """ % (exp_str, _MyWrapperPath(), repo_path) |
| 212 | |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 213 | def _CheckRepoDir(repo_dir): |
| 214 | if not repo_dir: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 215 | print >>sys.stderr, 'no --repo-dir argument' |
| 216 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 217 | |
| 218 | def _PruneOptions(argv, opt): |
| 219 | i = 0 |
| 220 | while i < len(argv): |
| 221 | a = argv[i] |
| 222 | if a == '--': |
| 223 | break |
| 224 | if a.startswith('--'): |
| 225 | eq = a.find('=') |
| 226 | if eq > 0: |
| 227 | a = a[0:eq] |
| 228 | if not opt.has_option(a): |
| 229 | del argv[i] |
| 230 | continue |
| 231 | i += 1 |
| 232 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 233 | _user_agent = None |
| 234 | |
| 235 | def _UserAgent(): |
| 236 | global _user_agent |
| 237 | |
| 238 | if _user_agent is None: |
| 239 | py_version = sys.version_info |
| 240 | |
| 241 | os_name = sys.platform |
| 242 | if os_name == 'linux2': |
| 243 | os_name = 'Linux' |
| 244 | elif os_name == 'win32': |
| 245 | os_name = 'Win32' |
| 246 | elif os_name == 'cygwin': |
| 247 | os_name = 'Cygwin' |
| 248 | elif os_name == 'darwin': |
| 249 | os_name = 'Darwin' |
| 250 | |
| 251 | p = GitCommand( |
| 252 | None, ['describe', 'HEAD'], |
| 253 | cwd = _MyRepoPath(), |
| 254 | capture_stdout = True) |
| 255 | if p.Wait() == 0: |
| 256 | repo_version = p.stdout |
| 257 | if len(repo_version) > 0 and repo_version[-1] == '\n': |
| 258 | repo_version = repo_version[0:-1] |
| 259 | if len(repo_version) > 0 and repo_version[0] == 'v': |
| 260 | repo_version = repo_version[1:] |
| 261 | else: |
| 262 | repo_version = 'unknown' |
| 263 | |
| 264 | _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( |
| 265 | repo_version, |
| 266 | os_name, |
| 267 | '.'.join(map(lambda d: str(d), git.version_tuple())), |
| 268 | py_version[0], py_version[1], py_version[2]) |
| 269 | return _user_agent |
| 270 | |
| 271 | class _UserAgentHandler(urllib2.BaseHandler): |
| 272 | def http_request(self, req): |
| 273 | req.add_header('User-Agent', _UserAgent()) |
| 274 | return req |
| 275 | |
| 276 | def https_request(self, req): |
| 277 | req.add_header('User-Agent', _UserAgent()) |
| 278 | return req |
| 279 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 280 | def _AddPasswordFromUserInput(handler, msg, req): |
| 281 | # If repo could not find auth info from netrc, try to get it from user input |
| 282 | url = req.get_full_url() |
| 283 | user, password = handler.passwd.find_user_password(None, url) |
| 284 | if user is None: |
| 285 | print msg |
| 286 | try: |
| 287 | user = raw_input('User: ') |
| 288 | password = getpass.getpass() |
| 289 | except KeyboardInterrupt: |
| 290 | return |
| 291 | handler.passwd.add_password(None, url, user, password) |
| 292 | |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 293 | class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 294 | def http_error_401(self, req, fp, code, msg, headers): |
| 295 | _AddPasswordFromUserInput(self, msg, req) |
| 296 | return urllib2.HTTPBasicAuthHandler.http_error_401( |
| 297 | self, req, fp, code, msg, headers) |
| 298 | |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 299 | def http_error_auth_reqed(self, authreq, host, req, headers): |
| 300 | try: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 301 | old_add_header = req.add_header |
| 302 | def _add_header(name, val): |
| 303 | val = val.replace('\n', '') |
| 304 | old_add_header(name, val) |
| 305 | req.add_header = _add_header |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 306 | return urllib2.AbstractBasicAuthHandler.http_error_auth_reqed( |
| 307 | self, authreq, host, req, headers) |
| 308 | except: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 309 | reset = getattr(self, 'reset_retry_count', None) |
| 310 | if reset is not None: |
| 311 | reset() |
Shawn O. Pearce | b660539 | 2011-10-11 15:58:07 -0700 | [diff] [blame] | 312 | elif getattr(self, 'retried', None): |
| 313 | self.retried = 0 |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 314 | raise |
| 315 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 316 | class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 317 | def http_error_401(self, req, fp, code, msg, headers): |
| 318 | _AddPasswordFromUserInput(self, msg, req) |
| 319 | return urllib2.HTTPDigestAuthHandler.http_error_401( |
| 320 | self, req, fp, code, msg, headers) |
| 321 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 322 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 323 | try: |
| 324 | old_add_header = req.add_header |
| 325 | def _add_header(name, val): |
| 326 | val = val.replace('\n', '') |
| 327 | old_add_header(name, val) |
| 328 | req.add_header = _add_header |
| 329 | return urllib2.AbstractDigestAuthHandler.http_error_auth_reqed( |
| 330 | self, auth_header, host, req, headers) |
| 331 | except: |
| 332 | reset = getattr(self, 'reset_retry_count', None) |
| 333 | if reset is not None: |
| 334 | reset() |
| 335 | elif getattr(self, 'retried', None): |
| 336 | self.retried = 0 |
| 337 | raise |
| 338 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 339 | def init_http(): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 340 | handlers = [_UserAgentHandler()] |
| 341 | |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 342 | mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() |
| 343 | try: |
| 344 | n = netrc.netrc() |
| 345 | for host in n.hosts: |
| 346 | p = n.hosts[host] |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 347 | mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) |
| 348 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 349 | except netrc.NetrcParseError: |
| 350 | pass |
Shawn O. Pearce | 7b947de | 2011-09-23 11:50:31 -0700 | [diff] [blame] | 351 | except IOError: |
| 352 | pass |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 353 | handlers.append(_BasicAuthHandler(mgr)) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 354 | handlers.append(_DigestAuthHandler(mgr)) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 355 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 356 | if 'http_proxy' in os.environ: |
| 357 | url = os.environ['http_proxy'] |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 358 | handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) |
| 359 | if 'REPO_CURL_VERBOSE' in os.environ: |
| 360 | handlers.append(urllib2.HTTPHandler(debuglevel=1)) |
| 361 | handlers.append(urllib2.HTTPSHandler(debuglevel=1)) |
| 362 | urllib2.install_opener(urllib2.build_opener(*handlers)) |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 363 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 364 | def _Main(argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 365 | result = 0 |
| 366 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 367 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 368 | opt.add_option("--repo-dir", dest="repodir", |
| 369 | help="path to .repo/") |
| 370 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 371 | help="version of the wrapper script") |
| 372 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 373 | help="location of the wrapper script") |
| 374 | _PruneOptions(argv, opt) |
| 375 | opt, argv = opt.parse_args(argv) |
| 376 | |
| 377 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 378 | _CheckRepoDir(opt.repodir) |
| 379 | |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 380 | Version.wrapper_version = opt.wrapper_version |
| 381 | Version.wrapper_path = opt.wrapper_path |
| 382 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 383 | repo = _Repo(opt.repodir) |
| 384 | try: |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 385 | try: |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 386 | init_ssh() |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 387 | init_http() |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 388 | result = repo._Run(argv) or 0 |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 389 | finally: |
| 390 | close_ssh() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 391 | except KeyboardInterrupt: |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 392 | result = 1 |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 393 | except RepoChangedException, rce: |
| 394 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 395 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 396 | argv = list(sys.argv) |
| 397 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 398 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 399 | os.execv(__file__, argv) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 400 | except OSError, e: |
| 401 | print >>sys.stderr, 'fatal: cannot restart repo after upgrade' |
| 402 | print >>sys.stderr, 'fatal: %s' % e |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 403 | result = 128 |
| 404 | |
| 405 | sys.exit(result) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 406 | |
| 407 | if __name__ == '__main__': |
| 408 | _Main(sys.argv[1:]) |