blob: 1c7f0af6970bc5dd6a2c92f787f074015e487eec [file] [log] [blame]
Mike Frysingera488af52020-09-06 13:33:45 -04001#!/usr/bin/env python3
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
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 Frysinger87fb5a12019-06-13 01:54:46 -040017"""The repo tool.
18
19People shouldn't run this directly; instead, they should use the `repo` wrapper
20which takes care of execing this entry point.
21"""
22
JoonCheol Parke9860722012-10-11 02:31:44 +090023import getpass
Mike Frysinger64477332023-08-21 21:20:32 -040024import json
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -070025import netrc
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026import optparse
27import os
Mike Frysinger949bc342020-02-18 21:37:00 -050028import shlex
Jason Changc6578442023-06-22 15:04:06 -070029import signal
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030import sys
Mike Frysinger7c321f12019-12-02 16:49:44 -050031import textwrap
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070032import time
Mike Frysingeracf63b22019-06-13 02:24:21 -040033import urllib.request
Mike Frysinger64477332023-08-21 21:20:32 -040034
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070035
Carlos Aguado1242e602014-02-03 13:48:47 +010036try:
Gavin Makea2e3302023-03-11 06:46:20 +000037 import kerberos
Carlos Aguado1242e602014-02-03 13:48:47 +010038except ImportError:
Gavin Makea2e3302023-03-11 06:46:20 +000039 kerberos = None
Carlos Aguado1242e602014-02-03 13:48:47 +010040
Mike Frysinger902665b2014-12-22 15:17:59 -050041from color import SetDefaultColoring
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080042from command import InteractiveCommand
43from command import MirrorSafeCommand
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070044from editor import Editor
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070045from error import DownloadError
Mike Frysinger64477332023-08-21 21:20:32 -040046from error import GitcUnsupportedError
Jarkko Pöyry87ea5912015-06-19 15:39:25 -070047from error import InvalidProjectGroupsError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080048from error import ManifestInvalidRevisionError
Conley Owens75ee0572012-11-15 17:33:11 -080049from error import NoManifestException
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070050from error import NoSuchProjectError
51from error import RepoChangedException
Mike Frysinger64477332023-08-21 21:20:32 -040052from error import RepoError
Jason Chang32b59562023-07-14 16:45:35 -070053from error import RepoExitError
54from error import RepoUnhandledExceptionError
Jason Chang1a3612f2023-08-08 14:12:53 -070055from error import SilentRepoExitError
Mike Frysinger64477332023-08-21 21:20:32 -040056import event_log
57from git_command import user_agent
58from git_config import RepoConfig
59from git_trace2_event_log import EventLog
Jason Chang8914b1f2023-05-26 12:44:50 -070060from manifest_xml import RepoClient
Mike Frysinger64477332023-08-21 21:20:32 -040061from pager import RunPager
62from pager import TerminatePager
63from repo_trace import SetTrace
64from repo_trace import SetTraceToStderr
65from repo_trace import Trace
David Pursehouse5c6eeac2012-10-11 16:44:48 +090066from subcmds import all_commands
Mike Frysinger64477332023-08-21 21:20:32 -040067from subcmds.version import Version
68from wrapper import Wrapper
69from wrapper import WrapperPath
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070070
Chirayu Desai217ea7d2013-03-01 19:14:38 +053071
Mike Frysinger37f28f12020-02-16 15:15:53 -050072# 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.
81MIN_PYTHON_VERSION_SOFT = (3, 6)
Peter Kjellerstedta3b2edf2021-04-15 01:32:40 +020082MIN_PYTHON_VERSION_HARD = (3, 6)
Mike Frysinger37f28f12020-02-16 15:15:53 -050083
84if sys.version_info.major < 3:
Gavin Makea2e3302023-03-11 06:46:20 +000085 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 Frysinger37f28f12020-02-16 15:15:53 -050090 sys.exit(1)
Gavin Makea2e3302023-03-11 06:46:20 +000091else:
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 Frysinger37f28f12020-02-16 15:15:53 -0500105
Jason Changc6578442023-06-22 15:04:06 -0700106KEYBOARD_INTERRUPT_EXIT = 128 + signal.SIGINT
Jason Chang32b59562023-07-14 16:45:35 -0700107MAX_PRINT_ERRORS = 5
Mike Frysinger37f28f12020-02-16 15:15:53 -0500108
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700109global_options = optparse.OptionParser(
Gavin Makea2e3302023-03-11 06:46:20 +0000110 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]",
111 add_help_option=False,
112)
113global_options.add_option(
114 "-h", "--help", action="store_true", help="show this help message and exit"
115)
116global_options.add_option(
117 "--help-all",
118 action="store_true",
119 help="show this help message with all subcommands and exit",
120)
121global_options.add_option(
122 "-p",
123 "--paginate",
124 dest="pager",
125 action="store_true",
126 help="display command output in the pager",
127)
128global_options.add_option(
129 "--no-pager", dest="pager", action="store_false", help="disable the pager"
130)
131global_options.add_option(
132 "--color",
133 choices=("auto", "always", "never"),
134 default=None,
135 help="control color usage: auto, always, never",
136)
137global_options.add_option(
138 "--trace",
139 dest="trace",
140 action="store_true",
141 help="trace git command execution (REPO_TRACE=1)",
142)
143global_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)
149global_options.add_option(
150 "--trace-python",
151 dest="trace_python",
152 action="store_true",
153 help="trace python command execution",
154)
155global_options.add_option(
156 "--time",
157 dest="time",
158 action="store_true",
159 help="time repo command execution",
160)
161global_options.add_option(
162 "--version",
163 dest="show_version",
164 action="store_true",
165 help="display this version of repo",
166)
167global_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)
173global_options.add_option(
174 "--event-log",
175 dest="event_log",
176 action="store",
177 help="filename of event log to append timeline to",
178)
179global_options.add_option(
180 "--git-trace2-event-log",
181 action="store",
182 help="directory to write git trace2 event log to",
183)
184global_options.add_option(
185 "--submanifest-path",
186 action="store",
187 metavar="REL_PATH",
188 help="submanifest path",
189)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700190
David Pursehouse819827a2020-02-12 15:20:19 +0900191
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700192class _Repo(object):
Gavin Makea2e3302023-03-11 06:46:20 +0000193 def __init__(self, repodir):
194 self.repodir = repodir
195 self.commands = all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700196
Gavin Makea2e3302023-03-11 06:46:20 +0000197 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 Owens7ba25be2012-11-14 14:18:06 -0800209 else:
Gavin Makea2e3302023-03-11 06:46:20 +0000210 cmd = self.commands["help"]()
211 if all_commands:
212 cmd.PrintAllCommandsBody()
213 else:
214 cmd.PrintCommonCommandsBody()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400215
Gavin Makea2e3302023-03-11 06:46:20 +0000216 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 Kasprzak30bc3542020-12-23 10:08:20 -0800229
Gavin Makea2e3302023-03-11 06:46:20 +0000230 if name:
231 name, alias_args = self._ExpandAlias(name)
232 argv = alias_args + argv
David Rileye0684ad2017-04-05 00:02:59 -0700233
Gavin Makea2e3302023-03-11 06:46:20 +0000234 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 Chang8914b1f2023-05-26 12:44:50 -0700310
311 if Wrapper().gitc_parse_clientdir(os.getcwd()):
312 print("GITC is not supported.", file=sys.stderr)
313 raise GitcUnsupportedError()
Gavin Makea2e3302023-03-11 06:46:20 +0000314
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 Makea2e3302023-03-11 06:46:20 +0000322 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 Makea2e3302023-03-11 06:46:20 +0000340 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 Changc6578442023-06-22 15:04:06 -0700371 def execute_command_helper():
372 """
373 Execute the subcommand.
374 """
375 nonlocal result
Gavin Makea2e3302023-03-11 06:46:20 +0000376 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 Changc6578442023-06-22 15:04:06 -0700410
411 def execute_command():
412 """
413 Execute the command and log uncaught exceptions.
414 """
415 try:
416 execute_command_helper()
Jason Chang32b59562023-07-14 16:45:35 -0700417 except (
418 KeyboardInterrupt,
419 SystemExit,
420 Exception,
421 RepoExitError,
422 ) as e:
Jason Changc6578442023-06-22 15:04:06 -0700423 ok = isinstance(e, SystemExit) and not e.code
Jason Chang32b59562023-07-14 16:45:35 -0700424 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 Changc6578442023-06-22 15:04:06 -0700443 if not ok:
Jason Changc6578442023-06-22 15:04:06 -0700444 git_trace2_event_log.ErrorEvent(
Gavin Mak1d2e99d2023-07-22 02:56:44 +0000445 f"RepoExitError:{exception_name}"
446 )
Jason Changc6578442023-06-22 15:04:06 -0700447 raise
448
449 try:
450 execute_command()
Gavin Makea2e3302023-03-11 06:46:20 +0000451 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 Chang32b59562023-07-14 16:45:35 -0700465 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000466 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 Chang32b59562023-07-14 16:45:35 -0700471 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000472 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 Chang32b59562023-07-14 16:45:35 -0700485 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000486 except SystemExit as e:
487 if e.code:
488 result = e.code
489 raise
Jason Changc6578442023-06-22 15:04:06 -0700490 except KeyboardInterrupt:
491 result = KEYBOARD_INTERRUPT_EXIT
492 raise
Jason Chang32b59562023-07-14 16:45:35 -0700493 except RepoExitError as e:
494 result = e.exit_code
495 raise
Jason Changc6578442023-06-22 15:04:06 -0700496 except Exception:
497 result = 1
498 raise
Gavin Makea2e3302023-03-11 06:46:20 +0000499 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 Projectcf31fe92008-10-21 07:00:00 -0700530
Conley Owens094cdbe2014-01-30 15:09:59 -0800531
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500532def _CheckWrapperVersion(ver_str, repo_path):
Gavin Makea2e3302023-03-11 06:46:20 +0000533 """Verify the repo launcher is new enough for this checkout.
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500534
Gavin Makea2e3302023-03-11 06:46:20 +0000535 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 Frysinger3285e4b2020-02-10 17:34:49 -0500545
Gavin Makea2e3302023-03-11 06:46:20 +0000546 if not repo_path:
547 repo_path = "~/bin/repo"
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700548
Gavin Makea2e3302023-03-11 06:46:20 +0000549 if not ver_str:
550 print("no --wrapper-version argument", file=sys.stderr)
551 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700552
Gavin Makea2e3302023-03-11 06:46:20 +0000553 # 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 Projectcf31fe92008-10-21 07:00:00 -0700556
Gavin Makea2e3302023-03-11 06:46:20 +0000557 exp_str = ".".join(map(str, exp))
558 if ver < MIN_REPO_VERSION:
559 print(
560 """
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500561repo: error:
562!!! Your version of repo %s is too old.
563!!! We need at least version %s.
David Pursehouse7838e382020-02-13 09:54:49 +0900564!!! A new version of repo (%s) is available.
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500565!!! You must upgrade before you can continue:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700566
567 cp %s %s
Gavin Makea2e3302023-03-11 06:46:20 +0000568"""
569 % (ver_str, min_str, exp_str, WrapperPath(), repo_path),
570 file=sys.stderr,
571 )
572 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700573
Gavin Makea2e3302023-03-11 06:46:20 +0000574 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 Projectcf31fe92008-10-21 07:00:00 -0700582... You should upgrade soon:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700583 cp %s %s
Gavin Makea2e3302023-03-11 06:46:20 +0000584"""
585 % (WrapperPath(), repo_path),
586 file=sys.stderr,
587 )
588 else:
589 print(
590 """\
Mike Frysingereea23b42020-02-26 16:21:08 -0500591... 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 Makea2e3302023-03-11 06:46:20 +0000595"""
596 % (WrapperPath(), repo_path),
597 file=sys.stderr,
598 )
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700599
David Pursehouse819827a2020-02-12 15:20:19 +0900600
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200601def _CheckRepoDir(repo_dir):
Gavin Makea2e3302023-03-11 06:46:20 +0000602 if not repo_dir:
603 print("no --repo-dir argument", file=sys.stderr)
604 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700605
David Pursehouse819827a2020-02-12 15:20:19 +0900606
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700607def _PruneOptions(argv, opt):
Gavin Makea2e3302023-03-11 06:46:20 +0000608 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 Projectcf31fe92008-10-21 07:00:00 -0700621
David Pursehouse819827a2020-02-12 15:20:19 +0900622
Sarah Owens1f7627f2012-10-31 09:21:55 -0700623class _UserAgentHandler(urllib.request.BaseHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000624 def http_request(self, req):
625 req.add_header("User-Agent", user_agent.repo)
626 return req
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700627
Gavin Makea2e3302023-03-11 06:46:20 +0000628 def https_request(self, req):
629 req.add_header("User-Agent", user_agent.repo)
630 return req
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700631
David Pursehouse819827a2020-02-12 15:20:19 +0900632
JoonCheol Parke9860722012-10-11 02:31:44 +0900633def _AddPasswordFromUserInput(handler, msg, req):
Gavin Makea2e3302023-03-11 06:46:20 +0000634 # 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 Parke9860722012-10-11 02:31:44 +0900645
David Pursehouse819827a2020-02-12 15:20:19 +0900646
Sarah Owens1f7627f2012-10-31 09:21:55 -0700647class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000648 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 Parke9860722012-10-11 02:31:44 +0900653
Gavin Makea2e3302023-03-11 06:46:20 +0000654 def http_error_auth_reqed(self, authreq, host, req, headers):
655 try:
656 old_add_header = req.add_header
David Pursehouse819827a2020-02-12 15:20:19 +0900657
Gavin Makea2e3302023-03-11 06:46:20 +0000658 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. Pearcefab96c62011-10-11 12:00:38 -0700675
David Pursehouse819827a2020-02-12 15:20:19 +0900676
Sarah Owens1f7627f2012-10-31 09:21:55 -0700677class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000678 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 Parke9860722012-10-11 02:31:44 +0900683
Gavin Makea2e3302023-03-11 06:46:20 +0000684 def http_error_auth_reqed(self, auth_header, host, req, headers):
685 try:
686 old_add_header = req.add_header
David Pursehouse819827a2020-02-12 15:20:19 +0900687
Gavin Makea2e3302023-03-11 06:46:20 +0000688 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 Xuae0a36c2012-01-31 11:10:09 +0800705
David Pursehouse819827a2020-02-12 15:20:19 +0900706
Carlos Aguado1242e602014-02-03 13:48:47 +0100707class _KerberosAuthHandler(urllib.request.BaseHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000708 def __init__(self):
709 self.retried = 0
710 self.context = None
711 self.handler_order = urllib.request.BaseHandler.handler_order - 50
Carlos Aguado1242e602014-02-03 13:48:47 +0100712
Gavin Makea2e3302023-03-11 06:46:20 +0000713 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 Aguado1242e602014-02-03 13:48:47 +0100719
Gavin Makea2e3302023-03-11 06:46:20 +0000720 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 Aguado1242e602014-02-03 13:48:47 +0100724
Gavin Makea2e3302023-03-11 06:46:20 +0000725 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 Aguado1242e602014-02-03 13:48:47 +0100735
Gavin Makea2e3302023-03-11 06:46:20 +0000736 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 Aguado1242e602014-02-03 13:48:47 +0100764 return None
765
Gavin Makea2e3302023-03-11 06:46:20 +0000766 def _negotiate_get_svctk(self, spn, authdata):
767 if authdata is None:
768 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100769
Gavin Makea2e3302023-03-11 06:46:20 +0000770 result, self.context = kerberos.authGSSClientInit(spn)
771 if result < kerberos.AUTH_GSS_COMPLETE:
772 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100773
Gavin Makea2e3302023-03-11 06:46:20 +0000774 result = kerberos.authGSSClientStep(self.context, authdata)
775 if result < kerberos.AUTH_GSS_CONTINUE:
776 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100777
Gavin Makea2e3302023-03-11 06:46:20 +0000778 response = kerberos.authGSSClientResponse(self.context)
779 return "Negotiate %s" % response
Carlos Aguado1242e602014-02-03 13:48:47 +0100780
Gavin Makea2e3302023-03-11 06:46:20 +0000781 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 Aguado1242e602014-02-03 13:48:47 +0100788
Gavin Makea2e3302023-03-11 06:46:20 +0000789 def _clean_context(self):
790 if self.context is not None:
791 kerberos.authGSSClientClean(self.context)
792 self.context = None
Carlos Aguado1242e602014-02-03 13:48:47 +0100793
David Pursehouse819827a2020-02-12 15:20:19 +0900794
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700795def init_http():
Gavin Makea2e3302023-03-11 06:46:20 +0000796 handlers = [_UserAgentHandler()]
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700797
Gavin Makea2e3302023-03-11 06:46:20 +0000798 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. Pearcebd0312a2011-09-19 10:04:23 -0700813
Gavin Makea2e3302023-03-11 06:46:20 +0000814 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. Pearce014d0602011-09-11 12:57:15 -0700823
David Pursehouse819827a2020-02-12 15:20:19 +0900824
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700825def _Main(argv):
Gavin Makea2e3302023-03-11 06:46:20 +0000826 result = 0
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400827
Gavin Makea2e3302023-03-11 06:46:20 +0000828 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 Projectcf31fe92008-10-21 07:00:00 -0700842
Gavin Makea2e3302023-03-11 06:46:20 +0000843 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
844 _CheckRepoDir(opt.repodir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700845
Gavin Makea2e3302023-03-11 06:46:20 +0000846 Version.wrapper_version = opt.wrapper_version
847 Version.wrapper_path = opt.wrapper_path
Shawn O. Pearceecff4f12011-11-29 15:01:33 -0800848
Gavin Makea2e3302023-03-11 06:46:20 +0000849 repo = _Repo(opt.repodir)
Joanna Wanga6c52f52022-11-03 16:51:19 -0400850
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700851 try:
Gavin Makea2e3302023-03-11 06:46:20 +0000852 init_http()
853 name, gopts, argv = repo._ParseArgs(argv)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400854
Gavin Makea2e3302023-03-11 06:46:20 +0000855 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 Chang32b59562023-07-14 16:45:35 -0700862 except RepoExitError as e:
Jason Chang1a3612f2023-08-08 14:12:53 -0700863 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 Chang32b59562023-07-14 16:45:35 -0700876 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000877 except KeyboardInterrupt:
878 print("aborted by user", file=sys.stderr)
Jason Changc6578442023-06-22 15:04:06 -0700879 result = KEYBOARD_INTERRUPT_EXIT
Gavin Makea2e3302023-03-11 06:46:20 +0000880 except RepoChangedException as rce:
881 # If repo changed, re-exec ourselves.
Gavin Makea2e3302023-03-11 06:46:20 +0000882 argv = list(sys.argv)
883 argv.extend(rce.extra_args)
884 try:
Gavin Makf1ddaaa2023-08-04 21:13:38 +0000885 os.execv(sys.executable, [sys.executable, __file__] + argv)
Gavin Makea2e3302023-03-11 06:46:20 +0000886 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 Projectcf31fe92008-10-21 07:00:00 -0700893
David Pursehouse819827a2020-02-12 15:20:19 +0900894
Gavin Makea2e3302023-03-11 06:46:20 +0000895if __name__ == "__main__":
896 _Main(sys.argv[1:])