blob: f965d7e14d1fa2bf604f20ff0b89c0df0431c43a [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
Dan Willemsen79360642015-08-31 15:45:06 -070045from command import GitcAvailableCommand, GitcClientCommand
Shawn O. Pearceecff4f12011-11-29 15:01:33 -080046from subcmds.version import Version
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070047from editor import Editor
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070048from error import DownloadError
Jarkko Pöyry87ea5912015-06-19 15:39:25 -070049from error import InvalidProjectGroupsError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080050from error import ManifestInvalidRevisionError
David Pursehouse0b8df7b2012-11-13 09:51:57 +090051from error import ManifestParseError
Conley Owens75ee0572012-11-15 17:33:11 -080052from error import NoManifestException
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070053from error import NoSuchProjectError
54from error import RepoChangedException
Simran Basib9a1b732015-08-20 12:19:28 -070055import gitc_utils
56from manifest_xml import GitcManifest, XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070057from pager import RunPager
Conley Owens094cdbe2014-01-30 15:09:59 -080058from wrapper import WrapperPath, Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070059
David Pursehouse5c6eeac2012-10-11 16:44:48 +090060from subcmds import all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070061
David Pursehouse59bbb582013-05-17 10:49:33 +090062if not is_python3():
63 # pylint:disable=W0622
Chirayu Desai217ea7d2013-03-01 19:14:38 +053064 input = raw_input
David Pursehouse59bbb582013-05-17 10:49:33 +090065 # pylint:enable=W0622
Chirayu Desai217ea7d2013-03-01 19:14:38 +053066
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070067global_options = optparse.OptionParser(
68 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
69 )
70global_options.add_option('-p', '--paginate',
71 dest='pager', action='store_true',
72 help='display command output in the pager')
73global_options.add_option('--no-pager',
74 dest='no_pager', action='store_true',
75 help='disable the pager')
Mike Frysinger902665b2014-12-22 15:17:59 -050076global_options.add_option('--color',
77 choices=('auto', 'always', 'never'), default=None,
78 help='control color usage: auto, always, never')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070079global_options.add_option('--trace',
80 dest='trace', action='store_true',
81 help='trace git command execution')
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070082global_options.add_option('--time',
83 dest='time', action='store_true',
84 help='time repo command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080085global_options.add_option('--version',
86 dest='show_version', action='store_true',
87 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088
89class _Repo(object):
90 def __init__(self, repodir):
91 self.repodir = repodir
92 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -040093 # add 'branch' as an alias for 'branches'
94 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095
96 def _Run(self, argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -040097 result = 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070098 name = None
99 glob = []
100
Sarah Owensa6053d52012-11-01 13:36:50 -0700101 for i in range(len(argv)):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102 if not argv[i].startswith('-'):
103 name = argv[i]
104 if i > 0:
105 glob = argv[:i]
106 argv = argv[i + 1:]
107 break
108 if not name:
109 glob = argv
110 name = 'help'
111 argv = []
David Pursehouse8a68ff92012-09-24 12:15:13 +0900112 gopts, _gargs = global_options.parse_args(glob)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -0700114 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700115 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800116 if gopts.show_version:
117 if name == 'help':
118 name = 'version'
119 else:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700120 print('fatal: invalid usage of --version', file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400121 return 1
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800122
Mike Frysinger902665b2014-12-22 15:17:59 -0500123 SetDefaultColoring(gopts.color)
124
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700125 try:
126 cmd = self.commands[name]
127 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700128 print("repo: '%s' is not a repo command. See 'repo help'." % name,
129 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400130 return 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131
132 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700133 cmd.manifest = XmlManifest(cmd.repodir)
Simran Basib9a1b732015-08-20 12:19:28 -0700134 cmd.gitc_manifest = None
135 gitc_client_name = gitc_utils.parse_clientdir(os.getcwd())
136 if gitc_client_name:
137 cmd.gitc_manifest = GitcManifest(cmd.repodir, gitc_client_name)
138 cmd.manifest.isGitcClient = True
139
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700140 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700141
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800142 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700143 print("fatal: '%s' requires a working directory" % name,
144 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400145 return 1
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800146
Dan Willemsen79360642015-08-31 15:45:06 -0700147 if isinstance(cmd, GitcAvailableCommand) and not gitc_utils.get_gitc_manifest_dir():
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700148 print("fatal: '%s' requires GITC to be available" % name,
149 file=sys.stderr)
150 return 1
151
Dan Willemsen79360642015-08-31 15:45:06 -0700152 if isinstance(cmd, GitcClientCommand) and not gitc_client_name:
153 print("fatal: '%s' requires a GITC client" % name,
154 file=sys.stderr)
155 return 1
156
Dan Sandler53e902a2014-03-09 13:20:02 -0400157 try:
158 copts, cargs = cmd.OptionParser.parse_args(argv)
159 copts = cmd.ReadEnvironmentOptions(copts)
160 except NoManifestException as e:
161 print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
162 file=sys.stderr)
163 print('error: manifest missing or unreadable -- please run init',
164 file=sys.stderr)
165 return 1
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700166
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700167 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
168 config = cmd.manifest.globalConfig
169 if gopts.pager:
170 use_pager = True
171 else:
172 use_pager = config.GetBoolean('pager.%s' % name)
173 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700174 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700175 if use_pager:
176 RunPager(config)
177
Conley Owens7ba25be2012-11-14 14:18:06 -0800178 start = time.time()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700179 try:
Conley Owens7ba25be2012-11-14 14:18:06 -0800180 result = cmd.Execute(copts, cargs)
Dan Sandler53e902a2014-03-09 13:20:02 -0400181 except (DownloadError, ManifestInvalidRevisionError,
182 NoManifestException) as e:
183 print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
184 file=sys.stderr)
185 if isinstance(e, NoManifestException):
186 print('error: manifest missing or unreadable -- please run init',
187 file=sys.stderr)
Conley Owens75ee0572012-11-15 17:33:11 -0800188 result = 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700189 except NoSuchProjectError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700190 if e.name:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700191 print('error: project %s not found' % e.name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700192 else:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700193 print('error: no project in current directory', file=sys.stderr)
Conley Owens7ba25be2012-11-14 14:18:06 -0800194 result = 1
Jarkko Pöyry87ea5912015-06-19 15:39:25 -0700195 except InvalidProjectGroupsError as e:
196 if e.name:
197 print('error: project group must be enabled for project %s' % e.name, file=sys.stderr)
198 else:
199 print('error: project group must be enabled for the project in the current directory', file=sys.stderr)
200 result = 1
David Rileyaa900212017-04-05 13:50:52 -0700201 except SystemExit as e:
202 if e.code:
203 result = e.code
204 raise
Conley Owens7ba25be2012-11-14 14:18:06 -0800205 finally:
206 elapsed = time.time() - start
207 hours, remainder = divmod(elapsed, 3600)
208 minutes, seconds = divmod(remainder, 60)
209 if gopts.time:
210 if hours == 0:
211 print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr)
212 else:
213 print('real\t%dh%dm%.3fs' % (hours, minutes, seconds),
214 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400215
216 return result
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700217
Conley Owens094cdbe2014-01-30 15:09:59 -0800218
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700219def _MyRepoPath():
220 return os.path.dirname(__file__)
221
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700222
223def _CheckWrapperVersion(ver, repo_path):
224 if not repo_path:
225 repo_path = '~/bin/repo'
226
227 if not ver:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700228 print('no --wrapper-version argument', file=sys.stderr)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900229 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700230
Conley Owens094cdbe2014-01-30 15:09:59 -0800231 exp = Wrapper().VERSION
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900232 ver = tuple(map(int, ver.split('.')))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700233 if len(ver) == 1:
234 ver = (0, ver[0])
235
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900236 exp_str = '.'.join(map(str, exp))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700237 if exp[0] > ver[0] or ver < (0, 4):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700238 print("""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700239!!! A new repo command (%5s) is available. !!!
240!!! You must upgrade before you can continue: !!!
241
242 cp %s %s
Conley Owens094cdbe2014-01-30 15:09:59 -0800243""" % (exp_str, WrapperPath(), repo_path), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700244 sys.exit(1)
245
246 if exp > ver:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700247 print("""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700248... A new repo command (%5s) is available.
249... You should upgrade soon:
250
251 cp %s %s
Conley Owens094cdbe2014-01-30 15:09:59 -0800252""" % (exp_str, WrapperPath(), repo_path), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700253
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200254def _CheckRepoDir(repo_dir):
255 if not repo_dir:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700256 print('no --repo-dir argument', file=sys.stderr)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900257 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700258
259def _PruneOptions(argv, opt):
260 i = 0
261 while i < len(argv):
262 a = argv[i]
263 if a == '--':
264 break
265 if a.startswith('--'):
266 eq = a.find('=')
267 if eq > 0:
268 a = a[0:eq]
269 if not opt.has_option(a):
270 del argv[i]
271 continue
272 i += 1
273
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700274_user_agent = None
275
276def _UserAgent():
277 global _user_agent
278
279 if _user_agent is None:
280 py_version = sys.version_info
281
282 os_name = sys.platform
283 if os_name == 'linux2':
284 os_name = 'Linux'
285 elif os_name == 'win32':
286 os_name = 'Win32'
287 elif os_name == 'cygwin':
288 os_name = 'Cygwin'
289 elif os_name == 'darwin':
290 os_name = 'Darwin'
291
292 p = GitCommand(
293 None, ['describe', 'HEAD'],
294 cwd = _MyRepoPath(),
295 capture_stdout = True)
296 if p.Wait() == 0:
297 repo_version = p.stdout
298 if len(repo_version) > 0 and repo_version[-1] == '\n':
299 repo_version = repo_version[0:-1]
300 if len(repo_version) > 0 and repo_version[0] == 'v':
301 repo_version = repo_version[1:]
302 else:
303 repo_version = 'unknown'
304
305 _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
306 repo_version,
307 os_name,
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900308 '.'.join(map(str, git.version_tuple())),
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700309 py_version[0], py_version[1], py_version[2])
310 return _user_agent
311
Sarah Owens1f7627f2012-10-31 09:21:55 -0700312class _UserAgentHandler(urllib.request.BaseHandler):
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700313 def http_request(self, req):
314 req.add_header('User-Agent', _UserAgent())
315 return req
316
317 def https_request(self, req):
318 req.add_header('User-Agent', _UserAgent())
319 return req
320
JoonCheol Parke9860722012-10-11 02:31:44 +0900321def _AddPasswordFromUserInput(handler, msg, req):
David Pursehousec1b86a22012-11-14 11:36:51 +0900322 # If repo could not find auth info from netrc, try to get it from user input
323 url = req.get_full_url()
324 user, password = handler.passwd.find_user_password(None, url)
325 if user is None:
326 print(msg)
327 try:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530328 user = input('User: ')
David Pursehousec1b86a22012-11-14 11:36:51 +0900329 password = getpass.getpass()
330 except KeyboardInterrupt:
331 return
332 handler.passwd.add_password(None, url, user, password)
JoonCheol Parke9860722012-10-11 02:31:44 +0900333
Sarah Owens1f7627f2012-10-31 09:21:55 -0700334class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
JoonCheol Parke9860722012-10-11 02:31:44 +0900335 def http_error_401(self, req, fp, code, msg, headers):
336 _AddPasswordFromUserInput(self, msg, req)
Sarah Owens1f7627f2012-10-31 09:21:55 -0700337 return urllib.request.HTTPBasicAuthHandler.http_error_401(
JoonCheol Parke9860722012-10-11 02:31:44 +0900338 self, req, fp, code, msg, headers)
339
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700340 def http_error_auth_reqed(self, authreq, host, req, headers):
341 try:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700342 old_add_header = req.add_header
343 def _add_header(name, val):
344 val = val.replace('\n', '')
345 old_add_header(name, val)
346 req.add_header = _add_header
Sarah Owens1f7627f2012-10-31 09:21:55 -0700347 return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed(
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700348 self, authreq, host, req, headers)
349 except:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700350 reset = getattr(self, 'reset_retry_count', None)
351 if reset is not None:
352 reset()
Shawn O. Pearceb6605392011-10-11 15:58:07 -0700353 elif getattr(self, 'retried', None):
354 self.retried = 0
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700355 raise
356
Sarah Owens1f7627f2012-10-31 09:21:55 -0700357class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
JoonCheol Parke9860722012-10-11 02:31:44 +0900358 def http_error_401(self, req, fp, code, msg, headers):
359 _AddPasswordFromUserInput(self, msg, req)
Sarah Owens1f7627f2012-10-31 09:21:55 -0700360 return urllib.request.HTTPDigestAuthHandler.http_error_401(
JoonCheol Parke9860722012-10-11 02:31:44 +0900361 self, req, fp, code, msg, headers)
362
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800363 def http_error_auth_reqed(self, auth_header, host, req, headers):
364 try:
365 old_add_header = req.add_header
366 def _add_header(name, val):
367 val = val.replace('\n', '')
368 old_add_header(name, val)
369 req.add_header = _add_header
Sarah Owens1f7627f2012-10-31 09:21:55 -0700370 return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800371 self, auth_header, host, req, headers)
372 except:
373 reset = getattr(self, 'reset_retry_count', None)
374 if reset is not None:
375 reset()
376 elif getattr(self, 'retried', None):
377 self.retried = 0
378 raise
379
Carlos Aguado1242e602014-02-03 13:48:47 +0100380class _KerberosAuthHandler(urllib.request.BaseHandler):
381 def __init__(self):
382 self.retried = 0
383 self.context = None
384 self.handler_order = urllib.request.BaseHandler.handler_order - 50
385
Stefan Beller9d2b14d2016-06-21 11:48:57 -0700386 def http_error_401(self, req, fp, code, msg, headers): # pylint:disable=unused-argument
Carlos Aguado1242e602014-02-03 13:48:47 +0100387 host = req.get_host()
388 retry = self.http_error_auth_reqed('www-authenticate', host, req, headers)
389 return retry
390
391 def http_error_auth_reqed(self, auth_header, host, req, headers):
392 try:
393 spn = "HTTP@%s" % host
394 authdata = self._negotiate_get_authdata(auth_header, headers)
395
396 if self.retried > 3:
397 raise urllib.request.HTTPError(req.get_full_url(), 401,
398 "Negotiate auth failed", headers, None)
399 else:
400 self.retried += 1
401
402 neghdr = self._negotiate_get_svctk(spn, authdata)
403 if neghdr is None:
404 return None
405
406 req.add_unredirected_header('Authorization', neghdr)
407 response = self.parent.open(req)
408
409 srvauth = self._negotiate_get_authdata(auth_header, response.info())
410 if self._validate_response(srvauth):
411 return response
412 except kerberos.GSSError:
413 return None
414 except:
415 self.reset_retry_count()
416 raise
417 finally:
418 self._clean_context()
419
420 def reset_retry_count(self):
421 self.retried = 0
422
423 def _negotiate_get_authdata(self, auth_header, headers):
424 authhdr = headers.get(auth_header, None)
425 if authhdr is not None:
426 for mech_tuple in authhdr.split(","):
427 mech, __, authdata = mech_tuple.strip().partition(" ")
428 if mech.lower() == "negotiate":
429 return authdata.strip()
430 return None
431
432 def _negotiate_get_svctk(self, spn, authdata):
433 if authdata is None:
434 return None
435
436 result, self.context = kerberos.authGSSClientInit(spn)
437 if result < kerberos.AUTH_GSS_COMPLETE:
438 return None
439
440 result = kerberos.authGSSClientStep(self.context, authdata)
441 if result < kerberos.AUTH_GSS_CONTINUE:
442 return None
443
444 response = kerberos.authGSSClientResponse(self.context)
445 return "Negotiate %s" % response
446
447 def _validate_response(self, authdata):
448 if authdata is None:
449 return None
450 result = kerberos.authGSSClientStep(self.context, authdata)
451 if result == kerberos.AUTH_GSS_COMPLETE:
452 return True
453 return None
454
455 def _clean_context(self):
456 if self.context is not None:
457 kerberos.authGSSClientClean(self.context)
458 self.context = None
459
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700460def init_http():
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700461 handlers = [_UserAgentHandler()]
462
Sarah Owens1f7627f2012-10-31 09:21:55 -0700463 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700464 try:
465 n = netrc.netrc()
466 for host in n.hosts:
467 p = n.hosts[host]
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800468 mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
469 mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700470 except netrc.NetrcParseError:
471 pass
Shawn O. Pearce7b947de2011-09-23 11:50:31 -0700472 except IOError:
473 pass
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700474 handlers.append(_BasicAuthHandler(mgr))
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800475 handlers.append(_DigestAuthHandler(mgr))
Carlos Aguado1242e602014-02-03 13:48:47 +0100476 if kerberos:
477 handlers.append(_KerberosAuthHandler())
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700478
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700479 if 'http_proxy' in os.environ:
480 url = os.environ['http_proxy']
Sarah Owens1f7627f2012-10-31 09:21:55 -0700481 handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700482 if 'REPO_CURL_VERBOSE' in os.environ:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700483 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
484 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
485 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700486
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700487def _Main(argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400488 result = 0
489
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700490 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
491 opt.add_option("--repo-dir", dest="repodir",
492 help="path to .repo/")
493 opt.add_option("--wrapper-version", dest="wrapper_version",
494 help="version of the wrapper script")
495 opt.add_option("--wrapper-path", dest="wrapper_path",
496 help="location of the wrapper script")
497 _PruneOptions(argv, opt)
498 opt, argv = opt.parse_args(argv)
499
500 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
501 _CheckRepoDir(opt.repodir)
502
Shawn O. Pearceecff4f12011-11-29 15:01:33 -0800503 Version.wrapper_version = opt.wrapper_version
504 Version.wrapper_path = opt.wrapper_path
505
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700506 repo = _Repo(opt.repodir)
507 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700508 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800509 init_ssh()
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700510 init_http()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400511 result = repo._Run(argv) or 0
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700512 finally:
513 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700514 except KeyboardInterrupt:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700515 print('aborted by user', file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400516 result = 1
David Pursehouse0b8df7b2012-11-13 09:51:57 +0900517 except ManifestParseError as mpe:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700518 print('fatal: %s' % mpe, file=sys.stderr)
David Pursehouse0b8df7b2012-11-13 09:51:57 +0900519 result = 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700520 except RepoChangedException as rce:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800521 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700522 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800523 argv = list(sys.argv)
524 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700525 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800526 os.execv(__file__, argv)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700527 except OSError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700528 print('fatal: cannot restart repo after upgrade', file=sys.stderr)
529 print('fatal: %s' % e, file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400530 result = 128
531
532 sys.exit(result)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700533
534if __name__ == '__main__':
535 _Main(sys.argv[1:])