Mike Frysinger | a488af5 | 2020-09-06 13:33:45 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 2 | # -*- coding:utf-8 -*- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3 | # |
| 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 Frysinger | 87fb5a1 | 2019-06-13 01:54:46 -0400 | [diff] [blame] | 18 | """The repo tool. |
| 19 | |
| 20 | People shouldn't run this directly; instead, they should use the `repo` wrapper |
| 21 | which takes care of execing this entry point. |
| 22 | """ |
| 23 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 24 | from __future__ import print_function |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 25 | import getpass |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 26 | import netrc |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | import optparse |
| 28 | import os |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 29 | import shlex |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | import sys |
Mike Frysinger | 7c321f1 | 2019-12-02 16:49:44 -0500 | [diff] [blame] | 31 | import textwrap |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 32 | import time |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 33 | |
| 34 | from pyversion import is_python3 |
| 35 | if is_python3(): |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 36 | import urllib.request |
| 37 | else: |
Rashed Abdel-Tawab | 2058c63 | 2019-10-05 00:18:41 -0400 | [diff] [blame] | 38 | import imp |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 39 | import urllib2 |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 40 | urllib = imp.new_module('urllib') |
| 41 | urllib.request = urllib2 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 43 | try: |
| 44 | import kerberos |
| 45 | except ImportError: |
| 46 | kerberos = None |
| 47 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 48 | from color import SetDefaultColoring |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 49 | import event_log |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 50 | from repo_trace import SetTrace |
David Pursehouse | 9090e80 | 2020-02-12 11:25:13 +0900 | [diff] [blame] | 51 | from git_command import user_agent |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 52 | from git_config import init_ssh, close_ssh, RepoConfig |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame^] | 53 | from git_trace2_event_log import EventLog |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 54 | from command import InteractiveCommand |
| 55 | from command import MirrorSafeCommand |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 56 | from command import GitcAvailableCommand, GitcClientCommand |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 57 | from subcmds.version import Version |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 58 | from editor import Editor |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 59 | from error import DownloadError |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 60 | from error import InvalidProjectGroupsError |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 61 | from error import ManifestInvalidRevisionError |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 62 | from error import ManifestParseError |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 63 | from error import NoManifestException |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | from error import NoSuchProjectError |
| 65 | from error import RepoChangedException |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 66 | import gitc_utils |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 67 | from manifest_xml import GitcClient, RepoClient |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 68 | from pager import RunPager, TerminatePager |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 69 | from wrapper import WrapperPath, Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 71 | from subcmds import all_commands |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 73 | if not is_python3(): |
David Pursehouse | a46bf7d | 2020-02-15 12:45:53 +0900 | [diff] [blame] | 74 | input = raw_input # noqa: F821 |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 75 | |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 76 | # NB: These do not need to be kept in sync with the repo launcher script. |
| 77 | # These may be much newer as it allows the repo launcher to roll between |
| 78 | # different repo releases while source versions might require a newer python. |
| 79 | # |
| 80 | # The soft version is when we start warning users that the version is old and |
| 81 | # we'll be dropping support for it. We'll refuse to work with versions older |
| 82 | # than the hard version. |
| 83 | # |
| 84 | # python-3.6 is in Ubuntu Bionic. |
| 85 | MIN_PYTHON_VERSION_SOFT = (3, 6) |
| 86 | MIN_PYTHON_VERSION_HARD = (3, 4) |
| 87 | |
| 88 | if sys.version_info.major < 3: |
Mike Frysinger | a488af5 | 2020-09-06 13:33:45 -0400 | [diff] [blame] | 89 | print('repo: error: Python 2 is no longer supported; ' |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 90 | 'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT), |
| 91 | file=sys.stderr) |
Mike Frysinger | a488af5 | 2020-09-06 13:33:45 -0400 | [diff] [blame] | 92 | sys.exit(1) |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 93 | else: |
| 94 | if sys.version_info < MIN_PYTHON_VERSION_HARD: |
| 95 | print('repo: error: Python 3 version is too old; ' |
| 96 | 'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT), |
| 97 | file=sys.stderr) |
| 98 | sys.exit(1) |
| 99 | elif sys.version_info < MIN_PYTHON_VERSION_SOFT: |
| 100 | print('repo: warning: your Python 3 version is no longer supported; ' |
| 101 | 'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT), |
| 102 | file=sys.stderr) |
| 103 | |
| 104 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | global_options = optparse.OptionParser( |
Mike Frysinger | 7c321f1 | 2019-12-02 16:49:44 -0500 | [diff] [blame] | 106 | usage='repo [-p|--paginate|--no-pager] COMMAND [ARGS]', |
| 107 | add_help_option=False) |
| 108 | global_options.add_option('-h', '--help', action='store_true', |
| 109 | help='show this help message and exit') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | global_options.add_option('-p', '--paginate', |
| 111 | dest='pager', action='store_true', |
| 112 | help='display command output in the pager') |
| 113 | global_options.add_option('--no-pager', |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 114 | dest='pager', action='store_false', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | help='disable the pager') |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 116 | global_options.add_option('--color', |
| 117 | choices=('auto', 'always', 'never'), default=None, |
| 118 | help='control color usage: auto, always, never') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 119 | global_options.add_option('--trace', |
| 120 | dest='trace', action='store_true', |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 121 | help='trace git command execution (REPO_TRACE=1)') |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 122 | global_options.add_option('--trace-python', |
| 123 | dest='trace_python', action='store_true', |
| 124 | help='trace python command execution') |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 125 | global_options.add_option('--time', |
| 126 | dest='time', action='store_true', |
| 127 | help='time repo command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 128 | global_options.add_option('--version', |
| 129 | dest='show_version', action='store_true', |
| 130 | help='display this version of repo') |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 131 | global_options.add_option('--event-log', |
| 132 | dest='event_log', action='store', |
| 133 | help='filename of event log to append timeline to') |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame^] | 134 | global_options.add_option('--git-trace2-event-log', action='store', |
| 135 | help='directory to write git trace2 event log to') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 136 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 137 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | class _Repo(object): |
| 139 | def __init__(self, repodir): |
| 140 | self.repodir = repodir |
| 141 | self.commands = all_commands |
| 142 | |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 143 | def _ParseArgs(self, argv): |
| 144 | """Parse the main `repo` command line options.""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 145 | name = None |
| 146 | glob = [] |
| 147 | |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 148 | for i in range(len(argv)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 149 | if not argv[i].startswith('-'): |
| 150 | name = argv[i] |
| 151 | if i > 0: |
| 152 | glob = argv[:i] |
| 153 | argv = argv[i + 1:] |
| 154 | break |
| 155 | if not name: |
| 156 | glob = argv |
| 157 | name = 'help' |
| 158 | argv = [] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 159 | gopts, _gargs = global_options.parse_args(glob) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 160 | |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 161 | name, alias_args = self._ExpandAlias(name) |
| 162 | argv = alias_args + argv |
| 163 | |
Mike Frysinger | 7c321f1 | 2019-12-02 16:49:44 -0500 | [diff] [blame] | 164 | if gopts.help: |
| 165 | global_options.print_help() |
| 166 | commands = ' '.join(sorted(self.commands)) |
| 167 | wrapped_commands = textwrap.wrap(commands, width=77) |
| 168 | print('\nAvailable commands:\n %s' % ('\n '.join(wrapped_commands),)) |
| 169 | print('\nRun `repo help <command>` for command-specific details.') |
| 170 | global_options.exit() |
| 171 | |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 172 | return (name, gopts, argv) |
| 173 | |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 174 | def _ExpandAlias(self, name): |
| 175 | """Look up user registered aliases.""" |
| 176 | # We don't resolve aliases for existing subcommands. This matches git. |
| 177 | if name in self.commands: |
| 178 | return name, [] |
| 179 | |
| 180 | key = 'alias.%s' % (name,) |
| 181 | alias = RepoConfig.ForRepository(self.repodir).GetString(key) |
| 182 | if alias is None: |
| 183 | alias = RepoConfig.ForUser().GetString(key) |
| 184 | if alias is None: |
| 185 | return name, [] |
| 186 | |
| 187 | args = alias.strip().split(' ', 1) |
| 188 | name = args[0] |
| 189 | if len(args) == 2: |
| 190 | args = shlex.split(args[1]) |
| 191 | else: |
| 192 | args = [] |
| 193 | return name, args |
| 194 | |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 195 | def _Run(self, name, gopts, argv): |
| 196 | """Execute the requested subcommand.""" |
| 197 | result = 0 |
| 198 | |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 199 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 200 | SetTrace() |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 201 | if gopts.show_version: |
| 202 | if name == 'help': |
| 203 | name = 'version' |
| 204 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 205 | print('fatal: invalid usage of --version', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 206 | return 1 |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 207 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 208 | SetDefaultColoring(gopts.color) |
| 209 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 210 | try: |
Mike Frysinger | bb93046 | 2020-02-25 15:18:31 -0500 | [diff] [blame] | 211 | cmd = self.commands[name]() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 212 | except KeyError: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 213 | print("repo: '%s' is not a repo command. See 'repo help'." % name, |
| 214 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 215 | return 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 216 | |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame^] | 217 | git_trace2_event_log = EventLog() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | cmd.repodir = self.repodir |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 219 | cmd.client = RepoClient(cmd.repodir) |
| 220 | cmd.manifest = cmd.client.manifest |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 221 | cmd.gitc_manifest = None |
| 222 | gitc_client_name = gitc_utils.parse_clientdir(os.getcwd()) |
| 223 | if gitc_client_name: |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 224 | cmd.gitc_manifest = GitcClient(cmd.repodir, gitc_client_name) |
| 225 | cmd.client.isGitcClient = True |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 226 | |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 227 | Editor.globalConfig = cmd.client.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 228 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 229 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 230 | print("fatal: '%s' requires a working directory" % name, |
| 231 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 232 | return 1 |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 233 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 234 | if isinstance(cmd, GitcAvailableCommand) and not gitc_utils.get_gitc_manifest_dir(): |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 235 | print("fatal: '%s' requires GITC to be available" % name, |
| 236 | file=sys.stderr) |
| 237 | return 1 |
| 238 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 239 | if isinstance(cmd, GitcClientCommand) and not gitc_client_name: |
| 240 | print("fatal: '%s' requires a GITC client" % name, |
| 241 | file=sys.stderr) |
| 242 | return 1 |
| 243 | |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 244 | try: |
| 245 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 246 | copts = cmd.ReadEnvironmentOptions(copts) |
| 247 | except NoManifestException as e: |
| 248 | print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)), |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 249 | file=sys.stderr) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 250 | print('error: manifest missing or unreadable -- please run init', |
| 251 | file=sys.stderr) |
| 252 | return 1 |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 253 | |
Mike Frysinger | 8a98efe | 2020-02-19 01:17:56 -0500 | [diff] [blame] | 254 | if gopts.pager is not False and not isinstance(cmd, InteractiveCommand): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 255 | config = cmd.client.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 256 | if gopts.pager: |
| 257 | use_pager = True |
| 258 | else: |
| 259 | use_pager = config.GetBoolean('pager.%s' % name) |
| 260 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 261 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 262 | if use_pager: |
| 263 | RunPager(config) |
| 264 | |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 265 | start = time.time() |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 266 | cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start) |
| 267 | cmd.event_log.SetParent(cmd_event) |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame^] | 268 | git_trace2_event_log.StartEvent() |
| 269 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 270 | try: |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 271 | cmd.ValidateOptions(copts, cargs) |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 272 | result = cmd.Execute(copts, cargs) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 273 | except (DownloadError, ManifestInvalidRevisionError, |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 274 | NoManifestException) as e: |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 275 | print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)), |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 276 | file=sys.stderr) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 277 | if isinstance(e, NoManifestException): |
| 278 | print('error: manifest missing or unreadable -- please run init', |
| 279 | file=sys.stderr) |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 280 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 281 | except NoSuchProjectError as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 282 | if e.name: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 283 | print('error: project %s not found' % e.name, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 284 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 285 | print('error: no project in current directory', file=sys.stderr) |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 286 | result = 1 |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 287 | except InvalidProjectGroupsError as e: |
| 288 | if e.name: |
| 289 | print('error: project group must be enabled for project %s' % e.name, file=sys.stderr) |
| 290 | else: |
David Pursehouse | 3cda50a | 2020-02-13 13:17:03 +0900 | [diff] [blame] | 291 | print('error: project group must be enabled for the project in the current directory', |
| 292 | file=sys.stderr) |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 293 | result = 1 |
David Riley | aa90021 | 2017-04-05 13:50:52 -0700 | [diff] [blame] | 294 | except SystemExit as e: |
| 295 | if e.code: |
| 296 | result = e.code |
| 297 | raise |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 298 | finally: |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 299 | finish = time.time() |
| 300 | elapsed = finish - start |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 301 | hours, remainder = divmod(elapsed, 3600) |
| 302 | minutes, seconds = divmod(remainder, 60) |
| 303 | if gopts.time: |
| 304 | if hours == 0: |
| 305 | print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr) |
| 306 | else: |
| 307 | print('real\t%dh%dm%.3fs' % (hours, minutes, seconds), |
| 308 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 309 | |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 310 | cmd.event_log.FinishEvent(cmd_event, finish, |
| 311 | result is None or result == 0) |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame^] | 312 | git_trace2_event_log.ExitEvent(result) |
| 313 | |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 314 | if gopts.event_log: |
| 315 | cmd.event_log.Write(os.path.abspath( |
| 316 | os.path.expanduser(gopts.event_log))) |
| 317 | |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame^] | 318 | git_trace2_event_log.Write(gopts.git_trace2_event_log) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 319 | return result |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 320 | |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 321 | |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 322 | def _CheckWrapperVersion(ver_str, repo_path): |
| 323 | """Verify the repo launcher is new enough for this checkout. |
| 324 | |
| 325 | Args: |
| 326 | ver_str: The version string passed from the repo launcher when it ran us. |
| 327 | repo_path: The path to the repo launcher that loaded us. |
| 328 | """ |
| 329 | # Refuse to work with really old wrapper versions. We don't test these, |
| 330 | # so might as well require a somewhat recent sane version. |
| 331 | # v1.15 of the repo launcher was released in ~Mar 2012. |
| 332 | MIN_REPO_VERSION = (1, 15) |
| 333 | min_str = '.'.join(str(x) for x in MIN_REPO_VERSION) |
| 334 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 335 | if not repo_path: |
| 336 | repo_path = '~/bin/repo' |
| 337 | |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 338 | if not ver_str: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 339 | print('no --wrapper-version argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 340 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 341 | |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 342 | # Pull out the version of the repo launcher we know about to compare. |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 343 | exp = Wrapper().VERSION |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 344 | ver = tuple(map(int, ver_str.split('.'))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 345 | |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 346 | exp_str = '.'.join(map(str, exp)) |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 347 | if ver < MIN_REPO_VERSION: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 348 | print(""" |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 349 | repo: error: |
| 350 | !!! Your version of repo %s is too old. |
| 351 | !!! We need at least version %s. |
David Pursehouse | 7838e38 | 2020-02-13 09:54:49 +0900 | [diff] [blame] | 352 | !!! A new version of repo (%s) is available. |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 353 | !!! You must upgrade before you can continue: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 354 | |
| 355 | cp %s %s |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 356 | """ % (ver_str, min_str, exp_str, WrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 357 | sys.exit(1) |
| 358 | |
| 359 | if exp > ver: |
Mike Frysinger | eea23b4 | 2020-02-26 16:21:08 -0500 | [diff] [blame] | 360 | print('\n... A new version of repo (%s) is available.' % (exp_str,), |
| 361 | file=sys.stderr) |
| 362 | if os.access(repo_path, os.W_OK): |
| 363 | print("""\ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 364 | ... You should upgrade soon: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 365 | cp %s %s |
Mike Frysinger | eea23b4 | 2020-02-26 16:21:08 -0500 | [diff] [blame] | 366 | """ % (WrapperPath(), repo_path), file=sys.stderr) |
| 367 | else: |
| 368 | print("""\ |
| 369 | ... New version is available at: %s |
| 370 | ... The launcher is run from: %s |
| 371 | !!! The launcher is not writable. Please talk to your sysadmin or distro |
| 372 | !!! to get an update installed. |
| 373 | """ % (WrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 374 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 375 | |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 376 | def _CheckRepoDir(repo_dir): |
| 377 | if not repo_dir: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 378 | print('no --repo-dir argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 379 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 380 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 381 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 382 | def _PruneOptions(argv, opt): |
| 383 | i = 0 |
| 384 | while i < len(argv): |
| 385 | a = argv[i] |
| 386 | if a == '--': |
| 387 | break |
| 388 | if a.startswith('--'): |
| 389 | eq = a.find('=') |
| 390 | if eq > 0: |
| 391 | a = a[0:eq] |
| 392 | if not opt.has_option(a): |
| 393 | del argv[i] |
| 394 | continue |
| 395 | i += 1 |
| 396 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 397 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 398 | class _UserAgentHandler(urllib.request.BaseHandler): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 399 | def http_request(self, req): |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 400 | req.add_header('User-Agent', user_agent.repo) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 401 | return req |
| 402 | |
| 403 | def https_request(self, req): |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 404 | req.add_header('User-Agent', user_agent.repo) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 405 | return req |
| 406 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 407 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 408 | def _AddPasswordFromUserInput(handler, msg, req): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 409 | # If repo could not find auth info from netrc, try to get it from user input |
| 410 | url = req.get_full_url() |
| 411 | user, password = handler.passwd.find_user_password(None, url) |
| 412 | if user is None: |
| 413 | print(msg) |
| 414 | try: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 415 | user = input('User: ') |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 416 | password = getpass.getpass() |
| 417 | except KeyboardInterrupt: |
| 418 | return |
| 419 | handler.passwd.add_password(None, url, user, password) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 420 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 421 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 422 | class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 423 | def http_error_401(self, req, fp, code, msg, headers): |
| 424 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 425 | return urllib.request.HTTPBasicAuthHandler.http_error_401( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 426 | self, req, fp, code, msg, headers) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 427 | |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 428 | def http_error_auth_reqed(self, authreq, host, req, headers): |
| 429 | try: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 430 | old_add_header = req.add_header |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 431 | |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 432 | def _add_header(name, val): |
| 433 | val = val.replace('\n', '') |
| 434 | old_add_header(name, val) |
| 435 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 436 | return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 437 | self, authreq, host, req, headers) |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 438 | except Exception: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 439 | reset = getattr(self, 'reset_retry_count', None) |
| 440 | if reset is not None: |
| 441 | reset() |
Shawn O. Pearce | b660539 | 2011-10-11 15:58:07 -0700 | [diff] [blame] | 442 | elif getattr(self, 'retried', None): |
| 443 | self.retried = 0 |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 444 | raise |
| 445 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 446 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 447 | class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 448 | def http_error_401(self, req, fp, code, msg, headers): |
| 449 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 450 | return urllib.request.HTTPDigestAuthHandler.http_error_401( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 451 | self, req, fp, code, msg, headers) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 452 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 453 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 454 | try: |
| 455 | old_add_header = req.add_header |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 456 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 457 | def _add_header(name, val): |
| 458 | val = val.replace('\n', '') |
| 459 | old_add_header(name, val) |
| 460 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 461 | return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 462 | self, auth_header, host, req, headers) |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 463 | except Exception: |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 464 | reset = getattr(self, 'reset_retry_count', None) |
| 465 | if reset is not None: |
| 466 | reset() |
| 467 | elif getattr(self, 'retried', None): |
| 468 | self.retried = 0 |
| 469 | raise |
| 470 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 471 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 472 | class _KerberosAuthHandler(urllib.request.BaseHandler): |
| 473 | def __init__(self): |
| 474 | self.retried = 0 |
| 475 | self.context = None |
| 476 | self.handler_order = urllib.request.BaseHandler.handler_order - 50 |
| 477 | |
David Pursehouse | 65b0ba5 | 2018-06-24 16:21:51 +0900 | [diff] [blame] | 478 | def http_error_401(self, req, fp, code, msg, headers): |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 479 | host = req.get_host() |
| 480 | retry = self.http_error_auth_reqed('www-authenticate', host, req, headers) |
| 481 | return retry |
| 482 | |
| 483 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 484 | try: |
| 485 | spn = "HTTP@%s" % host |
| 486 | authdata = self._negotiate_get_authdata(auth_header, headers) |
| 487 | |
| 488 | if self.retried > 3: |
| 489 | raise urllib.request.HTTPError(req.get_full_url(), 401, |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 490 | "Negotiate auth failed", headers, None) |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 491 | else: |
| 492 | self.retried += 1 |
| 493 | |
| 494 | neghdr = self._negotiate_get_svctk(spn, authdata) |
| 495 | if neghdr is None: |
| 496 | return None |
| 497 | |
| 498 | req.add_unredirected_header('Authorization', neghdr) |
| 499 | response = self.parent.open(req) |
| 500 | |
| 501 | srvauth = self._negotiate_get_authdata(auth_header, response.info()) |
| 502 | if self._validate_response(srvauth): |
| 503 | return response |
| 504 | except kerberos.GSSError: |
| 505 | return None |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 506 | except Exception: |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 507 | self.reset_retry_count() |
| 508 | raise |
| 509 | finally: |
| 510 | self._clean_context() |
| 511 | |
| 512 | def reset_retry_count(self): |
| 513 | self.retried = 0 |
| 514 | |
| 515 | def _negotiate_get_authdata(self, auth_header, headers): |
| 516 | authhdr = headers.get(auth_header, None) |
| 517 | if authhdr is not None: |
| 518 | for mech_tuple in authhdr.split(","): |
| 519 | mech, __, authdata = mech_tuple.strip().partition(" ") |
| 520 | if mech.lower() == "negotiate": |
| 521 | return authdata.strip() |
| 522 | return None |
| 523 | |
| 524 | def _negotiate_get_svctk(self, spn, authdata): |
| 525 | if authdata is None: |
| 526 | return None |
| 527 | |
| 528 | result, self.context = kerberos.authGSSClientInit(spn) |
| 529 | if result < kerberos.AUTH_GSS_COMPLETE: |
| 530 | return None |
| 531 | |
| 532 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 533 | if result < kerberos.AUTH_GSS_CONTINUE: |
| 534 | return None |
| 535 | |
| 536 | response = kerberos.authGSSClientResponse(self.context) |
| 537 | return "Negotiate %s" % response |
| 538 | |
| 539 | def _validate_response(self, authdata): |
| 540 | if authdata is None: |
| 541 | return None |
| 542 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 543 | if result == kerberos.AUTH_GSS_COMPLETE: |
| 544 | return True |
| 545 | return None |
| 546 | |
| 547 | def _clean_context(self): |
| 548 | if self.context is not None: |
| 549 | kerberos.authGSSClientClean(self.context) |
| 550 | self.context = None |
| 551 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 552 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 553 | def init_http(): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 554 | handlers = [_UserAgentHandler()] |
| 555 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 556 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 557 | try: |
| 558 | n = netrc.netrc() |
| 559 | for host in n.hosts: |
| 560 | p = n.hosts[host] |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 561 | mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 562 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 563 | except netrc.NetrcParseError: |
| 564 | pass |
Shawn O. Pearce | 7b947de | 2011-09-23 11:50:31 -0700 | [diff] [blame] | 565 | except IOError: |
| 566 | pass |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 567 | handlers.append(_BasicAuthHandler(mgr)) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 568 | handlers.append(_DigestAuthHandler(mgr)) |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 569 | if kerberos: |
| 570 | handlers.append(_KerberosAuthHandler()) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 571 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 572 | if 'http_proxy' in os.environ: |
| 573 | url = os.environ['http_proxy'] |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 574 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 575 | if 'REPO_CURL_VERBOSE' in os.environ: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 576 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 577 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 578 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 579 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 580 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 581 | def _Main(argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 582 | result = 0 |
| 583 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 584 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 585 | opt.add_option("--repo-dir", dest="repodir", |
| 586 | help="path to .repo/") |
| 587 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 588 | help="version of the wrapper script") |
| 589 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 590 | help="location of the wrapper script") |
| 591 | _PruneOptions(argv, opt) |
| 592 | opt, argv = opt.parse_args(argv) |
| 593 | |
| 594 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 595 | _CheckRepoDir(opt.repodir) |
| 596 | |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 597 | Version.wrapper_version = opt.wrapper_version |
| 598 | Version.wrapper_path = opt.wrapper_path |
| 599 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 600 | repo = _Repo(opt.repodir) |
| 601 | try: |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 602 | try: |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 603 | init_ssh() |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 604 | init_http() |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 605 | name, gopts, argv = repo._ParseArgs(argv) |
| 606 | run = lambda: repo._Run(name, gopts, argv) or 0 |
| 607 | if gopts.trace_python: |
| 608 | import trace |
| 609 | tracer = trace.Trace(count=False, trace=True, timing=True, |
| 610 | ignoredirs=set(sys.path[1:])) |
| 611 | result = tracer.runfunc(run) |
| 612 | else: |
| 613 | result = run() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 614 | finally: |
| 615 | close_ssh() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 616 | except KeyboardInterrupt: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 617 | print('aborted by user', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 618 | result = 1 |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 619 | except ManifestParseError as mpe: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 620 | print('fatal: %s' % mpe, file=sys.stderr) |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 621 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 622 | except RepoChangedException as rce: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 623 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 624 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 625 | argv = list(sys.argv) |
| 626 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 627 | try: |
Mike Frysinger | dd37fb2 | 2020-04-16 12:38:04 -0400 | [diff] [blame] | 628 | os.execv(sys.executable, [__file__] + argv) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 629 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 630 | print('fatal: cannot restart repo after upgrade', file=sys.stderr) |
| 631 | print('fatal: %s' % e, file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 632 | result = 128 |
| 633 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 634 | TerminatePager() |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 635 | sys.exit(result) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 636 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 637 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 638 | if __name__ == '__main__': |
| 639 | _Main(sys.argv[1:]) |