Mike Frysinger | a488af5 | 2020-09-06 13:33:45 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2008 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Mike Frysinger | 87fb5a1 | 2019-06-13 01:54:46 -0400 | [diff] [blame] | 17 | """The repo tool. |
| 18 | |
| 19 | People shouldn't run this directly; instead, they should use the `repo` wrapper |
| 20 | which takes care of execing this entry point. |
| 21 | """ |
| 22 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 23 | import getpass |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 24 | import netrc |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | import optparse |
| 26 | import os |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 27 | import shlex |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | import sys |
Mike Frysinger | 7c321f1 | 2019-12-02 16:49:44 -0500 | [diff] [blame] | 29 | import textwrap |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 30 | import time |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 31 | import urllib.request |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 33 | try: |
| 34 | import kerberos |
| 35 | except ImportError: |
| 36 | kerberos = None |
| 37 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 38 | from color import SetDefaultColoring |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 39 | import event_log |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 40 | from repo_trace import SetTrace |
David Pursehouse | 9090e80 | 2020-02-12 11:25:13 +0900 | [diff] [blame] | 41 | from git_command import user_agent |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 42 | from git_config import init_ssh, close_ssh, RepoConfig |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 43 | from git_trace2_event_log import EventLog |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 44 | from command import InteractiveCommand |
| 45 | from command import MirrorSafeCommand |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 46 | from command import GitcAvailableCommand, GitcClientCommand |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 47 | from subcmds.version import Version |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 48 | from editor import Editor |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 49 | from error import DownloadError |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 50 | from error import InvalidProjectGroupsError |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 51 | from error import ManifestInvalidRevisionError |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 52 | from error import ManifestParseError |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 53 | from error import NoManifestException |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | from error import NoSuchProjectError |
| 55 | from error import RepoChangedException |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 56 | import gitc_utils |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 57 | from manifest_xml import GitcClient, RepoClient |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 58 | from pager import RunPager, TerminatePager |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 59 | from wrapper import WrapperPath, Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 61 | from subcmds import all_commands |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 63 | |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 64 | # NB: These do not need to be kept in sync with the repo launcher script. |
| 65 | # These may be much newer as it allows the repo launcher to roll between |
| 66 | # different repo releases while source versions might require a newer python. |
| 67 | # |
| 68 | # The soft version is when we start warning users that the version is old and |
| 69 | # we'll be dropping support for it. We'll refuse to work with versions older |
| 70 | # than the hard version. |
| 71 | # |
| 72 | # python-3.6 is in Ubuntu Bionic. |
| 73 | MIN_PYTHON_VERSION_SOFT = (3, 6) |
Mike Frysinger | 128f34e | 2020-12-14 18:28:04 -0500 | [diff] [blame] | 74 | MIN_PYTHON_VERSION_HARD = (3, 5) |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 75 | |
| 76 | if sys.version_info.major < 3: |
Mike Frysinger | a488af5 | 2020-09-06 13:33:45 -0400 | [diff] [blame] | 77 | print('repo: error: Python 2 is no longer supported; ' |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 78 | 'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT), |
| 79 | file=sys.stderr) |
Mike Frysinger | a488af5 | 2020-09-06 13:33:45 -0400 | [diff] [blame] | 80 | sys.exit(1) |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 81 | else: |
| 82 | if sys.version_info < MIN_PYTHON_VERSION_HARD: |
| 83 | print('repo: error: Python 3 version is too old; ' |
| 84 | 'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT), |
| 85 | file=sys.stderr) |
| 86 | sys.exit(1) |
| 87 | elif sys.version_info < MIN_PYTHON_VERSION_SOFT: |
| 88 | print('repo: warning: your Python 3 version is no longer supported; ' |
| 89 | 'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT), |
| 90 | file=sys.stderr) |
| 91 | |
| 92 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | global_options = optparse.OptionParser( |
Mike Frysinger | 7c321f1 | 2019-12-02 16:49:44 -0500 | [diff] [blame] | 94 | usage='repo [-p|--paginate|--no-pager] COMMAND [ARGS]', |
| 95 | add_help_option=False) |
| 96 | global_options.add_option('-h', '--help', action='store_true', |
| 97 | help='show this help message and exit') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | global_options.add_option('-p', '--paginate', |
| 99 | dest='pager', action='store_true', |
| 100 | help='display command output in the pager') |
| 101 | global_options.add_option('--no-pager', |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 102 | dest='pager', action='store_false', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | help='disable the pager') |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 104 | global_options.add_option('--color', |
| 105 | choices=('auto', 'always', 'never'), default=None, |
| 106 | help='control color usage: auto, always, never') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 107 | global_options.add_option('--trace', |
| 108 | dest='trace', action='store_true', |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 109 | help='trace git command execution (REPO_TRACE=1)') |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 110 | global_options.add_option('--trace-python', |
| 111 | dest='trace_python', action='store_true', |
| 112 | help='trace python command execution') |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 113 | global_options.add_option('--time', |
| 114 | dest='time', action='store_true', |
| 115 | help='time repo command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 116 | global_options.add_option('--version', |
| 117 | dest='show_version', action='store_true', |
| 118 | help='display this version of repo') |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 119 | global_options.add_option('--event-log', |
| 120 | dest='event_log', action='store', |
| 121 | help='filename of event log to append timeline to') |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 122 | global_options.add_option('--git-trace2-event-log', action='store', |
| 123 | help='directory to write git trace2 event log to') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 125 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 126 | class _Repo(object): |
| 127 | def __init__(self, repodir): |
| 128 | self.repodir = repodir |
| 129 | self.commands = all_commands |
| 130 | |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 131 | def _ParseArgs(self, argv): |
| 132 | """Parse the main `repo` command line options.""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | name = None |
| 134 | glob = [] |
| 135 | |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 136 | for i in range(len(argv)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 137 | if not argv[i].startswith('-'): |
| 138 | name = argv[i] |
| 139 | if i > 0: |
| 140 | glob = argv[:i] |
| 141 | argv = argv[i + 1:] |
| 142 | break |
| 143 | if not name: |
| 144 | glob = argv |
| 145 | name = 'help' |
| 146 | argv = [] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 147 | gopts, _gargs = global_options.parse_args(glob) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 148 | |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 149 | name, alias_args = self._ExpandAlias(name) |
| 150 | argv = alias_args + argv |
| 151 | |
Mike Frysinger | 7c321f1 | 2019-12-02 16:49:44 -0500 | [diff] [blame] | 152 | if gopts.help: |
| 153 | global_options.print_help() |
| 154 | commands = ' '.join(sorted(self.commands)) |
| 155 | wrapped_commands = textwrap.wrap(commands, width=77) |
| 156 | print('\nAvailable commands:\n %s' % ('\n '.join(wrapped_commands),)) |
| 157 | print('\nRun `repo help <command>` for command-specific details.') |
| 158 | global_options.exit() |
| 159 | |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 160 | return (name, gopts, argv) |
| 161 | |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 162 | def _ExpandAlias(self, name): |
| 163 | """Look up user registered aliases.""" |
| 164 | # We don't resolve aliases for existing subcommands. This matches git. |
| 165 | if name in self.commands: |
| 166 | return name, [] |
| 167 | |
| 168 | key = 'alias.%s' % (name,) |
| 169 | alias = RepoConfig.ForRepository(self.repodir).GetString(key) |
| 170 | if alias is None: |
| 171 | alias = RepoConfig.ForUser().GetString(key) |
| 172 | if alias is None: |
| 173 | return name, [] |
| 174 | |
| 175 | args = alias.strip().split(' ', 1) |
| 176 | name = args[0] |
| 177 | if len(args) == 2: |
| 178 | args = shlex.split(args[1]) |
| 179 | else: |
| 180 | args = [] |
| 181 | return name, args |
| 182 | |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 183 | def _Run(self, name, gopts, argv): |
| 184 | """Execute the requested subcommand.""" |
| 185 | result = 0 |
| 186 | |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 187 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 188 | SetTrace() |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 189 | if gopts.show_version: |
| 190 | if name == 'help': |
| 191 | name = 'version' |
| 192 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 193 | print('fatal: invalid usage of --version', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 194 | return 1 |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 195 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 196 | SetDefaultColoring(gopts.color) |
| 197 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 198 | try: |
Mike Frysinger | bb93046 | 2020-02-25 15:18:31 -0500 | [diff] [blame] | 199 | cmd = self.commands[name]() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 200 | except KeyError: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 201 | print("repo: '%s' is not a repo command. See 'repo help'." % name, |
| 202 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 203 | return 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 205 | git_trace2_event_log = EventLog() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 206 | cmd.repodir = self.repodir |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 207 | cmd.client = RepoClient(cmd.repodir) |
| 208 | cmd.manifest = cmd.client.manifest |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 209 | cmd.gitc_manifest = None |
| 210 | gitc_client_name = gitc_utils.parse_clientdir(os.getcwd()) |
| 211 | if gitc_client_name: |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 212 | cmd.gitc_manifest = GitcClient(cmd.repodir, gitc_client_name) |
| 213 | cmd.client.isGitcClient = True |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 214 | |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 215 | Editor.globalConfig = cmd.client.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 216 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 217 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 218 | print("fatal: '%s' requires a working directory" % name, |
| 219 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 220 | return 1 |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 221 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 222 | if isinstance(cmd, GitcAvailableCommand) and not gitc_utils.get_gitc_manifest_dir(): |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 223 | print("fatal: '%s' requires GITC to be available" % name, |
| 224 | file=sys.stderr) |
| 225 | return 1 |
| 226 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 227 | if isinstance(cmd, GitcClientCommand) and not gitc_client_name: |
| 228 | print("fatal: '%s' requires a GITC client" % name, |
| 229 | file=sys.stderr) |
| 230 | return 1 |
| 231 | |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 232 | try: |
| 233 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 234 | copts = cmd.ReadEnvironmentOptions(copts) |
| 235 | except NoManifestException as e: |
| 236 | print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)), |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 237 | file=sys.stderr) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 238 | print('error: manifest missing or unreadable -- please run init', |
| 239 | file=sys.stderr) |
| 240 | return 1 |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 241 | |
Mike Frysinger | 8a98efe | 2020-02-19 01:17:56 -0500 | [diff] [blame] | 242 | if gopts.pager is not False and not isinstance(cmd, InteractiveCommand): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 243 | config = cmd.client.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 244 | if gopts.pager: |
| 245 | use_pager = True |
| 246 | else: |
| 247 | use_pager = config.GetBoolean('pager.%s' % name) |
| 248 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 249 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 250 | if use_pager: |
| 251 | RunPager(config) |
| 252 | |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 253 | start = time.time() |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 254 | cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start) |
| 255 | cmd.event_log.SetParent(cmd_event) |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 256 | git_trace2_event_log.StartEvent() |
| 257 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 258 | try: |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 259 | cmd.ValidateOptions(copts, cargs) |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 260 | result = cmd.Execute(copts, cargs) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 261 | except (DownloadError, ManifestInvalidRevisionError, |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 262 | NoManifestException) as e: |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 263 | print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)), |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 264 | file=sys.stderr) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 265 | if isinstance(e, NoManifestException): |
| 266 | print('error: manifest missing or unreadable -- please run init', |
| 267 | file=sys.stderr) |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 268 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 269 | except NoSuchProjectError as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 270 | if e.name: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 271 | print('error: project %s not found' % e.name, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 272 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 273 | print('error: no project in current directory', file=sys.stderr) |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 274 | result = 1 |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 275 | except InvalidProjectGroupsError as e: |
| 276 | if e.name: |
| 277 | print('error: project group must be enabled for project %s' % e.name, file=sys.stderr) |
| 278 | else: |
David Pursehouse | 3cda50a | 2020-02-13 13:17:03 +0900 | [diff] [blame] | 279 | print('error: project group must be enabled for the project in the current directory', |
| 280 | file=sys.stderr) |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 281 | result = 1 |
David Riley | aa90021 | 2017-04-05 13:50:52 -0700 | [diff] [blame] | 282 | except SystemExit as e: |
| 283 | if e.code: |
| 284 | result = e.code |
| 285 | raise |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 286 | finally: |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 287 | finish = time.time() |
| 288 | elapsed = finish - start |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 289 | hours, remainder = divmod(elapsed, 3600) |
| 290 | minutes, seconds = divmod(remainder, 60) |
| 291 | if gopts.time: |
| 292 | if hours == 0: |
| 293 | print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr) |
| 294 | else: |
| 295 | print('real\t%dh%dm%.3fs' % (hours, minutes, seconds), |
| 296 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 297 | |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 298 | cmd.event_log.FinishEvent(cmd_event, finish, |
| 299 | result is None or result == 0) |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 300 | git_trace2_event_log.ExitEvent(result) |
| 301 | |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 302 | if gopts.event_log: |
| 303 | cmd.event_log.Write(os.path.abspath( |
| 304 | os.path.expanduser(gopts.event_log))) |
| 305 | |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 306 | git_trace2_event_log.Write(gopts.git_trace2_event_log) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 307 | return result |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 308 | |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 309 | |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 310 | def _CheckWrapperVersion(ver_str, repo_path): |
| 311 | """Verify the repo launcher is new enough for this checkout. |
| 312 | |
| 313 | Args: |
| 314 | ver_str: The version string passed from the repo launcher when it ran us. |
| 315 | repo_path: The path to the repo launcher that loaded us. |
| 316 | """ |
| 317 | # Refuse to work with really old wrapper versions. We don't test these, |
| 318 | # so might as well require a somewhat recent sane version. |
| 319 | # v1.15 of the repo launcher was released in ~Mar 2012. |
| 320 | MIN_REPO_VERSION = (1, 15) |
| 321 | min_str = '.'.join(str(x) for x in MIN_REPO_VERSION) |
| 322 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 323 | if not repo_path: |
| 324 | repo_path = '~/bin/repo' |
| 325 | |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 326 | if not ver_str: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 327 | print('no --wrapper-version argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 328 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 329 | |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 330 | # Pull out the version of the repo launcher we know about to compare. |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 331 | exp = Wrapper().VERSION |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 332 | ver = tuple(map(int, ver_str.split('.'))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 333 | |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 334 | exp_str = '.'.join(map(str, exp)) |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 335 | if ver < MIN_REPO_VERSION: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 336 | print(""" |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 337 | repo: error: |
| 338 | !!! Your version of repo %s is too old. |
| 339 | !!! We need at least version %s. |
David Pursehouse | 7838e38 | 2020-02-13 09:54:49 +0900 | [diff] [blame] | 340 | !!! A new version of repo (%s) is available. |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 341 | !!! You must upgrade before you can continue: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 342 | |
| 343 | cp %s %s |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 344 | """ % (ver_str, min_str, exp_str, WrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 345 | sys.exit(1) |
| 346 | |
| 347 | if exp > ver: |
Mike Frysinger | eea23b4 | 2020-02-26 16:21:08 -0500 | [diff] [blame] | 348 | print('\n... A new version of repo (%s) is available.' % (exp_str,), |
| 349 | file=sys.stderr) |
| 350 | if os.access(repo_path, os.W_OK): |
| 351 | print("""\ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 352 | ... You should upgrade soon: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 353 | cp %s %s |
Mike Frysinger | eea23b4 | 2020-02-26 16:21:08 -0500 | [diff] [blame] | 354 | """ % (WrapperPath(), repo_path), file=sys.stderr) |
| 355 | else: |
| 356 | print("""\ |
| 357 | ... New version is available at: %s |
| 358 | ... The launcher is run from: %s |
| 359 | !!! The launcher is not writable. Please talk to your sysadmin or distro |
| 360 | !!! to get an update installed. |
| 361 | """ % (WrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 362 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 363 | |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 364 | def _CheckRepoDir(repo_dir): |
| 365 | if not repo_dir: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 366 | print('no --repo-dir argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 367 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 368 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 369 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 370 | def _PruneOptions(argv, opt): |
| 371 | i = 0 |
| 372 | while i < len(argv): |
| 373 | a = argv[i] |
| 374 | if a == '--': |
| 375 | break |
| 376 | if a.startswith('--'): |
| 377 | eq = a.find('=') |
| 378 | if eq > 0: |
| 379 | a = a[0:eq] |
| 380 | if not opt.has_option(a): |
| 381 | del argv[i] |
| 382 | continue |
| 383 | i += 1 |
| 384 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 385 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 386 | class _UserAgentHandler(urllib.request.BaseHandler): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 387 | def http_request(self, req): |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 388 | req.add_header('User-Agent', user_agent.repo) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 389 | return req |
| 390 | |
| 391 | def https_request(self, req): |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 392 | req.add_header('User-Agent', user_agent.repo) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 393 | return req |
| 394 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 395 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 396 | def _AddPasswordFromUserInput(handler, msg, req): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 397 | # If repo could not find auth info from netrc, try to get it from user input |
| 398 | url = req.get_full_url() |
| 399 | user, password = handler.passwd.find_user_password(None, url) |
| 400 | if user is None: |
| 401 | print(msg) |
| 402 | try: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 403 | user = input('User: ') |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 404 | password = getpass.getpass() |
| 405 | except KeyboardInterrupt: |
| 406 | return |
| 407 | handler.passwd.add_password(None, url, user, password) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 408 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 409 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 410 | class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 411 | def http_error_401(self, req, fp, code, msg, headers): |
| 412 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 413 | return urllib.request.HTTPBasicAuthHandler.http_error_401( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 414 | self, req, fp, code, msg, headers) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 415 | |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 416 | def http_error_auth_reqed(self, authreq, host, req, headers): |
| 417 | try: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 418 | old_add_header = req.add_header |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 419 | |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 420 | def _add_header(name, val): |
| 421 | val = val.replace('\n', '') |
| 422 | old_add_header(name, val) |
| 423 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 424 | return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 425 | self, authreq, host, req, headers) |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 426 | except Exception: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 427 | reset = getattr(self, 'reset_retry_count', None) |
| 428 | if reset is not None: |
| 429 | reset() |
Shawn O. Pearce | b660539 | 2011-10-11 15:58:07 -0700 | [diff] [blame] | 430 | elif getattr(self, 'retried', None): |
| 431 | self.retried = 0 |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 432 | raise |
| 433 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 434 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 435 | class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 436 | def http_error_401(self, req, fp, code, msg, headers): |
| 437 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 438 | return urllib.request.HTTPDigestAuthHandler.http_error_401( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 439 | self, req, fp, code, msg, headers) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 440 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 441 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 442 | try: |
| 443 | old_add_header = req.add_header |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 444 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 445 | def _add_header(name, val): |
| 446 | val = val.replace('\n', '') |
| 447 | old_add_header(name, val) |
| 448 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 449 | return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 450 | self, auth_header, host, req, headers) |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 451 | except Exception: |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 452 | reset = getattr(self, 'reset_retry_count', None) |
| 453 | if reset is not None: |
| 454 | reset() |
| 455 | elif getattr(self, 'retried', None): |
| 456 | self.retried = 0 |
| 457 | raise |
| 458 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 459 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 460 | class _KerberosAuthHandler(urllib.request.BaseHandler): |
| 461 | def __init__(self): |
| 462 | self.retried = 0 |
| 463 | self.context = None |
| 464 | self.handler_order = urllib.request.BaseHandler.handler_order - 50 |
| 465 | |
David Pursehouse | 65b0ba5 | 2018-06-24 16:21:51 +0900 | [diff] [blame] | 466 | def http_error_401(self, req, fp, code, msg, headers): |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 467 | host = req.get_host() |
| 468 | retry = self.http_error_auth_reqed('www-authenticate', host, req, headers) |
| 469 | return retry |
| 470 | |
| 471 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 472 | try: |
| 473 | spn = "HTTP@%s" % host |
| 474 | authdata = self._negotiate_get_authdata(auth_header, headers) |
| 475 | |
| 476 | if self.retried > 3: |
| 477 | raise urllib.request.HTTPError(req.get_full_url(), 401, |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 478 | "Negotiate auth failed", headers, None) |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 479 | else: |
| 480 | self.retried += 1 |
| 481 | |
| 482 | neghdr = self._negotiate_get_svctk(spn, authdata) |
| 483 | if neghdr is None: |
| 484 | return None |
| 485 | |
| 486 | req.add_unredirected_header('Authorization', neghdr) |
| 487 | response = self.parent.open(req) |
| 488 | |
| 489 | srvauth = self._negotiate_get_authdata(auth_header, response.info()) |
| 490 | if self._validate_response(srvauth): |
| 491 | return response |
| 492 | except kerberos.GSSError: |
| 493 | return None |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 494 | except Exception: |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 495 | self.reset_retry_count() |
| 496 | raise |
| 497 | finally: |
| 498 | self._clean_context() |
| 499 | |
| 500 | def reset_retry_count(self): |
| 501 | self.retried = 0 |
| 502 | |
| 503 | def _negotiate_get_authdata(self, auth_header, headers): |
| 504 | authhdr = headers.get(auth_header, None) |
| 505 | if authhdr is not None: |
| 506 | for mech_tuple in authhdr.split(","): |
| 507 | mech, __, authdata = mech_tuple.strip().partition(" ") |
| 508 | if mech.lower() == "negotiate": |
| 509 | return authdata.strip() |
| 510 | return None |
| 511 | |
| 512 | def _negotiate_get_svctk(self, spn, authdata): |
| 513 | if authdata is None: |
| 514 | return None |
| 515 | |
| 516 | result, self.context = kerberos.authGSSClientInit(spn) |
| 517 | if result < kerberos.AUTH_GSS_COMPLETE: |
| 518 | return None |
| 519 | |
| 520 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 521 | if result < kerberos.AUTH_GSS_CONTINUE: |
| 522 | return None |
| 523 | |
| 524 | response = kerberos.authGSSClientResponse(self.context) |
| 525 | return "Negotiate %s" % response |
| 526 | |
| 527 | def _validate_response(self, authdata): |
| 528 | if authdata is None: |
| 529 | return None |
| 530 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 531 | if result == kerberos.AUTH_GSS_COMPLETE: |
| 532 | return True |
| 533 | return None |
| 534 | |
| 535 | def _clean_context(self): |
| 536 | if self.context is not None: |
| 537 | kerberos.authGSSClientClean(self.context) |
| 538 | self.context = None |
| 539 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 540 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 541 | def init_http(): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 542 | handlers = [_UserAgentHandler()] |
| 543 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 544 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 545 | try: |
| 546 | n = netrc.netrc() |
| 547 | for host in n.hosts: |
| 548 | p = n.hosts[host] |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 549 | mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 550 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 551 | except netrc.NetrcParseError: |
| 552 | pass |
Shawn O. Pearce | 7b947de | 2011-09-23 11:50:31 -0700 | [diff] [blame] | 553 | except IOError: |
| 554 | pass |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 555 | handlers.append(_BasicAuthHandler(mgr)) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 556 | handlers.append(_DigestAuthHandler(mgr)) |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 557 | if kerberos: |
| 558 | handlers.append(_KerberosAuthHandler()) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 559 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 560 | if 'http_proxy' in os.environ: |
| 561 | url = os.environ['http_proxy'] |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 562 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 563 | if 'REPO_CURL_VERBOSE' in os.environ: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 564 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 565 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 566 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 567 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 568 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 569 | def _Main(argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 570 | result = 0 |
| 571 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 572 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 573 | opt.add_option("--repo-dir", dest="repodir", |
| 574 | help="path to .repo/") |
| 575 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 576 | help="version of the wrapper script") |
| 577 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 578 | help="location of the wrapper script") |
| 579 | _PruneOptions(argv, opt) |
| 580 | opt, argv = opt.parse_args(argv) |
| 581 | |
| 582 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 583 | _CheckRepoDir(opt.repodir) |
| 584 | |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 585 | Version.wrapper_version = opt.wrapper_version |
| 586 | Version.wrapper_path = opt.wrapper_path |
| 587 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 588 | repo = _Repo(opt.repodir) |
| 589 | try: |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 590 | try: |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 591 | init_ssh() |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 592 | init_http() |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 593 | name, gopts, argv = repo._ParseArgs(argv) |
| 594 | run = lambda: repo._Run(name, gopts, argv) or 0 |
| 595 | if gopts.trace_python: |
| 596 | import trace |
| 597 | tracer = trace.Trace(count=False, trace=True, timing=True, |
| 598 | ignoredirs=set(sys.path[1:])) |
| 599 | result = tracer.runfunc(run) |
| 600 | else: |
| 601 | result = run() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 602 | finally: |
| 603 | close_ssh() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 604 | except KeyboardInterrupt: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 605 | print('aborted by user', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 606 | result = 1 |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 607 | except ManifestParseError as mpe: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 608 | print('fatal: %s' % mpe, file=sys.stderr) |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 609 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 610 | except RepoChangedException as rce: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 611 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 612 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 613 | argv = list(sys.argv) |
| 614 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 615 | try: |
Mike Frysinger | dd37fb2 | 2020-04-16 12:38:04 -0400 | [diff] [blame] | 616 | os.execv(sys.executable, [__file__] + argv) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 617 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 618 | print('fatal: cannot restart repo after upgrade', file=sys.stderr) |
| 619 | print('fatal: %s' % e, file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 620 | result = 128 |
| 621 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 622 | TerminatePager() |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 623 | sys.exit(result) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 624 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 625 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 626 | if __name__ == '__main__': |
| 627 | _Main(sys.argv[1:]) |