blob: 6e74d5a48f2b9c4d9518bd9302ecd5530996b4e0 [file] [log] [blame]
David Pursehouse8898e2f2012-11-14 07:51:03 +09001#!/usr/bin/env python
Mike Frysingerf6013762019-06-13 02:30:51 -04002# -*- coding:utf-8 -*-
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07003#
4# Copyright (C) 2008 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
Mike Frysinger87fb5a12019-06-13 01:54:46 -040018"""The repo tool.
19
20People shouldn't run this directly; instead, they should use the `repo` wrapper
21which takes care of execing this entry point.
22"""
23
Sarah Owenscecd1d82012-11-01 22:59:27 -070024from __future__ import print_function
JoonCheol Parke9860722012-10-11 02:31:44 +090025import getpass
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -070026import netrc
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027import optparse
28import os
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029import sys
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070030import time
David Pursehouse59bbb582013-05-17 10:49:33 +090031
32from pyversion import is_python3
33if is_python3():
Sarah Owens1f7627f2012-10-31 09:21:55 -070034 import urllib.request
35else:
Rashed Abdel-Tawab2058c632019-10-05 00:18:41 -040036 import imp
David Pursehouse59bbb582013-05-17 10:49:33 +090037 import urllib2
Sarah Owens1f7627f2012-10-31 09:21:55 -070038 urllib = imp.new_module('urllib')
39 urllib.request = urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040
Carlos Aguado1242e602014-02-03 13:48:47 +010041try:
42 import kerberos
43except ImportError:
44 kerberos = None
45
Mike Frysinger902665b2014-12-22 15:17:59 -050046from color import SetDefaultColoring
David Rileye0684ad2017-04-05 00:02:59 -070047import event_log
Mike Frysinger8a11f6f2019-08-27 00:26:15 -040048from repo_trace import SetTrace
Mike Frysinger71b0f312019-09-30 22:39:49 -040049from git_command import git, GitCommand, user_agent
Doug Anderson0048b692010-12-21 13:39:23 -080050from git_config import init_ssh, close_ssh
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080051from command import InteractiveCommand
52from command import MirrorSafeCommand
Dan Willemsen79360642015-08-31 15:45:06 -070053from command import GitcAvailableCommand, GitcClientCommand
Shawn O. Pearceecff4f12011-11-29 15:01:33 -080054from subcmds.version import Version
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070055from editor import Editor
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070056from error import DownloadError
Jarkko Pöyry87ea5912015-06-19 15:39:25 -070057from error import InvalidProjectGroupsError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080058from error import ManifestInvalidRevisionError
David Pursehouse0b8df7b2012-11-13 09:51:57 +090059from error import ManifestParseError
Conley Owens75ee0572012-11-15 17:33:11 -080060from error import NoManifestException
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070061from error import NoSuchProjectError
62from error import RepoChangedException
Simran Basib9a1b732015-08-20 12:19:28 -070063import gitc_utils
64from manifest_xml import GitcManifest, XmlManifest
Renaud Paquaye8595e92016-11-01 15:51:59 -070065from pager import RunPager, TerminatePager
Conley Owens094cdbe2014-01-30 15:09:59 -080066from wrapper import WrapperPath, Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070067
David Pursehouse5c6eeac2012-10-11 16:44:48 +090068from subcmds import all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070069
David Pursehouse59bbb582013-05-17 10:49:33 +090070if not is_python3():
Chirayu Desai217ea7d2013-03-01 19:14:38 +053071 input = raw_input
Chirayu Desai217ea7d2013-03-01 19:14:38 +053072
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070073global_options = optparse.OptionParser(
74 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
75 )
76global_options.add_option('-p', '--paginate',
77 dest='pager', action='store_true',
78 help='display command output in the pager')
79global_options.add_option('--no-pager',
80 dest='no_pager', action='store_true',
81 help='disable the pager')
Mike Frysinger902665b2014-12-22 15:17:59 -050082global_options.add_option('--color',
83 choices=('auto', 'always', 'never'), default=None,
84 help='control color usage: auto, always, never')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070085global_options.add_option('--trace',
86 dest='trace', action='store_true',
Mike Frysinger8a11f6f2019-08-27 00:26:15 -040087 help='trace git command execution (REPO_TRACE=1)')
Mike Frysinger3fc15722019-08-27 00:36:46 -040088global_options.add_option('--trace-python',
89 dest='trace_python', action='store_true',
90 help='trace python command execution')
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070091global_options.add_option('--time',
92 dest='time', action='store_true',
93 help='time repo command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080094global_options.add_option('--version',
95 dest='show_version', action='store_true',
96 help='display this version of repo')
David Rileye0684ad2017-04-05 00:02:59 -070097global_options.add_option('--event-log',
98 dest='event_log', action='store',
99 help='filename of event log to append timeline to')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700100
101class _Repo(object):
102 def __init__(self, repodir):
103 self.repodir = repodir
104 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -0400105 # add 'branch' as an alias for 'branches'
106 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700107
Mike Frysinger3fc15722019-08-27 00:36:46 -0400108 def _ParseArgs(self, argv):
109 """Parse the main `repo` command line options."""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700110 name = None
111 glob = []
112
Sarah Owensa6053d52012-11-01 13:36:50 -0700113 for i in range(len(argv)):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700114 if not argv[i].startswith('-'):
115 name = argv[i]
116 if i > 0:
117 glob = argv[:i]
118 argv = argv[i + 1:]
119 break
120 if not name:
121 glob = argv
122 name = 'help'
123 argv = []
David Pursehouse8a68ff92012-09-24 12:15:13 +0900124 gopts, _gargs = global_options.parse_args(glob)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700125
Mike Frysinger3fc15722019-08-27 00:36:46 -0400126 return (name, gopts, argv)
127
128 def _Run(self, name, gopts, argv):
129 """Execute the requested subcommand."""
130 result = 0
131
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -0700132 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700133 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800134 if gopts.show_version:
135 if name == 'help':
136 name = 'version'
137 else:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700138 print('fatal: invalid usage of --version', file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400139 return 1
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800140
Mike Frysinger902665b2014-12-22 15:17:59 -0500141 SetDefaultColoring(gopts.color)
142
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700143 try:
144 cmd = self.commands[name]
145 except KeyError:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700146 print("repo: '%s' is not a repo command. See 'repo help'." % name,
147 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400148 return 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700149
150 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700151 cmd.manifest = XmlManifest(cmd.repodir)
Simran Basib9a1b732015-08-20 12:19:28 -0700152 cmd.gitc_manifest = None
153 gitc_client_name = gitc_utils.parse_clientdir(os.getcwd())
154 if gitc_client_name:
155 cmd.gitc_manifest = GitcManifest(cmd.repodir, gitc_client_name)
156 cmd.manifest.isGitcClient = True
157
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700158 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700159
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800160 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700161 print("fatal: '%s' requires a working directory" % name,
162 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400163 return 1
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800164
Dan Willemsen79360642015-08-31 15:45:06 -0700165 if isinstance(cmd, GitcAvailableCommand) and not gitc_utils.get_gitc_manifest_dir():
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700166 print("fatal: '%s' requires GITC to be available" % name,
167 file=sys.stderr)
168 return 1
169
Dan Willemsen79360642015-08-31 15:45:06 -0700170 if isinstance(cmd, GitcClientCommand) and not gitc_client_name:
171 print("fatal: '%s' requires a GITC client" % name,
172 file=sys.stderr)
173 return 1
174
Dan Sandler53e902a2014-03-09 13:20:02 -0400175 try:
176 copts, cargs = cmd.OptionParser.parse_args(argv)
177 copts = cmd.ReadEnvironmentOptions(copts)
178 except NoManifestException as e:
179 print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
180 file=sys.stderr)
181 print('error: manifest missing or unreadable -- please run init',
182 file=sys.stderr)
183 return 1
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700184
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700185 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
186 config = cmd.manifest.globalConfig
187 if gopts.pager:
188 use_pager = True
189 else:
190 use_pager = config.GetBoolean('pager.%s' % name)
191 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700192 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700193 if use_pager:
194 RunPager(config)
195
Conley Owens7ba25be2012-11-14 14:18:06 -0800196 start = time.time()
David Rileye0684ad2017-04-05 00:02:59 -0700197 cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start)
198 cmd.event_log.SetParent(cmd_event)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700199 try:
Mike Frysingerae6cb082019-08-27 01:10:59 -0400200 cmd.ValidateOptions(copts, cargs)
Conley Owens7ba25be2012-11-14 14:18:06 -0800201 result = cmd.Execute(copts, cargs)
Dan Sandler53e902a2014-03-09 13:20:02 -0400202 except (DownloadError, ManifestInvalidRevisionError,
203 NoManifestException) as e:
204 print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
205 file=sys.stderr)
206 if isinstance(e, NoManifestException):
207 print('error: manifest missing or unreadable -- please run init',
208 file=sys.stderr)
Conley Owens75ee0572012-11-15 17:33:11 -0800209 result = 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700210 except NoSuchProjectError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700211 if e.name:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700212 print('error: project %s not found' % e.name, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700213 else:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700214 print('error: no project in current directory', file=sys.stderr)
Conley Owens7ba25be2012-11-14 14:18:06 -0800215 result = 1
Jarkko Pöyry87ea5912015-06-19 15:39:25 -0700216 except InvalidProjectGroupsError as e:
217 if e.name:
218 print('error: project group must be enabled for project %s' % e.name, file=sys.stderr)
219 else:
220 print('error: project group must be enabled for the project in the current directory', file=sys.stderr)
221 result = 1
David Rileyaa900212017-04-05 13:50:52 -0700222 except SystemExit as e:
223 if e.code:
224 result = e.code
225 raise
Conley Owens7ba25be2012-11-14 14:18:06 -0800226 finally:
David Rileye0684ad2017-04-05 00:02:59 -0700227 finish = time.time()
228 elapsed = finish - start
Conley Owens7ba25be2012-11-14 14:18:06 -0800229 hours, remainder = divmod(elapsed, 3600)
230 minutes, seconds = divmod(remainder, 60)
231 if gopts.time:
232 if hours == 0:
233 print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr)
234 else:
235 print('real\t%dh%dm%.3fs' % (hours, minutes, seconds),
236 file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400237
David Rileye0684ad2017-04-05 00:02:59 -0700238 cmd.event_log.FinishEvent(cmd_event, finish,
239 result is None or result == 0)
240 if gopts.event_log:
241 cmd.event_log.Write(os.path.abspath(
242 os.path.expanduser(gopts.event_log)))
243
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400244 return result
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700245
Conley Owens094cdbe2014-01-30 15:09:59 -0800246
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700247def _CheckWrapperVersion(ver, repo_path):
248 if not repo_path:
249 repo_path = '~/bin/repo'
250
251 if not ver:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700252 print('no --wrapper-version argument', file=sys.stderr)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900253 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700254
Conley Owens094cdbe2014-01-30 15:09:59 -0800255 exp = Wrapper().VERSION
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900256 ver = tuple(map(int, ver.split('.')))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700257 if len(ver) == 1:
258 ver = (0, ver[0])
259
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900260 exp_str = '.'.join(map(str, exp))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700261 if exp[0] > ver[0] or ver < (0, 4):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700262 print("""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700263!!! A new repo command (%5s) is available. !!!
264!!! You must upgrade before you can continue: !!!
265
266 cp %s %s
Conley Owens094cdbe2014-01-30 15:09:59 -0800267""" % (exp_str, WrapperPath(), repo_path), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700268 sys.exit(1)
269
270 if exp > ver:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700271 print("""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700272... A new repo command (%5s) is available.
273... You should upgrade soon:
274
275 cp %s %s
Conley Owens094cdbe2014-01-30 15:09:59 -0800276""" % (exp_str, WrapperPath(), repo_path), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700277
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200278def _CheckRepoDir(repo_dir):
279 if not repo_dir:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700280 print('no --repo-dir argument', file=sys.stderr)
David Pursehouse8a68ff92012-09-24 12:15:13 +0900281 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700282
283def _PruneOptions(argv, opt):
284 i = 0
285 while i < len(argv):
286 a = argv[i]
287 if a == '--':
288 break
289 if a.startswith('--'):
290 eq = a.find('=')
291 if eq > 0:
292 a = a[0:eq]
293 if not opt.has_option(a):
294 del argv[i]
295 continue
296 i += 1
297
Sarah Owens1f7627f2012-10-31 09:21:55 -0700298class _UserAgentHandler(urllib.request.BaseHandler):
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700299 def http_request(self, req):
Mike Frysinger71b0f312019-09-30 22:39:49 -0400300 req.add_header('User-Agent', user_agent.repo)
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700301 return req
302
303 def https_request(self, req):
Mike Frysinger71b0f312019-09-30 22:39:49 -0400304 req.add_header('User-Agent', user_agent.repo)
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700305 return req
306
JoonCheol Parke9860722012-10-11 02:31:44 +0900307def _AddPasswordFromUserInput(handler, msg, req):
David Pursehousec1b86a22012-11-14 11:36:51 +0900308 # If repo could not find auth info from netrc, try to get it from user input
309 url = req.get_full_url()
310 user, password = handler.passwd.find_user_password(None, url)
311 if user is None:
312 print(msg)
313 try:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530314 user = input('User: ')
David Pursehousec1b86a22012-11-14 11:36:51 +0900315 password = getpass.getpass()
316 except KeyboardInterrupt:
317 return
318 handler.passwd.add_password(None, url, user, password)
JoonCheol Parke9860722012-10-11 02:31:44 +0900319
Sarah Owens1f7627f2012-10-31 09:21:55 -0700320class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
JoonCheol Parke9860722012-10-11 02:31:44 +0900321 def http_error_401(self, req, fp, code, msg, headers):
322 _AddPasswordFromUserInput(self, msg, req)
Sarah Owens1f7627f2012-10-31 09:21:55 -0700323 return urllib.request.HTTPBasicAuthHandler.http_error_401(
JoonCheol Parke9860722012-10-11 02:31:44 +0900324 self, req, fp, code, msg, headers)
325
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700326 def http_error_auth_reqed(self, authreq, host, req, headers):
327 try:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700328 old_add_header = req.add_header
329 def _add_header(name, val):
330 val = val.replace('\n', '')
331 old_add_header(name, val)
332 req.add_header = _add_header
Sarah Owens1f7627f2012-10-31 09:21:55 -0700333 return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed(
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700334 self, authreq, host, req, headers)
335 except:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700336 reset = getattr(self, 'reset_retry_count', None)
337 if reset is not None:
338 reset()
Shawn O. Pearceb6605392011-10-11 15:58:07 -0700339 elif getattr(self, 'retried', None):
340 self.retried = 0
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700341 raise
342
Sarah Owens1f7627f2012-10-31 09:21:55 -0700343class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
JoonCheol Parke9860722012-10-11 02:31:44 +0900344 def http_error_401(self, req, fp, code, msg, headers):
345 _AddPasswordFromUserInput(self, msg, req)
Sarah Owens1f7627f2012-10-31 09:21:55 -0700346 return urllib.request.HTTPDigestAuthHandler.http_error_401(
JoonCheol Parke9860722012-10-11 02:31:44 +0900347 self, req, fp, code, msg, headers)
348
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800349 def http_error_auth_reqed(self, auth_header, host, req, headers):
350 try:
351 old_add_header = req.add_header
352 def _add_header(name, val):
353 val = val.replace('\n', '')
354 old_add_header(name, val)
355 req.add_header = _add_header
Sarah Owens1f7627f2012-10-31 09:21:55 -0700356 return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800357 self, auth_header, host, req, headers)
358 except:
359 reset = getattr(self, 'reset_retry_count', None)
360 if reset is not None:
361 reset()
362 elif getattr(self, 'retried', None):
363 self.retried = 0
364 raise
365
Carlos Aguado1242e602014-02-03 13:48:47 +0100366class _KerberosAuthHandler(urllib.request.BaseHandler):
367 def __init__(self):
368 self.retried = 0
369 self.context = None
370 self.handler_order = urllib.request.BaseHandler.handler_order - 50
371
David Pursehouse65b0ba52018-06-24 16:21:51 +0900372 def http_error_401(self, req, fp, code, msg, headers):
Carlos Aguado1242e602014-02-03 13:48:47 +0100373 host = req.get_host()
374 retry = self.http_error_auth_reqed('www-authenticate', host, req, headers)
375 return retry
376
377 def http_error_auth_reqed(self, auth_header, host, req, headers):
378 try:
379 spn = "HTTP@%s" % host
380 authdata = self._negotiate_get_authdata(auth_header, headers)
381
382 if self.retried > 3:
383 raise urllib.request.HTTPError(req.get_full_url(), 401,
384 "Negotiate auth failed", headers, None)
385 else:
386 self.retried += 1
387
388 neghdr = self._negotiate_get_svctk(spn, authdata)
389 if neghdr is None:
390 return None
391
392 req.add_unredirected_header('Authorization', neghdr)
393 response = self.parent.open(req)
394
395 srvauth = self._negotiate_get_authdata(auth_header, response.info())
396 if self._validate_response(srvauth):
397 return response
398 except kerberos.GSSError:
399 return None
400 except:
401 self.reset_retry_count()
402 raise
403 finally:
404 self._clean_context()
405
406 def reset_retry_count(self):
407 self.retried = 0
408
409 def _negotiate_get_authdata(self, auth_header, headers):
410 authhdr = headers.get(auth_header, None)
411 if authhdr is not None:
412 for mech_tuple in authhdr.split(","):
413 mech, __, authdata = mech_tuple.strip().partition(" ")
414 if mech.lower() == "negotiate":
415 return authdata.strip()
416 return None
417
418 def _negotiate_get_svctk(self, spn, authdata):
419 if authdata is None:
420 return None
421
422 result, self.context = kerberos.authGSSClientInit(spn)
423 if result < kerberos.AUTH_GSS_COMPLETE:
424 return None
425
426 result = kerberos.authGSSClientStep(self.context, authdata)
427 if result < kerberos.AUTH_GSS_CONTINUE:
428 return None
429
430 response = kerberos.authGSSClientResponse(self.context)
431 return "Negotiate %s" % response
432
433 def _validate_response(self, authdata):
434 if authdata is None:
435 return None
436 result = kerberos.authGSSClientStep(self.context, authdata)
437 if result == kerberos.AUTH_GSS_COMPLETE:
438 return True
439 return None
440
441 def _clean_context(self):
442 if self.context is not None:
443 kerberos.authGSSClientClean(self.context)
444 self.context = None
445
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700446def init_http():
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700447 handlers = [_UserAgentHandler()]
448
Sarah Owens1f7627f2012-10-31 09:21:55 -0700449 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700450 try:
451 n = netrc.netrc()
452 for host in n.hosts:
453 p = n.hosts[host]
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800454 mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
455 mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700456 except netrc.NetrcParseError:
457 pass
Shawn O. Pearce7b947de2011-09-23 11:50:31 -0700458 except IOError:
459 pass
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700460 handlers.append(_BasicAuthHandler(mgr))
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800461 handlers.append(_DigestAuthHandler(mgr))
Carlos Aguado1242e602014-02-03 13:48:47 +0100462 if kerberos:
463 handlers.append(_KerberosAuthHandler())
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700464
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700465 if 'http_proxy' in os.environ:
466 url = os.environ['http_proxy']
Sarah Owens1f7627f2012-10-31 09:21:55 -0700467 handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700468 if 'REPO_CURL_VERBOSE' in os.environ:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700469 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
470 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
471 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700472
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700473def _Main(argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400474 result = 0
475
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700476 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
477 opt.add_option("--repo-dir", dest="repodir",
478 help="path to .repo/")
479 opt.add_option("--wrapper-version", dest="wrapper_version",
480 help="version of the wrapper script")
481 opt.add_option("--wrapper-path", dest="wrapper_path",
482 help="location of the wrapper script")
483 _PruneOptions(argv, opt)
484 opt, argv = opt.parse_args(argv)
485
486 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
487 _CheckRepoDir(opt.repodir)
488
Shawn O. Pearceecff4f12011-11-29 15:01:33 -0800489 Version.wrapper_version = opt.wrapper_version
490 Version.wrapper_path = opt.wrapper_path
491
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700492 repo = _Repo(opt.repodir)
493 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700494 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800495 init_ssh()
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700496 init_http()
Mike Frysinger3fc15722019-08-27 00:36:46 -0400497 name, gopts, argv = repo._ParseArgs(argv)
498 run = lambda: repo._Run(name, gopts, argv) or 0
499 if gopts.trace_python:
500 import trace
501 tracer = trace.Trace(count=False, trace=True, timing=True,
502 ignoredirs=set(sys.path[1:]))
503 result = tracer.runfunc(run)
504 else:
505 result = run()
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700506 finally:
507 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700508 except KeyboardInterrupt:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700509 print('aborted by user', file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400510 result = 1
David Pursehouse0b8df7b2012-11-13 09:51:57 +0900511 except ManifestParseError as mpe:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700512 print('fatal: %s' % mpe, file=sys.stderr)
David Pursehouse0b8df7b2012-11-13 09:51:57 +0900513 result = 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700514 except RepoChangedException as rce:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800515 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700516 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800517 argv = list(sys.argv)
518 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700519 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800520 os.execv(__file__, argv)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700521 except OSError as e:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700522 print('fatal: cannot restart repo after upgrade', file=sys.stderr)
523 print('fatal: %s' % e, file=sys.stderr)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400524 result = 128
525
Renaud Paquaye8595e92016-11-01 15:51:59 -0700526 TerminatePager()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400527 sys.exit(result)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700528
529if __name__ == '__main__':
530 _Main(sys.argv[1:])