Thanks to visit codestin.com
Credit goes to gerrit.googlesource.com

blob: 424ad91ca421d753ac60798be75c14f113ed1753 [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# Copyright (C) 2008 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Mike Frysinger87fb5a12019-06-13 01:54:46 -040016"""The repo tool.
17
18People shouldn't run this directly; instead, they should use the `repo` wrapper
19which takes care of execing this entry point.
20"""
21
JoonCheol Parke9860722012-10-11 02:31:44 +090022import getpass
Mike Frysinger64477332023-08-21 21:20:32 -040023import json
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -070024import netrc
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025import optparse
26import os
Mike Frysinger949bc342020-02-18 21:37:00 -050027import shlex
Jason Changc6578442023-06-22 15:04:06 -070028import signal
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029import sys
Mike Frysinger7c321f12019-12-02 16:49:44 -050030import textwrap
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070031import time
Mike Frysingeracf63b22019-06-13 02:24:21 -040032import urllib.request
Mike Frysinger64477332023-08-21 21:20:32 -040033
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +000034from repo_logging import RepoLogger
35
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036
Carlos Aguado1242e602014-02-03 13:48:47 +010037try:
Gavin Makea2e3302023-03-11 06:46:20 +000038 import kerberos
Carlos Aguado1242e602014-02-03 13:48:47 +010039except ImportError:
Gavin Makea2e3302023-03-11 06:46:20 +000040 kerberos = None
Carlos Aguado1242e602014-02-03 13:48:47 +010041
Mike Frysinger902665b2014-12-22 15:17:59 -050042from color import SetDefaultColoring
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080043from command import InteractiveCommand
44from command import MirrorSafeCommand
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070045from editor import Editor
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070046from error import DownloadError
Jarkko Pöyry87ea5912015-06-19 15:39:25 -070047from error import InvalidProjectGroupsError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080048from error import ManifestInvalidRevisionError
LuK133789f761c2023-11-01 09:37:53 +010049from error import ManifestParseError
Conley Owens75ee0572012-11-15 17:33:11 -080050from error import NoManifestException
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070051from error import NoSuchProjectError
52from error import RepoChangedException
Mike Frysinger64477332023-08-21 21:20:32 -040053from error import RepoError
Jason Chang32b59562023-07-14 16:45:35 -070054from error import RepoExitError
55from error import RepoUnhandledExceptionError
Jason Chang1a3612f2023-08-08 14:12:53 -070056from error import SilentRepoExitError
Mike Frysinger64477332023-08-21 21:20:32 -040057import event_log
58from git_command import user_agent
59from git_config import RepoConfig
60from git_trace2_event_log import EventLog
Jason Chang8914b1f2023-05-26 12:44:50 -070061from manifest_xml import RepoClient
Mike Frysinger64477332023-08-21 21:20:32 -040062from pager import RunPager
63from pager import TerminatePager
64from repo_trace import SetTrace
65from repo_trace import SetTraceToStderr
66from repo_trace import Trace
David Pursehouse5c6eeac2012-10-11 16:44:48 +090067from subcmds import all_commands
Mike Frysinger64477332023-08-21 21:20:32 -040068from subcmds.version import Version
69from wrapper import Wrapper
70from wrapper import WrapperPath
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070071
Chirayu Desai217ea7d2013-03-01 19:14:38 +053072
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +000073logger = RepoLogger(__file__)
74
75
Mike Frysinger37f28f12020-02-16 15:15:53 -050076# NB: These do not need to be kept in sync with the repo launcher script.
77# These may be much newer as it allows the repo launcher to roll between
78# different repo releases while source versions might require a newer python.
79#
80# The soft version is when we start warning users that the version is old and
81# we'll be dropping support for it. We'll refuse to work with versions older
82# than the hard version.
83#
84# python-3.6 is in Ubuntu Bionic.
85MIN_PYTHON_VERSION_SOFT = (3, 6)
Peter Kjellerstedta3b2edf2021-04-15 01:32:40 +020086MIN_PYTHON_VERSION_HARD = (3, 6)
Mike Frysinger37f28f12020-02-16 15:15:53 -050087
Mike Frysinger8f4f9852023-10-14 01:10:29 +054588if sys.version_info < MIN_PYTHON_VERSION_HARD:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +000089 logger.error(
Mike Frysinger8f4f9852023-10-14 01:10:29 +054590 "repo: error: Python version is too old; "
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +000091 "Please upgrade to Python %d.%d+.",
92 *MIN_PYTHON_VERSION_SOFT,
Gavin Makea2e3302023-03-11 06:46:20 +000093 )
Mike Frysinger37f28f12020-02-16 15:15:53 -050094 sys.exit(1)
Mike Frysinger8f4f9852023-10-14 01:10:29 +054595elif sys.version_info < MIN_PYTHON_VERSION_SOFT:
96 logger.error(
97 "repo: warning: your Python version is no longer supported; "
98 "Please upgrade to Python %d.%d+.",
99 *MIN_PYTHON_VERSION_SOFT,
100 )
Mike Frysinger37f28f12020-02-16 15:15:53 -0500101
Jason Changc6578442023-06-22 15:04:06 -0700102KEYBOARD_INTERRUPT_EXIT = 128 + signal.SIGINT
Jason Chang32b59562023-07-14 16:45:35 -0700103MAX_PRINT_ERRORS = 5
Mike Frysinger37f28f12020-02-16 15:15:53 -0500104
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105global_options = optparse.OptionParser(
Gavin Makea2e3302023-03-11 06:46:20 +0000106 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]",
107 add_help_option=False,
108)
109global_options.add_option(
110 "-h", "--help", action="store_true", help="show this help message and exit"
111)
112global_options.add_option(
113 "--help-all",
114 action="store_true",
115 help="show this help message with all subcommands and exit",
116)
117global_options.add_option(
118 "-p",
119 "--paginate",
120 dest="pager",
121 action="store_true",
122 help="display command output in the pager",
123)
124global_options.add_option(
125 "--no-pager", dest="pager", action="store_false", help="disable the pager"
126)
127global_options.add_option(
128 "--color",
129 choices=("auto", "always", "never"),
130 default=None,
131 help="control color usage: auto, always, never",
132)
133global_options.add_option(
134 "--trace",
135 dest="trace",
136 action="store_true",
137 help="trace git command execution (REPO_TRACE=1)",
138)
139global_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)
145global_options.add_option(
146 "--trace-python",
147 dest="trace_python",
148 action="store_true",
149 help="trace python command execution",
150)
151global_options.add_option(
152 "--time",
153 dest="time",
154 action="store_true",
155 help="time repo command execution",
156)
157global_options.add_option(
158 "--version",
159 dest="show_version",
160 action="store_true",
161 help="display this version of repo",
162)
163global_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)
169global_options.add_option(
170 "--event-log",
171 dest="event_log",
172 action="store",
173 help="filename of event log to append timeline to",
174)
175global_options.add_option(
176 "--git-trace2-event-log",
177 action="store",
178 help="directory to write git trace2 event log to",
179)
180global_options.add_option(
181 "--submanifest-path",
182 action="store",
183 metavar="REL_PATH",
184 help="submanifest path",
185)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700186
David Pursehouse819827a2020-02-12 15:20:19 +0900187
Mike Frysingerd4aee652023-10-19 05:13:32 -0400188class _Repo:
Gavin Makea2e3302023-03-11 06:46:20 +0000189 def __init__(self, repodir):
190 self.repodir = repodir
191 self.commands = all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700192
Gavin Makea2e3302023-03-11 06:46:20 +0000193 def _PrintHelp(self, short: bool = False, all_commands: bool = False):
194 """Show --help screen."""
195 global_options.print_help()
196 print()
197 if short:
198 commands = " ".join(sorted(self.commands))
199 wrapped_commands = textwrap.wrap(commands, width=77)
Jason R. Coombsb32ccbb2023-09-29 11:04:49 -0400200 help_commands = "".join(f"\n {x}" for x in wrapped_commands)
201 print(f"Available commands:{help_commands}")
Gavin Makea2e3302023-03-11 06:46:20 +0000202 print("\nRun `repo help <command>` for command-specific details.")
203 print("Bug reports:", Wrapper().BUG_URL)
Conley Owens7ba25be2012-11-14 14:18:06 -0800204 else:
Gavin Makea2e3302023-03-11 06:46:20 +0000205 cmd = self.commands["help"]()
206 if all_commands:
207 cmd.PrintAllCommandsBody()
208 else:
209 cmd.PrintCommonCommandsBody()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400210
Gavin Makea2e3302023-03-11 06:46:20 +0000211 def _ParseArgs(self, argv):
212 """Parse the main `repo` command line options."""
213 for i, arg in enumerate(argv):
214 if not arg.startswith("-"):
215 name = arg
216 glob = argv[:i]
217 argv = argv[i + 1 :]
218 break
219 else:
220 name = None
221 glob = argv
222 argv = []
223 gopts, _gargs = global_options.parse_args(glob)
Ian Kasprzak30bc3542020-12-23 10:08:20 -0800224
Gavin Makea2e3302023-03-11 06:46:20 +0000225 if name:
226 name, alias_args = self._ExpandAlias(name)
227 argv = alias_args + argv
David Rileye0684ad2017-04-05 00:02:59 -0700228
Gavin Makea2e3302023-03-11 06:46:20 +0000229 return (name, gopts, argv)
230
231 def _ExpandAlias(self, name):
232 """Look up user registered aliases."""
233 # We don't resolve aliases for existing subcommands. This matches git.
234 if name in self.commands:
235 return name, []
236
Jason R. Coombsb32ccbb2023-09-29 11:04:49 -0400237 key = f"alias.{name}"
Gavin Makea2e3302023-03-11 06:46:20 +0000238 alias = RepoConfig.ForRepository(self.repodir).GetString(key)
239 if alias is None:
240 alias = RepoConfig.ForUser().GetString(key)
241 if alias is None:
242 return name, []
243
244 args = alias.strip().split(" ", 1)
245 name = args[0]
246 if len(args) == 2:
247 args = shlex.split(args[1])
248 else:
249 args = []
250 return name, args
251
252 def _Run(self, name, gopts, argv):
253 """Execute the requested subcommand."""
254 result = 0
255
256 # Handle options that terminate quickly first.
257 if gopts.help or gopts.help_all:
258 self._PrintHelp(short=False, all_commands=gopts.help_all)
259 return 0
260 elif gopts.show_version:
261 # Always allow global --version regardless of subcommand validity.
262 name = "version"
263 elif gopts.show_toplevel:
264 print(os.path.dirname(self.repodir))
265 return 0
266 elif not name:
267 # No subcommand specified, so show the help/subcommand.
268 self._PrintHelp(short=True)
269 return 1
270
Josip Sokcevic8896b682024-02-21 11:04:03 -0800271 git_trace2_event_log = EventLog()
272 run = (
273 lambda: self._RunLong(name, gopts, argv, git_trace2_event_log) or 0
274 )
Gavin Makea2e3302023-03-11 06:46:20 +0000275 with Trace(
Josip Sokcevic8896b682024-02-21 11:04:03 -0800276 "starting new command: %s [sid=%s]",
Gavin Makea2e3302023-03-11 06:46:20 +0000277 ", ".join([name] + argv),
Josip Sokcevic8896b682024-02-21 11:04:03 -0800278 git_trace2_event_log.full_sid,
Gavin Makea2e3302023-03-11 06:46:20 +0000279 first_trace=True,
280 ):
281 if gopts.trace_python:
282 import trace
283
284 tracer = trace.Trace(
285 count=False,
286 trace=True,
287 timing=True,
288 ignoredirs=set(sys.path[1:]),
289 )
290 result = tracer.runfunc(run)
291 else:
292 result = run()
293 return result
294
Josip Sokcevic8896b682024-02-21 11:04:03 -0800295 def _RunLong(self, name, gopts, argv, git_trace2_event_log):
Gavin Makea2e3302023-03-11 06:46:20 +0000296 """Execute the (longer running) requested subcommand."""
297 result = 0
298 SetDefaultColoring(gopts.color)
299
Gavin Makea2e3302023-03-11 06:46:20 +0000300 outer_client = RepoClient(self.repodir)
301 repo_client = outer_client
302 if gopts.submanifest_path:
303 repo_client = RepoClient(
304 self.repodir,
305 submanifest_path=gopts.submanifest_path,
306 outer_client=outer_client,
307 )
Jason Chang8914b1f2023-05-26 12:44:50 -0700308
Gavin Makea2e3302023-03-11 06:46:20 +0000309 try:
310 cmd = self.commands[name](
311 repodir=self.repodir,
312 client=repo_client,
313 manifest=repo_client.manifest,
314 outer_client=outer_client,
315 outer_manifest=outer_client.manifest,
Gavin Makea2e3302023-03-11 06:46:20 +0000316 git_event_log=git_trace2_event_log,
317 )
318 except KeyError:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000319 logger.error(
320 "repo: '%s' is not a repo command. See 'repo help'.", name
Gavin Makea2e3302023-03-11 06:46:20 +0000321 )
322 return 1
323
324 Editor.globalConfig = cmd.client.globalConfig
325
326 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000327 logger.error("fatal: '%s' requires a working directory", name)
Gavin Makea2e3302023-03-11 06:46:20 +0000328 return 1
329
Gavin Makea2e3302023-03-11 06:46:20 +0000330 try:
331 copts, cargs = cmd.OptionParser.parse_args(argv)
332 copts = cmd.ReadEnvironmentOptions(copts)
333 except NoManifestException as e:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000334 logger.error("error: in `%s`: %s", " ".join([name] + argv), e)
335 logger.error(
336 "error: manifest missing or unreadable -- please run init"
Gavin Makea2e3302023-03-11 06:46:20 +0000337 )
338 return 1
339
340 if gopts.pager is not False and not isinstance(cmd, InteractiveCommand):
341 config = cmd.client.globalConfig
342 if gopts.pager:
343 use_pager = True
344 else:
345 use_pager = config.GetBoolean("pager.%s" % name)
346 if use_pager is None:
347 use_pager = cmd.WantPager(copts)
348 if use_pager:
349 RunPager(config)
350
351 start = time.time()
352 cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start)
353 cmd.event_log.SetParent(cmd_event)
Josip Sokcevicfafd1ec2024-11-22 00:02:40 +0000354 git_trace2_event_log.StartEvent(["repo", name] + argv)
Gavin Makea2e3302023-03-11 06:46:20 +0000355 git_trace2_event_log.CommandEvent(name="repo", subcommands=[name])
356
Jason Changc6578442023-06-22 15:04:06 -0700357 def execute_command_helper():
358 """
359 Execute the subcommand.
360 """
361 nonlocal result
Gavin Makea2e3302023-03-11 06:46:20 +0000362 cmd.CommonValidateOptions(copts, cargs)
363 cmd.ValidateOptions(copts, cargs)
364
365 this_manifest_only = copts.this_manifest_only
366 outer_manifest = copts.outer_manifest
367 if cmd.MULTI_MANIFEST_SUPPORT or this_manifest_only:
368 result = cmd.Execute(copts, cargs)
369 elif outer_manifest and repo_client.manifest.is_submanifest:
370 # The command does not support multi-manifest, we are using a
371 # submanifest, and the command line is for the outermost
372 # manifest. Re-run using the outermost manifest, which will
373 # recurse through the submanifests.
374 gopts.submanifest_path = ""
375 result = self._Run(name, gopts, argv)
376 else:
377 # No multi-manifest support. Run the command in the current
378 # (sub)manifest, and then any child submanifests.
379 result = cmd.Execute(copts, cargs)
380 for submanifest in repo_client.manifest.submanifests.values():
381 spec = submanifest.ToSubmanifestSpec()
382 gopts.submanifest_path = submanifest.repo_client.path_prefix
383 child_argv = argv[:]
384 child_argv.append("--no-outer-manifest")
385 # Not all subcommands support the 3 manifest options, so
386 # only add them if the original command includes them.
387 if hasattr(copts, "manifest_url"):
388 child_argv.extend(["--manifest-url", spec.manifestUrl])
389 if hasattr(copts, "manifest_name"):
390 child_argv.extend(
391 ["--manifest-name", spec.manifestName]
392 )
393 if hasattr(copts, "manifest_branch"):
394 child_argv.extend(["--manifest-branch", spec.revision])
395 result = self._Run(name, gopts, child_argv) or result
Jason Changc6578442023-06-22 15:04:06 -0700396
397 def execute_command():
398 """
399 Execute the command and log uncaught exceptions.
400 """
401 try:
402 execute_command_helper()
Jason Chang32b59562023-07-14 16:45:35 -0700403 except (
404 KeyboardInterrupt,
405 SystemExit,
406 Exception,
407 RepoExitError,
408 ) as e:
Jason Changc6578442023-06-22 15:04:06 -0700409 ok = isinstance(e, SystemExit) and not e.code
Jason Chang32b59562023-07-14 16:45:35 -0700410 exception_name = type(e).__name__
411 if isinstance(e, RepoUnhandledExceptionError):
412 exception_name = type(e.error).__name__
413 if isinstance(e, RepoExitError):
414 aggregated_errors = e.aggregate_errors or []
415 for error in aggregated_errors:
416 project = None
417 if isinstance(error, RepoError):
418 project = error.project
419 error_info = json.dumps(
420 {
421 "ErrorType": type(error).__name__,
Josip Sokcevica3a73722024-03-14 23:50:33 +0000422 "Project": str(project),
Jason Chang32b59562023-07-14 16:45:35 -0700423 "Message": str(error),
424 }
425 )
426 git_trace2_event_log.ErrorEvent(
427 f"AggregateExitError:{error_info}"
428 )
Jason Changc6578442023-06-22 15:04:06 -0700429 if not ok:
Jason Changc6578442023-06-22 15:04:06 -0700430 git_trace2_event_log.ErrorEvent(
Gavin Mak1d2e99d2023-07-22 02:56:44 +0000431 f"RepoExitError:{exception_name}"
432 )
Jason Changc6578442023-06-22 15:04:06 -0700433 raise
434
435 try:
436 execute_command()
Gavin Makea2e3302023-03-11 06:46:20 +0000437 except (
438 DownloadError,
439 ManifestInvalidRevisionError,
LuK133789f761c2023-11-01 09:37:53 +0100440 ManifestParseError,
Gavin Makea2e3302023-03-11 06:46:20 +0000441 NoManifestException,
442 ) as e:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000443 logger.error("error: in `%s`: %s", " ".join([name] + argv), e)
Gavin Makea2e3302023-03-11 06:46:20 +0000444 if isinstance(e, NoManifestException):
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000445 logger.error(
446 "error: manifest missing or unreadable -- please run init"
Gavin Makea2e3302023-03-11 06:46:20 +0000447 )
Jason Chang32b59562023-07-14 16:45:35 -0700448 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000449 except NoSuchProjectError as e:
450 if e.name:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000451 logger.error("error: project %s not found", e.name)
Gavin Makea2e3302023-03-11 06:46:20 +0000452 else:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000453 logger.error("error: no project in current directory")
Jason Chang32b59562023-07-14 16:45:35 -0700454 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000455 except InvalidProjectGroupsError as e:
456 if e.name:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000457 logger.error(
458 "error: project group must be enabled for project %s",
459 e.name,
Gavin Makea2e3302023-03-11 06:46:20 +0000460 )
461 else:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000462 logger.error(
Gavin Makea2e3302023-03-11 06:46:20 +0000463 "error: project group must be enabled for the project in "
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000464 "the current directory"
Gavin Makea2e3302023-03-11 06:46:20 +0000465 )
Jason Chang32b59562023-07-14 16:45:35 -0700466 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000467 except SystemExit as e:
468 if e.code:
469 result = e.code
470 raise
Jason Changc6578442023-06-22 15:04:06 -0700471 except KeyboardInterrupt:
472 result = KEYBOARD_INTERRUPT_EXIT
473 raise
Jason Chang32b59562023-07-14 16:45:35 -0700474 except RepoExitError as e:
475 result = e.exit_code
476 raise
Jason Changc6578442023-06-22 15:04:06 -0700477 except Exception:
478 result = 1
479 raise
Gavin Makea2e3302023-03-11 06:46:20 +0000480 finally:
481 finish = time.time()
482 elapsed = finish - start
483 hours, remainder = divmod(elapsed, 3600)
484 minutes, seconds = divmod(remainder, 60)
485 if gopts.time:
486 if hours == 0:
487 print(
488 "real\t%dm%.3fs" % (minutes, seconds), file=sys.stderr
489 )
490 else:
491 print(
492 "real\t%dh%dm%.3fs" % (hours, minutes, seconds),
493 file=sys.stderr,
494 )
495
496 cmd.event_log.FinishEvent(
497 cmd_event, finish, result is None or result == 0
498 )
499 git_trace2_event_log.DefParamRepoEvents(
500 cmd.manifest.manifestProject.config.DumpConfigDict()
501 )
502 git_trace2_event_log.ExitEvent(result)
503
504 if gopts.event_log:
505 cmd.event_log.Write(
506 os.path.abspath(os.path.expanduser(gopts.event_log))
507 )
508
509 git_trace2_event_log.Write(gopts.git_trace2_event_log)
510 return result
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700511
Conley Owens094cdbe2014-01-30 15:09:59 -0800512
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500513def _CheckWrapperVersion(ver_str, repo_path):
Gavin Makea2e3302023-03-11 06:46:20 +0000514 """Verify the repo launcher is new enough for this checkout.
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500515
Gavin Makea2e3302023-03-11 06:46:20 +0000516 Args:
517 ver_str: The version string passed from the repo launcher when it ran
518 us.
519 repo_path: The path to the repo launcher that loaded us.
520 """
521 # Refuse to work with really old wrapper versions. We don't test these,
522 # so might as well require a somewhat recent sane version.
523 # v1.15 of the repo launcher was released in ~Mar 2012.
524 MIN_REPO_VERSION = (1, 15)
525 min_str = ".".join(str(x) for x in MIN_REPO_VERSION)
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500526
Gavin Makea2e3302023-03-11 06:46:20 +0000527 if not repo_path:
528 repo_path = "~/bin/repo"
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700529
Gavin Makea2e3302023-03-11 06:46:20 +0000530 if not ver_str:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000531 logger.error("no --wrapper-version argument")
Gavin Makea2e3302023-03-11 06:46:20 +0000532 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700533
Gavin Makea2e3302023-03-11 06:46:20 +0000534 # Pull out the version of the repo launcher we know about to compare.
535 exp = Wrapper().VERSION
536 ver = tuple(map(int, ver_str.split(".")))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700537
Gavin Makea2e3302023-03-11 06:46:20 +0000538 exp_str = ".".join(map(str, exp))
539 if ver < MIN_REPO_VERSION:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000540 logger.error(
Gavin Makea2e3302023-03-11 06:46:20 +0000541 """
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500542repo: error:
543!!! Your version of repo %s is too old.
544!!! We need at least version %s.
David Pursehouse7838e382020-02-13 09:54:49 +0900545!!! A new version of repo (%s) is available.
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500546!!! You must upgrade before you can continue:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700547
548 cp %s %s
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000549""",
550 ver_str,
551 min_str,
552 exp_str,
553 WrapperPath(),
554 repo_path,
Gavin Makea2e3302023-03-11 06:46:20 +0000555 )
556 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700557
Gavin Makea2e3302023-03-11 06:46:20 +0000558 if exp > ver:
Aravind Vasudevan8bc50002023-10-13 19:22:47 +0000559 logger.warning(
560 "\n... A new version of repo (%s) is available.", exp_str
561 )
Gavin Makea2e3302023-03-11 06:46:20 +0000562 if os.access(repo_path, os.W_OK):
Aravind Vasudevan8bc50002023-10-13 19:22:47 +0000563 logger.warning(
Gavin Makea2e3302023-03-11 06:46:20 +0000564 """\
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700565... You should upgrade soon:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700566 cp %s %s
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000567""",
568 WrapperPath(),
569 repo_path,
Gavin Makea2e3302023-03-11 06:46:20 +0000570 )
571 else:
Aravind Vasudevan8bc50002023-10-13 19:22:47 +0000572 logger.warning(
Gavin Makea2e3302023-03-11 06:46:20 +0000573 """\
Mike Frysingereea23b42020-02-26 16:21:08 -0500574... New version is available at: %s
575... The launcher is run from: %s
576!!! The launcher is not writable. Please talk to your sysadmin or distro
577!!! to get an update installed.
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000578""",
579 WrapperPath(),
580 repo_path,
Gavin Makea2e3302023-03-11 06:46:20 +0000581 )
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700582
David Pursehouse819827a2020-02-12 15:20:19 +0900583
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200584def _CheckRepoDir(repo_dir):
Gavin Makea2e3302023-03-11 06:46:20 +0000585 if not repo_dir:
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000586 logger.error("no --repo-dir argument")
Gavin Makea2e3302023-03-11 06:46:20 +0000587 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700588
David Pursehouse819827a2020-02-12 15:20:19 +0900589
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700590def _PruneOptions(argv, opt):
Gavin Makea2e3302023-03-11 06:46:20 +0000591 i = 0
592 while i < len(argv):
593 a = argv[i]
594 if a == "--":
595 break
596 if a.startswith("--"):
597 eq = a.find("=")
598 if eq > 0:
599 a = a[0:eq]
600 if not opt.has_option(a):
601 del argv[i]
602 continue
603 i += 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700604
David Pursehouse819827a2020-02-12 15:20:19 +0900605
Sarah Owens1f7627f2012-10-31 09:21:55 -0700606class _UserAgentHandler(urllib.request.BaseHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000607 def http_request(self, req):
608 req.add_header("User-Agent", user_agent.repo)
609 return req
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700610
Gavin Makea2e3302023-03-11 06:46:20 +0000611 def https_request(self, req):
612 req.add_header("User-Agent", user_agent.repo)
613 return req
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700614
David Pursehouse819827a2020-02-12 15:20:19 +0900615
JoonCheol Parke9860722012-10-11 02:31:44 +0900616def _AddPasswordFromUserInput(handler, msg, req):
Gavin Makea2e3302023-03-11 06:46:20 +0000617 # If repo could not find auth info from netrc, try to get it from user input
618 url = req.get_full_url()
619 user, password = handler.passwd.find_user_password(None, url)
620 if user is None:
621 print(msg)
622 try:
623 user = input("User: ")
624 password = getpass.getpass()
625 except KeyboardInterrupt:
626 return
627 handler.passwd.add_password(None, url, user, password)
JoonCheol Parke9860722012-10-11 02:31:44 +0900628
David Pursehouse819827a2020-02-12 15:20:19 +0900629
Sarah Owens1f7627f2012-10-31 09:21:55 -0700630class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000631 def http_error_401(self, req, fp, code, msg, headers):
632 _AddPasswordFromUserInput(self, msg, req)
633 return urllib.request.HTTPBasicAuthHandler.http_error_401(
634 self, req, fp, code, msg, headers
635 )
JoonCheol Parke9860722012-10-11 02:31:44 +0900636
Gavin Makea2e3302023-03-11 06:46:20 +0000637 def http_error_auth_reqed(self, authreq, host, req, headers):
638 try:
639 old_add_header = req.add_header
David Pursehouse819827a2020-02-12 15:20:19 +0900640
Gavin Makea2e3302023-03-11 06:46:20 +0000641 def _add_header(name, val):
642 val = val.replace("\n", "")
643 old_add_header(name, val)
644
645 req.add_header = _add_header
646 return (
647 urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed(
648 self, authreq, host, req, headers
649 )
650 )
651 except Exception:
652 reset = getattr(self, "reset_retry_count", None)
653 if reset is not None:
654 reset()
655 elif getattr(self, "retried", None):
656 self.retried = 0
657 raise
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700658
David Pursehouse819827a2020-02-12 15:20:19 +0900659
Sarah Owens1f7627f2012-10-31 09:21:55 -0700660class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000661 def http_error_401(self, req, fp, code, msg, headers):
662 _AddPasswordFromUserInput(self, msg, req)
663 return urllib.request.HTTPDigestAuthHandler.http_error_401(
664 self, req, fp, code, msg, headers
665 )
JoonCheol Parke9860722012-10-11 02:31:44 +0900666
Gavin Makea2e3302023-03-11 06:46:20 +0000667 def http_error_auth_reqed(self, auth_header, host, req, headers):
668 try:
669 old_add_header = req.add_header
David Pursehouse819827a2020-02-12 15:20:19 +0900670
Gavin Makea2e3302023-03-11 06:46:20 +0000671 def _add_header(name, val):
672 val = val.replace("\n", "")
673 old_add_header(name, val)
674
675 req.add_header = _add_header
676 return (
677 urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
678 self, auth_header, host, req, headers
679 )
680 )
681 except Exception:
682 reset = getattr(self, "reset_retry_count", None)
683 if reset is not None:
684 reset()
685 elif getattr(self, "retried", None):
686 self.retried = 0
687 raise
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800688
David Pursehouse819827a2020-02-12 15:20:19 +0900689
Carlos Aguado1242e602014-02-03 13:48:47 +0100690class _KerberosAuthHandler(urllib.request.BaseHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000691 def __init__(self):
692 self.retried = 0
693 self.context = None
694 self.handler_order = urllib.request.BaseHandler.handler_order - 50
Carlos Aguado1242e602014-02-03 13:48:47 +0100695
Gavin Makea2e3302023-03-11 06:46:20 +0000696 def http_error_401(self, req, fp, code, msg, headers):
697 host = req.get_host()
698 retry = self.http_error_auth_reqed(
699 "www-authenticate", host, req, headers
700 )
701 return retry
Carlos Aguado1242e602014-02-03 13:48:47 +0100702
Gavin Makea2e3302023-03-11 06:46:20 +0000703 def http_error_auth_reqed(self, auth_header, host, req, headers):
704 try:
705 spn = "HTTP@%s" % host
706 authdata = self._negotiate_get_authdata(auth_header, headers)
Carlos Aguado1242e602014-02-03 13:48:47 +0100707
Gavin Makea2e3302023-03-11 06:46:20 +0000708 if self.retried > 3:
709 raise urllib.request.HTTPError(
710 req.get_full_url(),
711 401,
712 "Negotiate auth failed",
713 headers,
714 None,
715 )
716 else:
717 self.retried += 1
Carlos Aguado1242e602014-02-03 13:48:47 +0100718
Gavin Makea2e3302023-03-11 06:46:20 +0000719 neghdr = self._negotiate_get_svctk(spn, authdata)
720 if neghdr is None:
721 return None
722
723 req.add_unredirected_header("Authorization", neghdr)
724 response = self.parent.open(req)
725
726 srvauth = self._negotiate_get_authdata(auth_header, response.info())
727 if self._validate_response(srvauth):
728 return response
729 except kerberos.GSSError:
730 return None
731 except Exception:
732 self.reset_retry_count()
733 raise
734 finally:
735 self._clean_context()
736
737 def reset_retry_count(self):
738 self.retried = 0
739
740 def _negotiate_get_authdata(self, auth_header, headers):
741 authhdr = headers.get(auth_header, None)
742 if authhdr is not None:
743 for mech_tuple in authhdr.split(","):
744 mech, __, authdata = mech_tuple.strip().partition(" ")
745 if mech.lower() == "negotiate":
746 return authdata.strip()
Carlos Aguado1242e602014-02-03 13:48:47 +0100747 return None
748
Gavin Makea2e3302023-03-11 06:46:20 +0000749 def _negotiate_get_svctk(self, spn, authdata):
750 if authdata is None:
751 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100752
Gavin Makea2e3302023-03-11 06:46:20 +0000753 result, self.context = kerberos.authGSSClientInit(spn)
754 if result < kerberos.AUTH_GSS_COMPLETE:
755 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100756
Gavin Makea2e3302023-03-11 06:46:20 +0000757 result = kerberos.authGSSClientStep(self.context, authdata)
758 if result < kerberos.AUTH_GSS_CONTINUE:
759 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100760
Gavin Makea2e3302023-03-11 06:46:20 +0000761 response = kerberos.authGSSClientResponse(self.context)
762 return "Negotiate %s" % response
Carlos Aguado1242e602014-02-03 13:48:47 +0100763
Gavin Makea2e3302023-03-11 06:46:20 +0000764 def _validate_response(self, authdata):
765 if authdata is None:
766 return None
767 result = kerberos.authGSSClientStep(self.context, authdata)
768 if result == kerberos.AUTH_GSS_COMPLETE:
769 return True
770 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100771
Gavin Makea2e3302023-03-11 06:46:20 +0000772 def _clean_context(self):
773 if self.context is not None:
774 kerberos.authGSSClientClean(self.context)
775 self.context = None
Carlos Aguado1242e602014-02-03 13:48:47 +0100776
David Pursehouse819827a2020-02-12 15:20:19 +0900777
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700778def init_http():
Gavin Makea2e3302023-03-11 06:46:20 +0000779 handlers = [_UserAgentHandler()]
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700780
Gavin Makea2e3302023-03-11 06:46:20 +0000781 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
782 try:
783 n = netrc.netrc()
784 for host in n.hosts:
785 p = n.hosts[host]
786 mgr.add_password(p[1], "http://%s/" % host, p[0], p[2])
787 mgr.add_password(p[1], "https://%s/" % host, p[0], p[2])
788 except netrc.NetrcParseError:
789 pass
Jason R. Coombsae824fb2023-10-20 23:32:40 +0545790 except OSError:
Gavin Makea2e3302023-03-11 06:46:20 +0000791 pass
792 handlers.append(_BasicAuthHandler(mgr))
793 handlers.append(_DigestAuthHandler(mgr))
794 if kerberos:
795 handlers.append(_KerberosAuthHandler())
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700796
Gavin Makea2e3302023-03-11 06:46:20 +0000797 if "http_proxy" in os.environ:
798 url = os.environ["http_proxy"]
799 handlers.append(
800 urllib.request.ProxyHandler({"http": url, "https": url})
801 )
802 if "REPO_CURL_VERBOSE" in os.environ:
803 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
804 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
805 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700806
David Pursehouse819827a2020-02-12 15:20:19 +0900807
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700808def _Main(argv):
Gavin Makea2e3302023-03-11 06:46:20 +0000809 result = 0
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400810
Gavin Makea2e3302023-03-11 06:46:20 +0000811 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
812 opt.add_option("--repo-dir", dest="repodir", help="path to .repo/")
813 opt.add_option(
814 "--wrapper-version",
815 dest="wrapper_version",
816 help="version of the wrapper script",
817 )
818 opt.add_option(
819 "--wrapper-path",
820 dest="wrapper_path",
821 help="location of the wrapper script",
822 )
823 _PruneOptions(argv, opt)
824 opt, argv = opt.parse_args(argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700825
Gavin Makea2e3302023-03-11 06:46:20 +0000826 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
827 _CheckRepoDir(opt.repodir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700828
Gavin Makea2e3302023-03-11 06:46:20 +0000829 Version.wrapper_version = opt.wrapper_version
830 Version.wrapper_path = opt.wrapper_path
Shawn O. Pearceecff4f12011-11-29 15:01:33 -0800831
Gavin Makea2e3302023-03-11 06:46:20 +0000832 repo = _Repo(opt.repodir)
Joanna Wanga6c52f52022-11-03 16:51:19 -0400833
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700834 try:
Gavin Makea2e3302023-03-11 06:46:20 +0000835 init_http()
836 name, gopts, argv = repo._ParseArgs(argv)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400837
Gavin Makea2e3302023-03-11 06:46:20 +0000838 if gopts.trace:
839 SetTrace()
840
841 if gopts.trace_to_stderr:
842 SetTraceToStderr()
843
844 result = repo._Run(name, gopts, argv) or 0
Jason Chang32b59562023-07-14 16:45:35 -0700845 except RepoExitError as e:
Jason Chang1a3612f2023-08-08 14:12:53 -0700846 if not isinstance(e, SilentRepoExitError):
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +0000847 logger.log_aggregated_errors(e)
Jason Chang32b59562023-07-14 16:45:35 -0700848 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000849 except KeyboardInterrupt:
850 print("aborted by user", file=sys.stderr)
Jason Changc6578442023-06-22 15:04:06 -0700851 result = KEYBOARD_INTERRUPT_EXIT
Gavin Makea2e3302023-03-11 06:46:20 +0000852 except RepoChangedException as rce:
853 # If repo changed, re-exec ourselves.
Gavin Makea2e3302023-03-11 06:46:20 +0000854 argv = list(sys.argv)
855 argv.extend(rce.extra_args)
856 try:
Gavin Makf1ddaaa2023-08-04 21:13:38 +0000857 os.execv(sys.executable, [sys.executable, __file__] + argv)
Gavin Makea2e3302023-03-11 06:46:20 +0000858 except OSError as e:
859 print("fatal: cannot restart repo after upgrade", file=sys.stderr)
860 print("fatal: %s" % e, file=sys.stderr)
861 result = 128
862
863 TerminatePager()
864 sys.exit(result)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700865
David Pursehouse819827a2020-02-12 15:20:19 +0900866
Gavin Makea2e3302023-03-11 06:46:20 +0000867if __name__ == "__main__":
868 _Main(sys.argv[1:])