blob: adfaffb0a0266f5ddceaec3b9317c94a6cedceeb [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
Jarkko Pöyry87ea5912015-06-19 15:39:25 -070048from error import InvalidProjectGroupsError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080049from error import ManifestInvalidRevisionError
David Pursehouse0b8df7b2012-11-13 09:51:57 +090050from error import ManifestParseError
Conley Owens75ee0572012-11-15 17:33:11 -080051from error import NoManifestException
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070052from error import NoSuchProjectError
53from error import RepoChangedException
Simran Basib9a1b732015-08-20 12:19:28 -070054import gitc_utils
55from manifest_xml import GitcManifest, XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070056from pager import RunPager
Conley Owens094cdbe2014-01-30 15:09:59 -080057from wrapper import WrapperPath, Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070058
David Pursehouse5c6eeac2012-10-11 16:44:48 +090059from subcmds import all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070060
David Pursehouse59bbb582013-05-17 10:49:33 +090061if not is_python3():
62 # pylint:disable=W0622
Chirayu Desai217ea7d2013-03-01 19:14:38 +053063 input = raw_input
David Pursehouse59bbb582013-05-17 10:49:33 +090064 # pylint:enable=W0622
Chirayu Desai217ea7d2013-03-01 19:14:38 +053065
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066global_options = optparse.OptionParser(
67 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
68 )
69global_options.add_option('-p', '--paginate',
70 dest='pager', action='store_true',
71 help='display command output in the pager')
72global_options.add_option('--no-pager',
73 dest='no_pager', action='store_true',
74 help='disable the pager')
Mike Frysinger902665b2014-12-22 15:17:59 -050075global_options.add_option('--color',
76 choices=('auto', 'always', 'never'), default=None,
77 help='control color usage: auto, always, never')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070078global_options.add_option('--trace',
79 dest='trace', action='store_true',
80 help='trace git command execution')
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070081global_options.add_option('--time',
82 dest='time', action='store_true',
83 help='time repo command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080084global_options.add_option('--version',
85 dest='show_version', action='store_true',
86 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070087
88class _Repo(object):
89 def __init__(self, repodir):
90 self.repodir = repodir
91 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -040092 # add 'branch' as an alias for 'branches'
93 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070094
95 def _Run(self, argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -040096 result = 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097 name = None
98 glob = []
99
Sarah Owensa6053d52012-11-01 13:36:50 -0700100 for i in range(len(argv)):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101 if not argv[i].startswith('-'):
102 name = argv[i]
103 if i > 0:
104 glob = argv[:i]
105 argv = argv[i + 1:]
106 break
107 if not name:
108 glob = argv
109 name = 'help'
110 argv = []
David Pursehouse8a68ff92012-09-24 12:15:13 +0900111 gopts, _gargs = global_options.parse_args(glob)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700112
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -0700113 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700114 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800115 if gopts.show_version:
116 if name == 'help':
117 name = 'version'
118 else:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700119 print('fatal: invalid usage of --version', file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400120 return 1
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800121
Mike Frysinger902665b2014-12-22 15:17:59 -0500122 SetDefaultColoring(gopts.color)
123
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700124 try:
125 cmd = self.commands[name]
126 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700127 print("repo: '%s' is not a repo command. See 'repo help'." % name,
128 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400129 return 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700130
131 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700132 cmd.manifest = XmlManifest(cmd.repodir)
Simran Basib9a1b732015-08-20 12:19:28 -0700133 cmd.gitc_manifest = None
134 gitc_client_name = gitc_utils.parse_clientdir(os.getcwd())
135 if gitc_client_name:
136 cmd.gitc_manifest = GitcManifest(cmd.repodir, gitc_client_name)
137 cmd.manifest.isGitcClient = True
138
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700139 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800141 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700142 print("fatal: '%s' requires a working directory" % name,
143 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400144 return 1
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800145
Dan Sandler53e902a2014-03-09 13:20:02 -0400146 try:
147 copts, cargs = cmd.OptionParser.parse_args(argv)
148 copts = cmd.ReadEnvironmentOptions(copts)
149 except NoManifestException as e:
150 print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
151 file=sys.stderr)
152 print('error: manifest missing or unreadable -- please run init',
153 file=sys.stderr)
154 return 1
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700155
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700156 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
157 config = cmd.manifest.globalConfig
158 if gopts.pager:
159 use_pager = True
160 else:
161 use_pager = config.GetBoolean('pager.%s' % name)
162 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700163 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700164 if use_pager:
165 RunPager(config)
166
Conley Owens7ba25be2012-11-14 14:18:06 -0800167 start = time.time()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700168 try:
Conley Owens7ba25be2012-11-14 14:18:06 -0800169 result = cmd.Execute(copts, cargs)
Dan Sandler53e902a2014-03-09 13:20:02 -0400170 except (DownloadError, ManifestInvalidRevisionError,
171 NoManifestException) as e:
172 print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
173 file=sys.stderr)
174 if isinstance(e, NoManifestException):
175 print('error: manifest missing or unreadable -- please run init',
176 file=sys.stderr)
Conley Owens75ee0572012-11-15 17:33:11 -0800177 result = 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700178 except NoSuchProjectError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700179 if e.name:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700180 print('error: project %s not found' % e.name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700181 else:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700182 print('error: no project in current directory', file=sys.stderr)
Conley Owens7ba25be2012-11-14 14:18:06 -0800183 result = 1
Jarkko Pöyry87ea5912015-06-19 15:39:25 -0700184 except InvalidProjectGroupsError as e:
185 if e.name:
186 print('error: project group must be enabled for project %s' % e.name, file=sys.stderr)
187 else:
188 print('error: project group must be enabled for the project in the current directory', file=sys.stderr)
189 result = 1
Conley Owens7ba25be2012-11-14 14:18:06 -0800190 finally:
191 elapsed = time.time() - start
192 hours, remainder = divmod(elapsed, 3600)
193 minutes, seconds = divmod(remainder, 60)
194 if gopts.time:
195 if hours == 0:
196 print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr)
197 else:
198 print('real\t%dh%dm%.3fs' % (hours, minutes, seconds),
199 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400200
201 return result
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700202
Conley Owens094cdbe2014-01-30 15:09:59 -0800203
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700204def _MyRepoPath():
205 return os.path.dirname(__file__)
206
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700207
208def _CheckWrapperVersion(ver, repo_path):
209 if not repo_path:
210 repo_path = '~/bin/repo'
211
212 if not ver:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700213 print('no --wrapper-version argument', file=sys.stderr)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900214 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700215
Conley Owens094cdbe2014-01-30 15:09:59 -0800216 exp = Wrapper().VERSION
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900217 ver = tuple(map(int, ver.split('.')))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700218 if len(ver) == 1:
219 ver = (0, ver[0])
220
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900221 exp_str = '.'.join(map(str, exp))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700222 if exp[0] > ver[0] or ver < (0, 4):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700223 print("""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700224!!! A new repo command (%5s) is available. !!!
225!!! You must upgrade before you can continue: !!!
226
227 cp %s %s
Conley Owens094cdbe2014-01-30 15:09:59 -0800228""" % (exp_str, WrapperPath(), repo_path), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700229 sys.exit(1)
230
231 if exp > ver:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700232 print("""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700233... A new repo command (%5s) is available.
234... You should upgrade soon:
235
236 cp %s %s
Conley Owens094cdbe2014-01-30 15:09:59 -0800237""" % (exp_str, WrapperPath(), repo_path), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700238
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200239def _CheckRepoDir(repo_dir):
240 if not repo_dir:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700241 print('no --repo-dir argument', file=sys.stderr)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900242 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700243
244def _PruneOptions(argv, opt):
245 i = 0
246 while i < len(argv):
247 a = argv[i]
248 if a == '--':
249 break
250 if a.startswith('--'):
251 eq = a.find('=')
252 if eq > 0:
253 a = a[0:eq]
254 if not opt.has_option(a):
255 del argv[i]
256 continue
257 i += 1
258
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700259_user_agent = None
260
261def _UserAgent():
262 global _user_agent
263
264 if _user_agent is None:
265 py_version = sys.version_info
266
267 os_name = sys.platform
268 if os_name == 'linux2':
269 os_name = 'Linux'
270 elif os_name == 'win32':
271 os_name = 'Win32'
272 elif os_name == 'cygwin':
273 os_name = 'Cygwin'
274 elif os_name == 'darwin':
275 os_name = 'Darwin'
276
277 p = GitCommand(
278 None, ['describe', 'HEAD'],
279 cwd = _MyRepoPath(),
280 capture_stdout = True)
281 if p.Wait() == 0:
282 repo_version = p.stdout
283 if len(repo_version) > 0 and repo_version[-1] == '\n':
284 repo_version = repo_version[0:-1]
285 if len(repo_version) > 0 and repo_version[0] == 'v':
286 repo_version = repo_version[1:]
287 else:
288 repo_version = 'unknown'
289
290 _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
291 repo_version,
292 os_name,
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900293 '.'.join(map(str, git.version_tuple())),
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700294 py_version[0], py_version[1], py_version[2])
295 return _user_agent
296
Sarah Owens1f7627f2012-10-31 09:21:55 -0700297class _UserAgentHandler(urllib.request.BaseHandler):
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700298 def http_request(self, req):
299 req.add_header('User-Agent', _UserAgent())
300 return req
301
302 def https_request(self, req):
303 req.add_header('User-Agent', _UserAgent())
304 return req
305
JoonCheol Parke9860722012-10-11 02:31:44 +0900306def _AddPasswordFromUserInput(handler, msg, req):
David Pursehousec1b86a22012-11-14 11:36:51 +0900307 # If repo could not find auth info from netrc, try to get it from user input
308 url = req.get_full_url()
309 user, password = handler.passwd.find_user_password(None, url)
310 if user is None:
311 print(msg)
312 try:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530313 user = input('User: ')
David Pursehousec1b86a22012-11-14 11:36:51 +0900314 password = getpass.getpass()
315 except KeyboardInterrupt:
316 return
317 handler.passwd.add_password(None, url, user, password)
JoonCheol Parke9860722012-10-11 02:31:44 +0900318
Sarah Owens1f7627f2012-10-31 09:21:55 -0700319class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
JoonCheol Parke9860722012-10-11 02:31:44 +0900320 def http_error_401(self, req, fp, code, msg, headers):
321 _AddPasswordFromUserInput(self, msg, req)
Sarah Owens1f7627f2012-10-31 09:21:55 -0700322 return urllib.request.HTTPBasicAuthHandler.http_error_401(
JoonCheol Parke9860722012-10-11 02:31:44 +0900323 self, req, fp, code, msg, headers)
324
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700325 def http_error_auth_reqed(self, authreq, host, req, headers):
326 try:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700327 old_add_header = req.add_header
328 def _add_header(name, val):
329 val = val.replace('\n', '')
330 old_add_header(name, val)
331 req.add_header = _add_header
Sarah Owens1f7627f2012-10-31 09:21:55 -0700332 return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed(
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700333 self, authreq, host, req, headers)
334 except:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700335 reset = getattr(self, 'reset_retry_count', None)
336 if reset is not None:
337 reset()
Shawn O. Pearceb6605392011-10-11 15:58:07 -0700338 elif getattr(self, 'retried', None):
339 self.retried = 0
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700340 raise
341
Sarah Owens1f7627f2012-10-31 09:21:55 -0700342class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
JoonCheol Parke9860722012-10-11 02:31:44 +0900343 def http_error_401(self, req, fp, code, msg, headers):
344 _AddPasswordFromUserInput(self, msg, req)
Sarah Owens1f7627f2012-10-31 09:21:55 -0700345 return urllib.request.HTTPDigestAuthHandler.http_error_401(
JoonCheol Parke9860722012-10-11 02:31:44 +0900346 self, req, fp, code, msg, headers)
347
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800348 def http_error_auth_reqed(self, auth_header, host, req, headers):
349 try:
350 old_add_header = req.add_header
351 def _add_header(name, val):
352 val = val.replace('\n', '')
353 old_add_header(name, val)
354 req.add_header = _add_header
Sarah Owens1f7627f2012-10-31 09:21:55 -0700355 return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800356 self, auth_header, host, req, headers)
357 except:
358 reset = getattr(self, 'reset_retry_count', None)
359 if reset is not None:
360 reset()
361 elif getattr(self, 'retried', None):
362 self.retried = 0
363 raise
364
Carlos Aguado1242e602014-02-03 13:48:47 +0100365class _KerberosAuthHandler(urllib.request.BaseHandler):
366 def __init__(self):
367 self.retried = 0
368 self.context = None
369 self.handler_order = urllib.request.BaseHandler.handler_order - 50
370
371 def http_error_401(self, req, fp, code, msg, headers):
372 host = req.get_host()
373 retry = self.http_error_auth_reqed('www-authenticate', host, req, headers)
374 return retry
375
376 def http_error_auth_reqed(self, auth_header, host, req, headers):
377 try:
378 spn = "HTTP@%s" % host
379 authdata = self._negotiate_get_authdata(auth_header, headers)
380
381 if self.retried > 3:
382 raise urllib.request.HTTPError(req.get_full_url(), 401,
383 "Negotiate auth failed", headers, None)
384 else:
385 self.retried += 1
386
387 neghdr = self._negotiate_get_svctk(spn, authdata)
388 if neghdr is None:
389 return None
390
391 req.add_unredirected_header('Authorization', neghdr)
392 response = self.parent.open(req)
393
394 srvauth = self._negotiate_get_authdata(auth_header, response.info())
395 if self._validate_response(srvauth):
396 return response
397 except kerberos.GSSError:
398 return None
399 except:
400 self.reset_retry_count()
401 raise
402 finally:
403 self._clean_context()
404
405 def reset_retry_count(self):
406 self.retried = 0
407
408 def _negotiate_get_authdata(self, auth_header, headers):
409 authhdr = headers.get(auth_header, None)
410 if authhdr is not None:
411 for mech_tuple in authhdr.split(","):
412 mech, __, authdata = mech_tuple.strip().partition(" ")
413 if mech.lower() == "negotiate":
414 return authdata.strip()
415 return None
416
417 def _negotiate_get_svctk(self, spn, authdata):
418 if authdata is None:
419 return None
420
421 result, self.context = kerberos.authGSSClientInit(spn)
422 if result < kerberos.AUTH_GSS_COMPLETE:
423 return None
424
425 result = kerberos.authGSSClientStep(self.context, authdata)
426 if result < kerberos.AUTH_GSS_CONTINUE:
427 return None
428
429 response = kerberos.authGSSClientResponse(self.context)
430 return "Negotiate %s" % response
431
432 def _validate_response(self, authdata):
433 if authdata is None:
434 return None
435 result = kerberos.authGSSClientStep(self.context, authdata)
436 if result == kerberos.AUTH_GSS_COMPLETE:
437 return True
438 return None
439
440 def _clean_context(self):
441 if self.context is not None:
442 kerberos.authGSSClientClean(self.context)
443 self.context = None
444
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700445def init_http():
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700446 handlers = [_UserAgentHandler()]
447
Sarah Owens1f7627f2012-10-31 09:21:55 -0700448 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700449 try:
450 n = netrc.netrc()
451 for host in n.hosts:
452 p = n.hosts[host]
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800453 mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
454 mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700455 except netrc.NetrcParseError:
456 pass
Shawn O. Pearce7b947de2011-09-23 11:50:31 -0700457 except IOError:
458 pass
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700459 handlers.append(_BasicAuthHandler(mgr))
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800460 handlers.append(_DigestAuthHandler(mgr))
Carlos Aguado1242e602014-02-03 13:48:47 +0100461 if kerberos:
462 handlers.append(_KerberosAuthHandler())
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700463
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700464 if 'http_proxy' in os.environ:
465 url = os.environ['http_proxy']
Sarah Owens1f7627f2012-10-31 09:21:55 -0700466 handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700467 if 'REPO_CURL_VERBOSE' in os.environ:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700468 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
469 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
470 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700471
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700472def _Main(argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400473 result = 0
474
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700475 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
476 opt.add_option("--repo-dir", dest="repodir",
477 help="path to .repo/")
478 opt.add_option("--wrapper-version", dest="wrapper_version",
479 help="version of the wrapper script")
480 opt.add_option("--wrapper-path", dest="wrapper_path",
481 help="location of the wrapper script")
482 _PruneOptions(argv, opt)
483 opt, argv = opt.parse_args(argv)
484
485 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
486 _CheckRepoDir(opt.repodir)
487
Shawn O. Pearceecff4f12011-11-29 15:01:33 -0800488 Version.wrapper_version = opt.wrapper_version
489 Version.wrapper_path = opt.wrapper_path
490
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700491 repo = _Repo(opt.repodir)
492 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700493 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800494 init_ssh()
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700495 init_http()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400496 result = repo._Run(argv) or 0
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700497 finally:
498 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700499 except KeyboardInterrupt:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700500 print('aborted by user', file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400501 result = 1
David Pursehouse0b8df7b2012-11-13 09:51:57 +0900502 except ManifestParseError as mpe:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700503 print('fatal: %s' % mpe, file=sys.stderr)
David Pursehouse0b8df7b2012-11-13 09:51:57 +0900504 result = 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700505 except RepoChangedException as rce:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800506 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700507 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800508 argv = list(sys.argv)
509 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700510 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800511 os.execv(__file__, argv)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700512 except OSError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700513 print('fatal: cannot restart repo after upgrade', file=sys.stderr)
514 print('fatal: %s' % e, file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400515 result = 128
516
517 sys.exit(result)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700518
519if __name__ == '__main__':
520 _Main(sys.argv[1:])