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