blob: 47f083df4baabbd67bb117e03b66ab5cca003485 [file] [log] [blame]
David Pursehouse8898e2f2012-11-14 07:51:03 +09001#!/usr/bin/env python
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
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
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
JoonCheol Parke9860722012-10-11 02:31:44 +090018import getpass
Conley Owensc9129d92012-10-01 16:12:28 -070019import imp
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -070020import netrc
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021import optparse
22import os
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023import sys
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070024import time
David Pursehouse59bbb582013-05-17 10:49:33 +090025
26from pyversion import is_python3
27if is_python3():
Sarah Owens1f7627f2012-10-31 09:21:55 -070028 import urllib.request
29else:
David Pursehouse59bbb582013-05-17 10:49:33 +090030 import urllib2
Sarah Owens1f7627f2012-10-31 09:21:55 -070031 urllib = imp.new_module('urllib')
32 urllib.request = urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033
Carlos Aguado1242e602014-02-03 13:48:47 +010034try:
35 import kerberos
36except ImportError:
37 kerberos = None
38
Mike Frysinger902665b2014-12-22 15:17:59 -050039from color import SetDefaultColoring
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070040from trace import SetTrace
Shawn O. Pearce334851e2011-09-19 08:05:31 -070041from git_command import git, GitCommand
Doug Anderson0048b692010-12-21 13:39:23 -080042from git_config import init_ssh, close_ssh
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080043from command import InteractiveCommand
44from command import MirrorSafeCommand
Shawn O. Pearceecff4f12011-11-29 15:01:33 -080045from subcmds.version import Version
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070046from editor import Editor
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070047from error import DownloadError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080048from error import ManifestInvalidRevisionError
David Pursehouse0b8df7b2012-11-13 09:51:57 +090049from error import ManifestParseError
Conley Owens75ee0572012-11-15 17:33:11 -080050from error import NoManifestException
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070051from error import NoSuchProjectError
52from error import RepoChangedException
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070053from manifest_xml import XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070054from pager import RunPager
Conley Owens094cdbe2014-01-30 15:09:59 -080055from wrapper import WrapperPath, Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070056
David Pursehouse5c6eeac2012-10-11 16:44:48 +090057from subcmds import all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070058
David Pursehouse59bbb582013-05-17 10:49:33 +090059if not is_python3():
60 # pylint:disable=W0622
Chirayu Desai217ea7d2013-03-01 19:14:38 +053061 input = raw_input
David Pursehouse59bbb582013-05-17 10:49:33 +090062 # pylint:enable=W0622
Chirayu Desai217ea7d2013-03-01 19:14:38 +053063
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070064global_options = optparse.OptionParser(
65 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
66 )
67global_options.add_option('-p', '--paginate',
68 dest='pager', action='store_true',
69 help='display command output in the pager')
70global_options.add_option('--no-pager',
71 dest='no_pager', action='store_true',
72 help='disable the pager')
Mike Frysinger902665b2014-12-22 15:17:59 -050073global_options.add_option('--color',
74 choices=('auto', 'always', 'never'), default=None,
75 help='control color usage: auto, always, never')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070076global_options.add_option('--trace',
77 dest='trace', action='store_true',
78 help='trace git command execution')
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070079global_options.add_option('--time',
80 dest='time', action='store_true',
81 help='time repo command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080082global_options.add_option('--version',
83 dest='show_version', action='store_true',
84 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070085
86class _Repo(object):
87 def __init__(self, repodir):
88 self.repodir = repodir
89 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -040090 # add 'branch' as an alias for 'branches'
91 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070092
93 def _Run(self, argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -040094 result = 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095 name = None
96 glob = []
97
Sarah Owensa6053d52012-11-01 13:36:50 -070098 for i in range(len(argv)):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070099 if not argv[i].startswith('-'):
100 name = argv[i]
101 if i > 0:
102 glob = argv[:i]
103 argv = argv[i + 1:]
104 break
105 if not name:
106 glob = argv
107 name = 'help'
108 argv = []
David Pursehouse8a68ff92012-09-24 12:15:13 +0900109 gopts, _gargs = global_options.parse_args(glob)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700110
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -0700111 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700112 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800113 if gopts.show_version:
114 if name == 'help':
115 name = 'version'
116 else:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700117 print('fatal: invalid usage of --version', file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400118 return 1
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800119
Mike Frysinger902665b2014-12-22 15:17:59 -0500120 SetDefaultColoring(gopts.color)
121
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700122 try:
123 cmd = self.commands[name]
124 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700125 print("repo: '%s' is not a repo command. See 'repo help'." % name,
126 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400127 return 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128
129 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700130 cmd.manifest = XmlManifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700131 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700132
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800133 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700134 print("fatal: '%s' requires a working directory" % name,
135 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400136 return 1
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800137
Dan Sandler53e902a2014-03-09 13:20:02 -0400138 try:
139 copts, cargs = cmd.OptionParser.parse_args(argv)
140 copts = cmd.ReadEnvironmentOptions(copts)
141 except NoManifestException as e:
142 print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
143 file=sys.stderr)
144 print('error: manifest missing or unreadable -- please run init',
145 file=sys.stderr)
146 return 1
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700147
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700148 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
149 config = cmd.manifest.globalConfig
150 if gopts.pager:
151 use_pager = True
152 else:
153 use_pager = config.GetBoolean('pager.%s' % name)
154 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700155 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700156 if use_pager:
157 RunPager(config)
158
Conley Owens7ba25be2012-11-14 14:18:06 -0800159 start = time.time()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700160 try:
Conley Owens7ba25be2012-11-14 14:18:06 -0800161 result = cmd.Execute(copts, cargs)
Dan Sandler53e902a2014-03-09 13:20:02 -0400162 except (DownloadError, ManifestInvalidRevisionError,
163 NoManifestException) as e:
164 print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
165 file=sys.stderr)
166 if isinstance(e, NoManifestException):
167 print('error: manifest missing or unreadable -- please run init',
168 file=sys.stderr)
Conley Owens75ee0572012-11-15 17:33:11 -0800169 result = 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700170 except NoSuchProjectError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700171 if e.name:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700172 print('error: project %s not found' % e.name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700173 else:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700174 print('error: no project in current directory', file=sys.stderr)
Conley Owens7ba25be2012-11-14 14:18:06 -0800175 result = 1
176 finally:
177 elapsed = time.time() - start
178 hours, remainder = divmod(elapsed, 3600)
179 minutes, seconds = divmod(remainder, 60)
180 if gopts.time:
181 if hours == 0:
182 print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr)
183 else:
184 print('real\t%dh%dm%.3fs' % (hours, minutes, seconds),
185 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400186
187 return result
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700188
Conley Owens094cdbe2014-01-30 15:09:59 -0800189
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700190def _MyRepoPath():
191 return os.path.dirname(__file__)
192
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700193
194def _CheckWrapperVersion(ver, repo_path):
195 if not repo_path:
196 repo_path = '~/bin/repo'
197
198 if not ver:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700199 print('no --wrapper-version argument', file=sys.stderr)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900200 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700201
Conley Owens094cdbe2014-01-30 15:09:59 -0800202 exp = Wrapper().VERSION
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900203 ver = tuple(map(int, ver.split('.')))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700204 if len(ver) == 1:
205 ver = (0, ver[0])
206
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900207 exp_str = '.'.join(map(str, exp))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700208 if exp[0] > ver[0] or ver < (0, 4):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700209 print("""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700210!!! A new repo command (%5s) is available. !!!
211!!! You must upgrade before you can continue: !!!
212
213 cp %s %s
Conley Owens094cdbe2014-01-30 15:09:59 -0800214""" % (exp_str, WrapperPath(), repo_path), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700215 sys.exit(1)
216
217 if exp > ver:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700218 print("""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700219... A new repo command (%5s) is available.
220... You should upgrade soon:
221
222 cp %s %s
Conley Owens094cdbe2014-01-30 15:09:59 -0800223""" % (exp_str, WrapperPath(), repo_path), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700224
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200225def _CheckRepoDir(repo_dir):
226 if not repo_dir:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700227 print('no --repo-dir argument', file=sys.stderr)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900228 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700229
230def _PruneOptions(argv, opt):
231 i = 0
232 while i < len(argv):
233 a = argv[i]
234 if a == '--':
235 break
236 if a.startswith('--'):
237 eq = a.find('=')
238 if eq > 0:
239 a = a[0:eq]
240 if not opt.has_option(a):
241 del argv[i]
242 continue
243 i += 1
244
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700245_user_agent = None
246
247def _UserAgent():
248 global _user_agent
249
250 if _user_agent is None:
251 py_version = sys.version_info
252
253 os_name = sys.platform
254 if os_name == 'linux2':
255 os_name = 'Linux'
256 elif os_name == 'win32':
257 os_name = 'Win32'
258 elif os_name == 'cygwin':
259 os_name = 'Cygwin'
260 elif os_name == 'darwin':
261 os_name = 'Darwin'
262
263 p = GitCommand(
264 None, ['describe', 'HEAD'],
265 cwd = _MyRepoPath(),
266 capture_stdout = True)
267 if p.Wait() == 0:
268 repo_version = p.stdout
269 if len(repo_version) > 0 and repo_version[-1] == '\n':
270 repo_version = repo_version[0:-1]
271 if len(repo_version) > 0 and repo_version[0] == 'v':
272 repo_version = repo_version[1:]
273 else:
274 repo_version = 'unknown'
275
276 _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
277 repo_version,
278 os_name,
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900279 '.'.join(map(str, git.version_tuple())),
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700280 py_version[0], py_version[1], py_version[2])
281 return _user_agent
282
Sarah Owens1f7627f2012-10-31 09:21:55 -0700283class _UserAgentHandler(urllib.request.BaseHandler):
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700284 def http_request(self, req):
285 req.add_header('User-Agent', _UserAgent())
286 return req
287
288 def https_request(self, req):
289 req.add_header('User-Agent', _UserAgent())
290 return req
291
JoonCheol Parke9860722012-10-11 02:31:44 +0900292def _AddPasswordFromUserInput(handler, msg, req):
David Pursehousec1b86a22012-11-14 11:36:51 +0900293 # If repo could not find auth info from netrc, try to get it from user input
294 url = req.get_full_url()
295 user, password = handler.passwd.find_user_password(None, url)
296 if user is None:
297 print(msg)
298 try:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530299 user = input('User: ')
David Pursehousec1b86a22012-11-14 11:36:51 +0900300 password = getpass.getpass()
301 except KeyboardInterrupt:
302 return
303 handler.passwd.add_password(None, url, user, password)
JoonCheol Parke9860722012-10-11 02:31:44 +0900304
Sarah Owens1f7627f2012-10-31 09:21:55 -0700305class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
JoonCheol Parke9860722012-10-11 02:31:44 +0900306 def http_error_401(self, req, fp, code, msg, headers):
307 _AddPasswordFromUserInput(self, msg, req)
Sarah Owens1f7627f2012-10-31 09:21:55 -0700308 return urllib.request.HTTPBasicAuthHandler.http_error_401(
JoonCheol Parke9860722012-10-11 02:31:44 +0900309 self, req, fp, code, msg, headers)
310
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700311 def http_error_auth_reqed(self, authreq, host, req, headers):
312 try:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700313 old_add_header = req.add_header
314 def _add_header(name, val):
315 val = val.replace('\n', '')
316 old_add_header(name, val)
317 req.add_header = _add_header
Sarah Owens1f7627f2012-10-31 09:21:55 -0700318 return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed(
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700319 self, authreq, host, req, headers)
320 except:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700321 reset = getattr(self, 'reset_retry_count', None)
322 if reset is not None:
323 reset()
Shawn O. Pearceb6605392011-10-11 15:58:07 -0700324 elif getattr(self, 'retried', None):
325 self.retried = 0
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700326 raise
327
Sarah Owens1f7627f2012-10-31 09:21:55 -0700328class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
JoonCheol Parke9860722012-10-11 02:31:44 +0900329 def http_error_401(self, req, fp, code, msg, headers):
330 _AddPasswordFromUserInput(self, msg, req)
Sarah Owens1f7627f2012-10-31 09:21:55 -0700331 return urllib.request.HTTPDigestAuthHandler.http_error_401(
JoonCheol Parke9860722012-10-11 02:31:44 +0900332 self, req, fp, code, msg, headers)
333
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800334 def http_error_auth_reqed(self, auth_header, host, req, headers):
335 try:
336 old_add_header = req.add_header
337 def _add_header(name, val):
338 val = val.replace('\n', '')
339 old_add_header(name, val)
340 req.add_header = _add_header
Sarah Owens1f7627f2012-10-31 09:21:55 -0700341 return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800342 self, auth_header, host, req, headers)
343 except:
344 reset = getattr(self, 'reset_retry_count', None)
345 if reset is not None:
346 reset()
347 elif getattr(self, 'retried', None):
348 self.retried = 0
349 raise
350
Carlos Aguado1242e602014-02-03 13:48:47 +0100351class _KerberosAuthHandler(urllib.request.BaseHandler):
352 def __init__(self):
353 self.retried = 0
354 self.context = None
355 self.handler_order = urllib.request.BaseHandler.handler_order - 50
356
357 def http_error_401(self, req, fp, code, msg, headers):
358 host = req.get_host()
359 retry = self.http_error_auth_reqed('www-authenticate', host, req, headers)
360 return retry
361
362 def http_error_auth_reqed(self, auth_header, host, req, headers):
363 try:
364 spn = "HTTP@%s" % host
365 authdata = self._negotiate_get_authdata(auth_header, headers)
366
367 if self.retried > 3:
368 raise urllib.request.HTTPError(req.get_full_url(), 401,
369 "Negotiate auth failed", headers, None)
370 else:
371 self.retried += 1
372
373 neghdr = self._negotiate_get_svctk(spn, authdata)
374 if neghdr is None:
375 return None
376
377 req.add_unredirected_header('Authorization', neghdr)
378 response = self.parent.open(req)
379
380 srvauth = self._negotiate_get_authdata(auth_header, response.info())
381 if self._validate_response(srvauth):
382 return response
383 except kerberos.GSSError:
384 return None
385 except:
386 self.reset_retry_count()
387 raise
388 finally:
389 self._clean_context()
390
391 def reset_retry_count(self):
392 self.retried = 0
393
394 def _negotiate_get_authdata(self, auth_header, headers):
395 authhdr = headers.get(auth_header, None)
396 if authhdr is not None:
397 for mech_tuple in authhdr.split(","):
398 mech, __, authdata = mech_tuple.strip().partition(" ")
399 if mech.lower() == "negotiate":
400 return authdata.strip()
401 return None
402
403 def _negotiate_get_svctk(self, spn, authdata):
404 if authdata is None:
405 return None
406
407 result, self.context = kerberos.authGSSClientInit(spn)
408 if result < kerberos.AUTH_GSS_COMPLETE:
409 return None
410
411 result = kerberos.authGSSClientStep(self.context, authdata)
412 if result < kerberos.AUTH_GSS_CONTINUE:
413 return None
414
415 response = kerberos.authGSSClientResponse(self.context)
416 return "Negotiate %s" % response
417
418 def _validate_response(self, authdata):
419 if authdata is None:
420 return None
421 result = kerberos.authGSSClientStep(self.context, authdata)
422 if result == kerberos.AUTH_GSS_COMPLETE:
423 return True
424 return None
425
426 def _clean_context(self):
427 if self.context is not None:
428 kerberos.authGSSClientClean(self.context)
429 self.context = None
430
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700431def init_http():
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700432 handlers = [_UserAgentHandler()]
433
Sarah Owens1f7627f2012-10-31 09:21:55 -0700434 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700435 try:
436 n = netrc.netrc()
437 for host in n.hosts:
438 p = n.hosts[host]
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800439 mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
440 mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700441 except netrc.NetrcParseError:
442 pass
Shawn O. Pearce7b947de2011-09-23 11:50:31 -0700443 except IOError:
444 pass
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700445 handlers.append(_BasicAuthHandler(mgr))
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800446 handlers.append(_DigestAuthHandler(mgr))
Carlos Aguado1242e602014-02-03 13:48:47 +0100447 if kerberos:
448 handlers.append(_KerberosAuthHandler())
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700449
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700450 if 'http_proxy' in os.environ:
451 url = os.environ['http_proxy']
Sarah Owens1f7627f2012-10-31 09:21:55 -0700452 handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700453 if 'REPO_CURL_VERBOSE' in os.environ:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700454 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
455 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
456 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700457
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700458def _Main(argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400459 result = 0
460
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700461 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
462 opt.add_option("--repo-dir", dest="repodir",
463 help="path to .repo/")
464 opt.add_option("--wrapper-version", dest="wrapper_version",
465 help="version of the wrapper script")
466 opt.add_option("--wrapper-path", dest="wrapper_path",
467 help="location of the wrapper script")
468 _PruneOptions(argv, opt)
469 opt, argv = opt.parse_args(argv)
470
471 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
472 _CheckRepoDir(opt.repodir)
473
Shawn O. Pearceecff4f12011-11-29 15:01:33 -0800474 Version.wrapper_version = opt.wrapper_version
475 Version.wrapper_path = opt.wrapper_path
476
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700477 repo = _Repo(opt.repodir)
478 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700479 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800480 init_ssh()
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700481 init_http()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400482 result = repo._Run(argv) or 0
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700483 finally:
484 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700485 except KeyboardInterrupt:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700486 print('aborted by user', file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400487 result = 1
David Pursehouse0b8df7b2012-11-13 09:51:57 +0900488 except ManifestParseError as mpe:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700489 print('fatal: %s' % mpe, file=sys.stderr)
David Pursehouse0b8df7b2012-11-13 09:51:57 +0900490 result = 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700491 except RepoChangedException as rce:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800492 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700493 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800494 argv = list(sys.argv)
495 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700496 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800497 os.execv(__file__, argv)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700498 except OSError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700499 print('fatal: cannot restart repo after upgrade', file=sys.stderr)
500 print('fatal: %s' % e, file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400501 result = 128
502
503 sys.exit(result)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700504
505if __name__ == '__main__':
506 _Main(sys.argv[1:])