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