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