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