2020-09-06 17:33:45 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-06-13 06:30:51 +00:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-10-21 14:00:00 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2019-06-13 05:54:46 +00:00
|
|
|
"""The repo tool.
|
|
|
|
|
|
|
|
People shouldn't run this directly; instead, they should use the `repo` wrapper
|
|
|
|
which takes care of execing this entry point.
|
|
|
|
"""
|
|
|
|
|
2012-11-02 05:59:27 +00:00
|
|
|
from __future__ import print_function
|
2012-10-10 17:31:44 +00:00
|
|
|
import getpass
|
2011-09-19 17:04:23 +00:00
|
|
|
import netrc
|
2008-10-21 14:00:00 +00:00
|
|
|
import optparse
|
|
|
|
import os
|
2020-02-19 02:37:00 +00:00
|
|
|
import shlex
|
2008-10-21 14:00:00 +00:00
|
|
|
import sys
|
2019-12-02 21:49:44 +00:00
|
|
|
import textwrap
|
2011-09-23 00:06:41 +00:00
|
|
|
import time
|
2013-05-17 01:49:33 +00:00
|
|
|
|
|
|
|
from pyversion import is_python3
|
|
|
|
if is_python3():
|
2012-10-31 16:21:55 +00:00
|
|
|
import urllib.request
|
|
|
|
else:
|
2019-10-05 04:18:41 +00:00
|
|
|
import imp
|
2013-05-17 01:49:33 +00:00
|
|
|
import urllib2
|
2012-10-31 16:21:55 +00:00
|
|
|
urllib = imp.new_module('urllib')
|
|
|
|
urllib.request = urllib2
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2014-02-03 12:48:47 +00:00
|
|
|
try:
|
|
|
|
import kerberos
|
|
|
|
except ImportError:
|
|
|
|
kerberos = None
|
|
|
|
|
2014-12-22 20:17:59 +00:00
|
|
|
from color import SetDefaultColoring
|
2017-04-05 07:02:59 +00:00
|
|
|
import event_log
|
2019-08-27 04:26:15 +00:00
|
|
|
from repo_trace import SetTrace
|
2020-02-12 02:25:13 +00:00
|
|
|
from git_command import user_agent
|
2020-02-19 02:37:00 +00:00
|
|
|
from git_config import init_ssh, close_ssh, RepoConfig
|
2009-03-04 01:47:06 +00:00
|
|
|
from command import InteractiveCommand
|
|
|
|
from command import MirrorSafeCommand
|
2015-08-31 22:45:06 +00:00
|
|
|
from command import GitcAvailableCommand, GitcClientCommand
|
2011-11-29 23:01:33 +00:00
|
|
|
from subcmds.version import Version
|
2008-10-29 22:20:02 +00:00
|
|
|
from editor import Editor
|
2011-09-19 21:50:58 +00:00
|
|
|
from error import DownloadError
|
2015-06-19 22:39:25 +00:00
|
|
|
from error import InvalidProjectGroupsError
|
2009-03-02 20:56:08 +00:00
|
|
|
from error import ManifestInvalidRevisionError
|
2012-11-13 00:51:57 +00:00
|
|
|
from error import ManifestParseError
|
2012-11-16 01:33:11 +00:00
|
|
|
from error import NoManifestException
|
2008-10-21 14:00:00 +00:00
|
|
|
from error import NoSuchProjectError
|
|
|
|
from error import RepoChangedException
|
2015-08-20 19:19:28 +00:00
|
|
|
import gitc_utils
|
|
|
|
from manifest_xml import GitcManifest, XmlManifest
|
2016-11-01 22:51:59 +00:00
|
|
|
from pager import RunPager, TerminatePager
|
2014-01-30 23:09:59 +00:00
|
|
|
from wrapper import WrapperPath, Wrapper
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2012-10-11 07:44:48 +00:00
|
|
|
from subcmds import all_commands
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2013-05-17 01:49:33 +00:00
|
|
|
if not is_python3():
|
2020-02-15 03:45:53 +00:00
|
|
|
input = raw_input # noqa: F821
|
2013-03-01 13:44:38 +00:00
|
|
|
|
2020-02-16 20:15:53 +00:00
|
|
|
# NB: These do not need to be kept in sync with the repo launcher script.
|
|
|
|
# These may be much newer as it allows the repo launcher to roll between
|
|
|
|
# different repo releases while source versions might require a newer python.
|
|
|
|
#
|
|
|
|
# The soft version is when we start warning users that the version is old and
|
|
|
|
# we'll be dropping support for it. We'll refuse to work with versions older
|
|
|
|
# than the hard version.
|
|
|
|
#
|
|
|
|
# python-3.6 is in Ubuntu Bionic.
|
|
|
|
MIN_PYTHON_VERSION_SOFT = (3, 6)
|
|
|
|
MIN_PYTHON_VERSION_HARD = (3, 4)
|
|
|
|
|
|
|
|
if sys.version_info.major < 3:
|
2020-09-06 17:33:45 +00:00
|
|
|
print('repo: error: Python 2 is no longer supported; '
|
2020-02-16 20:15:53 +00:00
|
|
|
'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT),
|
|
|
|
file=sys.stderr)
|
2020-09-06 17:33:45 +00:00
|
|
|
sys.exit(1)
|
2020-02-16 20:15:53 +00:00
|
|
|
else:
|
|
|
|
if sys.version_info < MIN_PYTHON_VERSION_HARD:
|
|
|
|
print('repo: error: Python 3 version is too old; '
|
|
|
|
'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT),
|
|
|
|
file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
elif sys.version_info < MIN_PYTHON_VERSION_SOFT:
|
|
|
|
print('repo: warning: your Python 3 version is no longer supported; '
|
|
|
|
'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT),
|
|
|
|
file=sys.stderr)
|
|
|
|
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
global_options = optparse.OptionParser(
|
2019-12-02 21:49:44 +00:00
|
|
|
usage='repo [-p|--paginate|--no-pager] COMMAND [ARGS]',
|
|
|
|
add_help_option=False)
|
|
|
|
global_options.add_option('-h', '--help', action='store_true',
|
|
|
|
help='show this help message and exit')
|
2008-10-21 14:00:00 +00:00
|
|
|
global_options.add_option('-p', '--paginate',
|
|
|
|
dest='pager', action='store_true',
|
|
|
|
help='display command output in the pager')
|
|
|
|
global_options.add_option('--no-pager',
|
2020-02-17 19:36:08 +00:00
|
|
|
dest='pager', action='store_false',
|
2008-10-21 14:00:00 +00:00
|
|
|
help='disable the pager')
|
2014-12-22 20:17:59 +00:00
|
|
|
global_options.add_option('--color',
|
|
|
|
choices=('auto', 'always', 'never'), default=None,
|
|
|
|
help='control color usage: auto, always, never')
|
2009-03-10 01:26:31 +00:00
|
|
|
global_options.add_option('--trace',
|
|
|
|
dest='trace', action='store_true',
|
2019-08-27 04:26:15 +00:00
|
|
|
help='trace git command execution (REPO_TRACE=1)')
|
2019-08-27 04:36:46 +00:00
|
|
|
global_options.add_option('--trace-python',
|
|
|
|
dest='trace_python', action='store_true',
|
|
|
|
help='trace python command execution')
|
2011-09-23 00:06:41 +00:00
|
|
|
global_options.add_option('--time',
|
|
|
|
dest='time', action='store_true',
|
|
|
|
help='time repo command execution')
|
2009-03-03 02:24:23 +00:00
|
|
|
global_options.add_option('--version',
|
|
|
|
dest='show_version', action='store_true',
|
|
|
|
help='display this version of repo')
|
2017-04-05 07:02:59 +00:00
|
|
|
global_options.add_option('--event-log',
|
|
|
|
dest='event_log', action='store',
|
|
|
|
help='filename of event log to append timeline to')
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
class _Repo(object):
|
|
|
|
def __init__(self, repodir):
|
|
|
|
self.repodir = repodir
|
|
|
|
self.commands = all_commands
|
|
|
|
|
2019-08-27 04:36:46 +00:00
|
|
|
def _ParseArgs(self, argv):
|
|
|
|
"""Parse the main `repo` command line options."""
|
2008-10-21 14:00:00 +00:00
|
|
|
name = None
|
|
|
|
glob = []
|
|
|
|
|
2012-11-01 20:36:50 +00:00
|
|
|
for i in range(len(argv)):
|
2008-10-21 14:00:00 +00:00
|
|
|
if not argv[i].startswith('-'):
|
|
|
|
name = argv[i]
|
|
|
|
if i > 0:
|
|
|
|
glob = argv[:i]
|
|
|
|
argv = argv[i + 1:]
|
|
|
|
break
|
|
|
|
if not name:
|
|
|
|
glob = argv
|
|
|
|
name = 'help'
|
|
|
|
argv = []
|
2012-09-24 03:15:13 +00:00
|
|
|
gopts, _gargs = global_options.parse_args(glob)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2020-02-19 02:37:00 +00:00
|
|
|
name, alias_args = self._ExpandAlias(name)
|
|
|
|
argv = alias_args + argv
|
|
|
|
|
2019-12-02 21:49:44 +00:00
|
|
|
if gopts.help:
|
|
|
|
global_options.print_help()
|
|
|
|
commands = ' '.join(sorted(self.commands))
|
|
|
|
wrapped_commands = textwrap.wrap(commands, width=77)
|
|
|
|
print('\nAvailable commands:\n %s' % ('\n '.join(wrapped_commands),))
|
|
|
|
print('\nRun `repo help <command>` for command-specific details.')
|
|
|
|
global_options.exit()
|
|
|
|
|
2019-08-27 04:36:46 +00:00
|
|
|
return (name, gopts, argv)
|
|
|
|
|
2020-02-19 02:37:00 +00:00
|
|
|
def _ExpandAlias(self, name):
|
|
|
|
"""Look up user registered aliases."""
|
|
|
|
# We don't resolve aliases for existing subcommands. This matches git.
|
|
|
|
if name in self.commands:
|
|
|
|
return name, []
|
|
|
|
|
|
|
|
key = 'alias.%s' % (name,)
|
|
|
|
alias = RepoConfig.ForRepository(self.repodir).GetString(key)
|
|
|
|
if alias is None:
|
|
|
|
alias = RepoConfig.ForUser().GetString(key)
|
|
|
|
if alias is None:
|
|
|
|
return name, []
|
|
|
|
|
|
|
|
args = alias.strip().split(' ', 1)
|
|
|
|
name = args[0]
|
|
|
|
if len(args) == 2:
|
|
|
|
args = shlex.split(args[1])
|
|
|
|
else:
|
|
|
|
args = []
|
|
|
|
return name, args
|
|
|
|
|
2019-08-27 04:36:46 +00:00
|
|
|
def _Run(self, name, gopts, argv):
|
|
|
|
"""Execute the requested subcommand."""
|
|
|
|
result = 0
|
|
|
|
|
2009-03-10 01:26:31 +00:00
|
|
|
if gopts.trace:
|
2009-04-18 16:54:51 +00:00
|
|
|
SetTrace()
|
2009-03-03 02:24:23 +00:00
|
|
|
if gopts.show_version:
|
|
|
|
if name == 'help':
|
|
|
|
name = 'version'
|
|
|
|
else:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('fatal: invalid usage of --version', file=sys.stderr)
|
2011-04-29 13:59:12 +00:00
|
|
|
return 1
|
2009-03-03 02:24:23 +00:00
|
|
|
|
2014-12-22 20:17:59 +00:00
|
|
|
SetDefaultColoring(gopts.color)
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
try:
|
2020-02-25 20:18:31 +00:00
|
|
|
cmd = self.commands[name]()
|
2008-10-21 14:00:00 +00:00
|
|
|
except KeyError:
|
2012-11-02 05:59:27 +00:00
|
|
|
print("repo: '%s' is not a repo command. See 'repo help'." % name,
|
|
|
|
file=sys.stderr)
|
2011-04-29 13:59:12 +00:00
|
|
|
return 1
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
cmd.repodir = self.repodir
|
2009-05-18 20:19:57 +00:00
|
|
|
cmd.manifest = XmlManifest(cmd.repodir)
|
2015-08-20 19:19:28 +00:00
|
|
|
cmd.gitc_manifest = None
|
|
|
|
gitc_client_name = gitc_utils.parse_clientdir(os.getcwd())
|
|
|
|
if gitc_client_name:
|
|
|
|
cmd.gitc_manifest = GitcManifest(cmd.repodir, gitc_client_name)
|
|
|
|
cmd.manifest.isGitcClient = True
|
|
|
|
|
2008-10-29 22:20:02 +00:00
|
|
|
Editor.globalConfig = cmd.manifest.globalConfig
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-03-04 01:47:06 +00:00
|
|
|
if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
|
2012-11-02 05:59:27 +00:00
|
|
|
print("fatal: '%s' requires a working directory" % name,
|
|
|
|
file=sys.stderr)
|
2011-04-29 13:59:12 +00:00
|
|
|
return 1
|
2009-03-04 01:47:06 +00:00
|
|
|
|
2015-08-31 22:45:06 +00:00
|
|
|
if isinstance(cmd, GitcAvailableCommand) and not gitc_utils.get_gitc_manifest_dir():
|
2015-08-31 22:45:06 +00:00
|
|
|
print("fatal: '%s' requires GITC to be available" % name,
|
|
|
|
file=sys.stderr)
|
|
|
|
return 1
|
|
|
|
|
2015-08-31 22:45:06 +00:00
|
|
|
if isinstance(cmd, GitcClientCommand) and not gitc_client_name:
|
|
|
|
print("fatal: '%s' requires a GITC client" % name,
|
|
|
|
file=sys.stderr)
|
|
|
|
return 1
|
|
|
|
|
2014-03-09 17:20:02 +00:00
|
|
|
try:
|
|
|
|
copts, cargs = cmd.OptionParser.parse_args(argv)
|
|
|
|
copts = cmd.ReadEnvironmentOptions(copts)
|
|
|
|
except NoManifestException as e:
|
|
|
|
print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
|
2020-02-12 05:58:39 +00:00
|
|
|
file=sys.stderr)
|
2014-03-09 17:20:02 +00:00
|
|
|
print('error: manifest missing or unreadable -- please run init',
|
|
|
|
file=sys.stderr)
|
|
|
|
return 1
|
2009-04-18 20:49:13 +00:00
|
|
|
|
2020-02-19 06:17:56 +00:00
|
|
|
if gopts.pager is not False and not isinstance(cmd, InteractiveCommand):
|
2008-10-21 14:00:00 +00:00
|
|
|
config = cmd.manifest.globalConfig
|
|
|
|
if gopts.pager:
|
|
|
|
use_pager = True
|
|
|
|
else:
|
|
|
|
use_pager = config.GetBoolean('pager.%s' % name)
|
|
|
|
if use_pager is None:
|
2009-04-18 20:49:13 +00:00
|
|
|
use_pager = cmd.WantPager(copts)
|
2008-10-21 14:00:00 +00:00
|
|
|
if use_pager:
|
|
|
|
RunPager(config)
|
|
|
|
|
2012-11-14 22:18:06 +00:00
|
|
|
start = time.time()
|
2017-04-05 07:02:59 +00:00
|
|
|
cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start)
|
|
|
|
cmd.event_log.SetParent(cmd_event)
|
2008-10-21 14:00:00 +00:00
|
|
|
try:
|
2019-08-27 05:10:59 +00:00
|
|
|
cmd.ValidateOptions(copts, cargs)
|
2012-11-14 22:18:06 +00:00
|
|
|
result = cmd.Execute(copts, cargs)
|
2014-03-09 17:20:02 +00:00
|
|
|
except (DownloadError, ManifestInvalidRevisionError,
|
2020-02-12 05:58:39 +00:00
|
|
|
NoManifestException) as e:
|
2014-03-09 17:20:02 +00:00
|
|
|
print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
|
2020-02-12 05:58:39 +00:00
|
|
|
file=sys.stderr)
|
2014-03-09 17:20:02 +00:00
|
|
|
if isinstance(e, NoManifestException):
|
|
|
|
print('error: manifest missing or unreadable -- please run init',
|
|
|
|
file=sys.stderr)
|
2012-11-16 01:33:11 +00:00
|
|
|
result = 1
|
2012-09-09 22:37:57 +00:00
|
|
|
except NoSuchProjectError as e:
|
2008-10-21 14:00:00 +00:00
|
|
|
if e.name:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('error: project %s not found' % e.name, file=sys.stderr)
|
2008-10-21 14:00:00 +00:00
|
|
|
else:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('error: no project in current directory', file=sys.stderr)
|
2012-11-14 22:18:06 +00:00
|
|
|
result = 1
|
2015-06-19 22:39:25 +00:00
|
|
|
except InvalidProjectGroupsError as e:
|
|
|
|
if e.name:
|
|
|
|
print('error: project group must be enabled for project %s' % e.name, file=sys.stderr)
|
|
|
|
else:
|
2020-02-13 04:17:03 +00:00
|
|
|
print('error: project group must be enabled for the project in the current directory',
|
|
|
|
file=sys.stderr)
|
2015-06-19 22:39:25 +00:00
|
|
|
result = 1
|
2017-04-05 20:50:52 +00:00
|
|
|
except SystemExit as e:
|
|
|
|
if e.code:
|
|
|
|
result = e.code
|
|
|
|
raise
|
2012-11-14 22:18:06 +00:00
|
|
|
finally:
|
2017-04-05 07:02:59 +00:00
|
|
|
finish = time.time()
|
|
|
|
elapsed = finish - start
|
2012-11-14 22:18:06 +00:00
|
|
|
hours, remainder = divmod(elapsed, 3600)
|
|
|
|
minutes, seconds = divmod(remainder, 60)
|
|
|
|
if gopts.time:
|
|
|
|
if hours == 0:
|
|
|
|
print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr)
|
|
|
|
else:
|
|
|
|
print('real\t%dh%dm%.3fs' % (hours, minutes, seconds),
|
|
|
|
file=sys.stderr)
|
2011-04-29 13:59:12 +00:00
|
|
|
|
2017-04-05 07:02:59 +00:00
|
|
|
cmd.event_log.FinishEvent(cmd_event, finish,
|
|
|
|
result is None or result == 0)
|
|
|
|
if gopts.event_log:
|
|
|
|
cmd.event_log.Write(os.path.abspath(
|
|
|
|
os.path.expanduser(gopts.event_log)))
|
|
|
|
|
2011-04-29 13:59:12 +00:00
|
|
|
return result
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2014-01-30 23:09:59 +00:00
|
|
|
|
2020-02-10 22:34:49 +00:00
|
|
|
def _CheckWrapperVersion(ver_str, repo_path):
|
|
|
|
"""Verify the repo launcher is new enough for this checkout.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ver_str: The version string passed from the repo launcher when it ran us.
|
|
|
|
repo_path: The path to the repo launcher that loaded us.
|
|
|
|
"""
|
|
|
|
# Refuse to work with really old wrapper versions. We don't test these,
|
|
|
|
# so might as well require a somewhat recent sane version.
|
|
|
|
# v1.15 of the repo launcher was released in ~Mar 2012.
|
|
|
|
MIN_REPO_VERSION = (1, 15)
|
|
|
|
min_str = '.'.join(str(x) for x in MIN_REPO_VERSION)
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
if not repo_path:
|
|
|
|
repo_path = '~/bin/repo'
|
|
|
|
|
2020-02-10 22:34:49 +00:00
|
|
|
if not ver_str:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('no --wrapper-version argument', file=sys.stderr)
|
2012-09-24 03:15:13 +00:00
|
|
|
sys.exit(1)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2020-02-10 22:34:49 +00:00
|
|
|
# Pull out the version of the repo launcher we know about to compare.
|
2014-01-30 23:09:59 +00:00
|
|
|
exp = Wrapper().VERSION
|
2020-02-10 22:34:49 +00:00
|
|
|
ver = tuple(map(int, ver_str.split('.')))
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2012-10-25 03:40:51 +00:00
|
|
|
exp_str = '.'.join(map(str, exp))
|
2020-02-10 22:34:49 +00:00
|
|
|
if ver < MIN_REPO_VERSION:
|
2012-11-02 05:59:27 +00:00
|
|
|
print("""
|
2020-02-10 22:34:49 +00:00
|
|
|
repo: error:
|
|
|
|
!!! Your version of repo %s is too old.
|
|
|
|
!!! We need at least version %s.
|
2020-02-13 00:54:49 +00:00
|
|
|
!!! A new version of repo (%s) is available.
|
2020-02-10 22:34:49 +00:00
|
|
|
!!! You must upgrade before you can continue:
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
cp %s %s
|
2020-02-10 22:34:49 +00:00
|
|
|
""" % (ver_str, min_str, exp_str, WrapperPath(), repo_path), file=sys.stderr)
|
2008-10-21 14:00:00 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if exp > ver:
|
2020-02-26 21:21:08 +00:00
|
|
|
print('\n... A new version of repo (%s) is available.' % (exp_str,),
|
|
|
|
file=sys.stderr)
|
|
|
|
if os.access(repo_path, os.W_OK):
|
|
|
|
print("""\
|
2008-10-21 14:00:00 +00:00
|
|
|
... You should upgrade soon:
|
|
|
|
cp %s %s
|
2020-02-26 21:21:08 +00:00
|
|
|
""" % (WrapperPath(), repo_path), file=sys.stderr)
|
|
|
|
else:
|
|
|
|
print("""\
|
|
|
|
... New version is available at: %s
|
|
|
|
... The launcher is run from: %s
|
|
|
|
!!! The launcher is not writable. Please talk to your sysadmin or distro
|
|
|
|
!!! to get an update installed.
|
|
|
|
""" % (WrapperPath(), repo_path), file=sys.stderr)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2012-09-29 22:37:55 +00:00
|
|
|
def _CheckRepoDir(repo_dir):
|
|
|
|
if not repo_dir:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('no --repo-dir argument', file=sys.stderr)
|
2012-09-24 03:15:13 +00:00
|
|
|
sys.exit(1)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
def _PruneOptions(argv, opt):
|
|
|
|
i = 0
|
|
|
|
while i < len(argv):
|
|
|
|
a = argv[i]
|
|
|
|
if a == '--':
|
|
|
|
break
|
|
|
|
if a.startswith('--'):
|
|
|
|
eq = a.find('=')
|
|
|
|
if eq > 0:
|
|
|
|
a = a[0:eq]
|
|
|
|
if not opt.has_option(a):
|
|
|
|
del argv[i]
|
|
|
|
continue
|
|
|
|
i += 1
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2012-10-31 16:21:55 +00:00
|
|
|
class _UserAgentHandler(urllib.request.BaseHandler):
|
2011-09-19 15:05:31 +00:00
|
|
|
def http_request(self, req):
|
2019-10-01 02:39:49 +00:00
|
|
|
req.add_header('User-Agent', user_agent.repo)
|
2011-09-19 15:05:31 +00:00
|
|
|
return req
|
|
|
|
|
|
|
|
def https_request(self, req):
|
2019-10-01 02:39:49 +00:00
|
|
|
req.add_header('User-Agent', user_agent.repo)
|
2011-09-19 15:05:31 +00:00
|
|
|
return req
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2012-10-10 17:31:44 +00:00
|
|
|
def _AddPasswordFromUserInput(handler, msg, req):
|
2012-11-14 02:36:51 +00:00
|
|
|
# If repo could not find auth info from netrc, try to get it from user input
|
|
|
|
url = req.get_full_url()
|
|
|
|
user, password = handler.passwd.find_user_password(None, url)
|
|
|
|
if user is None:
|
|
|
|
print(msg)
|
|
|
|
try:
|
2013-03-01 13:44:38 +00:00
|
|
|
user = input('User: ')
|
2012-11-14 02:36:51 +00:00
|
|
|
password = getpass.getpass()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return
|
|
|
|
handler.passwd.add_password(None, url, user, password)
|
2012-10-10 17:31:44 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2012-10-31 16:21:55 +00:00
|
|
|
class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
|
2012-10-10 17:31:44 +00:00
|
|
|
def http_error_401(self, req, fp, code, msg, headers):
|
|
|
|
_AddPasswordFromUserInput(self, msg, req)
|
2012-10-31 16:21:55 +00:00
|
|
|
return urllib.request.HTTPBasicAuthHandler.http_error_401(
|
2020-02-12 05:58:39 +00:00
|
|
|
self, req, fp, code, msg, headers)
|
2012-10-10 17:31:44 +00:00
|
|
|
|
2011-10-11 19:00:38 +00:00
|
|
|
def http_error_auth_reqed(self, authreq, host, req, headers):
|
|
|
|
try:
|
2011-10-11 21:05:21 +00:00
|
|
|
old_add_header = req.add_header
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2011-10-11 21:05:21 +00:00
|
|
|
def _add_header(name, val):
|
|
|
|
val = val.replace('\n', '')
|
|
|
|
old_add_header(name, val)
|
|
|
|
req.add_header = _add_header
|
2012-10-31 16:21:55 +00:00
|
|
|
return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed(
|
2020-02-12 05:58:39 +00:00
|
|
|
self, authreq, host, req, headers)
|
2020-02-12 06:40:47 +00:00
|
|
|
except Exception:
|
2011-10-11 21:05:21 +00:00
|
|
|
reset = getattr(self, 'reset_retry_count', None)
|
|
|
|
if reset is not None:
|
|
|
|
reset()
|
2011-10-11 22:58:07 +00:00
|
|
|
elif getattr(self, 'retried', None):
|
|
|
|
self.retried = 0
|
2011-10-11 19:00:38 +00:00
|
|
|
raise
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2012-10-31 16:21:55 +00:00
|
|
|
class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
|
2012-10-10 17:31:44 +00:00
|
|
|
def http_error_401(self, req, fp, code, msg, headers):
|
|
|
|
_AddPasswordFromUserInput(self, msg, req)
|
2012-10-31 16:21:55 +00:00
|
|
|
return urllib.request.HTTPDigestAuthHandler.http_error_401(
|
2020-02-12 05:58:39 +00:00
|
|
|
self, req, fp, code, msg, headers)
|
2012-10-10 17:31:44 +00:00
|
|
|
|
2012-01-31 03:10:09 +00:00
|
|
|
def http_error_auth_reqed(self, auth_header, host, req, headers):
|
|
|
|
try:
|
|
|
|
old_add_header = req.add_header
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2012-01-31 03:10:09 +00:00
|
|
|
def _add_header(name, val):
|
|
|
|
val = val.replace('\n', '')
|
|
|
|
old_add_header(name, val)
|
|
|
|
req.add_header = _add_header
|
2012-10-31 16:21:55 +00:00
|
|
|
return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
|
2020-02-12 05:58:39 +00:00
|
|
|
self, auth_header, host, req, headers)
|
2020-02-12 06:40:47 +00:00
|
|
|
except Exception:
|
2012-01-31 03:10:09 +00:00
|
|
|
reset = getattr(self, 'reset_retry_count', None)
|
|
|
|
if reset is not None:
|
|
|
|
reset()
|
|
|
|
elif getattr(self, 'retried', None):
|
|
|
|
self.retried = 0
|
|
|
|
raise
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2014-02-03 12:48:47 +00:00
|
|
|
class _KerberosAuthHandler(urllib.request.BaseHandler):
|
|
|
|
def __init__(self):
|
|
|
|
self.retried = 0
|
|
|
|
self.context = None
|
|
|
|
self.handler_order = urllib.request.BaseHandler.handler_order - 50
|
|
|
|
|
2018-06-24 07:21:51 +00:00
|
|
|
def http_error_401(self, req, fp, code, msg, headers):
|
2014-02-03 12:48:47 +00:00
|
|
|
host = req.get_host()
|
|
|
|
retry = self.http_error_auth_reqed('www-authenticate', host, req, headers)
|
|
|
|
return retry
|
|
|
|
|
|
|
|
def http_error_auth_reqed(self, auth_header, host, req, headers):
|
|
|
|
try:
|
|
|
|
spn = "HTTP@%s" % host
|
|
|
|
authdata = self._negotiate_get_authdata(auth_header, headers)
|
|
|
|
|
|
|
|
if self.retried > 3:
|
|
|
|
raise urllib.request.HTTPError(req.get_full_url(), 401,
|
2020-02-12 05:58:39 +00:00
|
|
|
"Negotiate auth failed", headers, None)
|
2014-02-03 12:48:47 +00:00
|
|
|
else:
|
|
|
|
self.retried += 1
|
|
|
|
|
|
|
|
neghdr = self._negotiate_get_svctk(spn, authdata)
|
|
|
|
if neghdr is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
req.add_unredirected_header('Authorization', neghdr)
|
|
|
|
response = self.parent.open(req)
|
|
|
|
|
|
|
|
srvauth = self._negotiate_get_authdata(auth_header, response.info())
|
|
|
|
if self._validate_response(srvauth):
|
|
|
|
return response
|
|
|
|
except kerberos.GSSError:
|
|
|
|
return None
|
2020-02-12 06:40:47 +00:00
|
|
|
except Exception:
|
2014-02-03 12:48:47 +00:00
|
|
|
self.reset_retry_count()
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
self._clean_context()
|
|
|
|
|
|
|
|
def reset_retry_count(self):
|
|
|
|
self.retried = 0
|
|
|
|
|
|
|
|
def _negotiate_get_authdata(self, auth_header, headers):
|
|
|
|
authhdr = headers.get(auth_header, None)
|
|
|
|
if authhdr is not None:
|
|
|
|
for mech_tuple in authhdr.split(","):
|
|
|
|
mech, __, authdata = mech_tuple.strip().partition(" ")
|
|
|
|
if mech.lower() == "negotiate":
|
|
|
|
return authdata.strip()
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _negotiate_get_svctk(self, spn, authdata):
|
|
|
|
if authdata is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
result, self.context = kerberos.authGSSClientInit(spn)
|
|
|
|
if result < kerberos.AUTH_GSS_COMPLETE:
|
|
|
|
return None
|
|
|
|
|
|
|
|
result = kerberos.authGSSClientStep(self.context, authdata)
|
|
|
|
if result < kerberos.AUTH_GSS_CONTINUE:
|
|
|
|
return None
|
|
|
|
|
|
|
|
response = kerberos.authGSSClientResponse(self.context)
|
|
|
|
return "Negotiate %s" % response
|
|
|
|
|
|
|
|
def _validate_response(self, authdata):
|
|
|
|
if authdata is None:
|
|
|
|
return None
|
|
|
|
result = kerberos.authGSSClientStep(self.context, authdata)
|
|
|
|
if result == kerberos.AUTH_GSS_COMPLETE:
|
|
|
|
return True
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _clean_context(self):
|
|
|
|
if self.context is not None:
|
|
|
|
kerberos.authGSSClientClean(self.context)
|
|
|
|
self.context = None
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2011-09-11 19:57:15 +00:00
|
|
|
def init_http():
|
2011-09-19 15:05:31 +00:00
|
|
|
handlers = [_UserAgentHandler()]
|
|
|
|
|
2012-10-31 16:21:55 +00:00
|
|
|
mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
|
2011-09-19 17:04:23 +00:00
|
|
|
try:
|
|
|
|
n = netrc.netrc()
|
|
|
|
for host in n.hosts:
|
|
|
|
p = n.hosts[host]
|
2020-02-12 05:31:05 +00:00
|
|
|
mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
|
2012-01-31 03:10:09 +00:00
|
|
|
mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
|
2011-09-19 17:04:23 +00:00
|
|
|
except netrc.NetrcParseError:
|
|
|
|
pass
|
2011-09-23 18:50:31 +00:00
|
|
|
except IOError:
|
|
|
|
pass
|
2011-10-11 19:00:38 +00:00
|
|
|
handlers.append(_BasicAuthHandler(mgr))
|
2012-01-31 03:10:09 +00:00
|
|
|
handlers.append(_DigestAuthHandler(mgr))
|
2014-02-03 12:48:47 +00:00
|
|
|
if kerberos:
|
|
|
|
handlers.append(_KerberosAuthHandler())
|
2011-09-19 17:04:23 +00:00
|
|
|
|
2011-09-11 19:57:15 +00:00
|
|
|
if 'http_proxy' in os.environ:
|
|
|
|
url = os.environ['http_proxy']
|
2012-10-31 16:21:55 +00:00
|
|
|
handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
|
2011-09-19 15:05:31 +00:00
|
|
|
if 'REPO_CURL_VERBOSE' in os.environ:
|
2012-10-31 16:21:55 +00:00
|
|
|
handlers.append(urllib.request.HTTPHandler(debuglevel=1))
|
|
|
|
handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
|
|
|
|
urllib.request.install_opener(urllib.request.build_opener(*handlers))
|
2011-09-11 19:57:15 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
def _Main(argv):
|
2011-04-29 13:59:12 +00:00
|
|
|
result = 0
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
|
|
|
|
opt.add_option("--repo-dir", dest="repodir",
|
|
|
|
help="path to .repo/")
|
|
|
|
opt.add_option("--wrapper-version", dest="wrapper_version",
|
|
|
|
help="version of the wrapper script")
|
|
|
|
opt.add_option("--wrapper-path", dest="wrapper_path",
|
|
|
|
help="location of the wrapper script")
|
|
|
|
_PruneOptions(argv, opt)
|
|
|
|
opt, argv = opt.parse_args(argv)
|
|
|
|
|
|
|
|
_CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
|
|
|
|
_CheckRepoDir(opt.repodir)
|
|
|
|
|
2011-11-29 23:01:33 +00:00
|
|
|
Version.wrapper_version = opt.wrapper_version
|
|
|
|
Version.wrapper_path = opt.wrapper_path
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
repo = _Repo(opt.repodir)
|
|
|
|
try:
|
2009-04-11 01:53:46 +00:00
|
|
|
try:
|
2010-12-21 21:39:23 +00:00
|
|
|
init_ssh()
|
2011-09-11 19:57:15 +00:00
|
|
|
init_http()
|
2019-08-27 04:36:46 +00:00
|
|
|
name, gopts, argv = repo._ParseArgs(argv)
|
|
|
|
run = lambda: repo._Run(name, gopts, argv) or 0
|
|
|
|
if gopts.trace_python:
|
|
|
|
import trace
|
|
|
|
tracer = trace.Trace(count=False, trace=True, timing=True,
|
|
|
|
ignoredirs=set(sys.path[1:]))
|
|
|
|
result = tracer.runfunc(run)
|
|
|
|
else:
|
|
|
|
result = run()
|
2009-04-11 01:53:46 +00:00
|
|
|
finally:
|
|
|
|
close_ssh()
|
2008-10-21 14:00:00 +00:00
|
|
|
except KeyboardInterrupt:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('aborted by user', file=sys.stderr)
|
2011-04-29 13:59:12 +00:00
|
|
|
result = 1
|
2012-11-13 00:51:57 +00:00
|
|
|
except ManifestParseError as mpe:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('fatal: %s' % mpe, file=sys.stderr)
|
2012-11-13 00:51:57 +00:00
|
|
|
result = 1
|
2012-09-09 22:37:57 +00:00
|
|
|
except RepoChangedException as rce:
|
2008-11-03 18:32:09 +00:00
|
|
|
# If repo changed, re-exec ourselves.
|
2008-10-21 14:00:00 +00:00
|
|
|
#
|
2008-11-03 18:32:09 +00:00
|
|
|
argv = list(sys.argv)
|
|
|
|
argv.extend(rce.extra_args)
|
2008-10-21 14:00:00 +00:00
|
|
|
try:
|
2020-04-16 16:38:04 +00:00
|
|
|
os.execv(sys.executable, [__file__] + argv)
|
2012-09-09 22:37:57 +00:00
|
|
|
except OSError as e:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('fatal: cannot restart repo after upgrade', file=sys.stderr)
|
|
|
|
print('fatal: %s' % e, file=sys.stderr)
|
2011-04-29 13:59:12 +00:00
|
|
|
result = 128
|
|
|
|
|
2016-11-01 22:51:59 +00:00
|
|
|
TerminatePager()
|
2011-04-29 13:59:12 +00:00
|
|
|
sys.exit(result)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
_Main(sys.argv[1:])
|