David Pursehouse | 8898e2f | 2012-11-14 07:51:03 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
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 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 17 | from __future__ import print_function |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 18 | import getpass |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 19 | import imp |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 20 | import netrc |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | import optparse |
| 22 | import os |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | import sys |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 24 | import time |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 25 | |
| 26 | from pyversion import is_python3 |
| 27 | if is_python3(): |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 28 | import urllib.request |
| 29 | else: |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 30 | import urllib2 |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 31 | urllib = imp.new_module('urllib') |
| 32 | urllib.request = urllib2 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 34 | try: |
| 35 | import kerberos |
| 36 | except ImportError: |
| 37 | kerberos = None |
| 38 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 39 | from color import SetDefaultColoring |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 40 | import event_log |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 41 | from trace import SetTrace |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 42 | from git_command import git, GitCommand |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 43 | from git_config import init_ssh, close_ssh |
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 |
| 57 | from manifest_xml import GitcManifest, XmlManifest |
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 | |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 63 | if not is_python3(): |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 64 | input = raw_input |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 65 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | global_options = optparse.OptionParser( |
| 67 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" |
| 68 | ) |
| 69 | global_options.add_option('-p', '--paginate', |
| 70 | dest='pager', action='store_true', |
| 71 | help='display command output in the pager') |
| 72 | global_options.add_option('--no-pager', |
| 73 | dest='no_pager', action='store_true', |
| 74 | help='disable the pager') |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 75 | global_options.add_option('--color', |
| 76 | choices=('auto', 'always', 'never'), default=None, |
| 77 | help='control color usage: auto, always, never') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 78 | global_options.add_option('--trace', |
| 79 | dest='trace', action='store_true', |
| 80 | help='trace git command execution') |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 81 | global_options.add_option('--time', |
| 82 | dest='time', action='store_true', |
| 83 | help='time repo command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 84 | global_options.add_option('--version', |
| 85 | dest='show_version', action='store_true', |
| 86 | help='display this version of repo') |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 87 | global_options.add_option('--event-log', |
| 88 | dest='event_log', action='store', |
| 89 | help='filename of event log to append timeline to') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | |
| 91 | class _Repo(object): |
| 92 | def __init__(self, repodir): |
| 93 | self.repodir = repodir |
| 94 | self.commands = all_commands |
Mike Lockwood | 2bf9db0 | 2009-07-14 15:23:39 -0400 | [diff] [blame] | 95 | # add 'branch' as an alias for 'branches' |
| 96 | all_commands['branch'] = all_commands['branches'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | |
| 98 | def _Run(self, argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 99 | result = 0 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | name = None |
| 101 | glob = [] |
| 102 | |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 103 | for i in range(len(argv)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 104 | if not argv[i].startswith('-'): |
| 105 | name = argv[i] |
| 106 | if i > 0: |
| 107 | glob = argv[:i] |
| 108 | argv = argv[i + 1:] |
| 109 | break |
| 110 | if not name: |
| 111 | glob = argv |
| 112 | name = 'help' |
| 113 | argv = [] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 114 | gopts, _gargs = global_options.parse_args(glob) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 116 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 117 | SetTrace() |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 118 | if gopts.show_version: |
| 119 | if name == 'help': |
| 120 | name = 'version' |
| 121 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 122 | print('fatal: invalid usage of --version', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 123 | return 1 |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 124 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 125 | SetDefaultColoring(gopts.color) |
| 126 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | try: |
| 128 | cmd = self.commands[name] |
| 129 | except KeyError: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 130 | print("repo: '%s' is not a repo command. See 'repo help'." % name, |
| 131 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 132 | return 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | |
| 134 | cmd.repodir = self.repodir |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 135 | cmd.manifest = XmlManifest(cmd.repodir) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 136 | cmd.gitc_manifest = None |
| 137 | gitc_client_name = gitc_utils.parse_clientdir(os.getcwd()) |
| 138 | if gitc_client_name: |
| 139 | cmd.gitc_manifest = GitcManifest(cmd.repodir, gitc_client_name) |
| 140 | cmd.manifest.isGitcClient = True |
| 141 | |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 142 | Editor.globalConfig = cmd.manifest.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 144 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 145 | print("fatal: '%s' requires a working directory" % name, |
| 146 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 147 | return 1 |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 148 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 149 | if isinstance(cmd, GitcAvailableCommand) and not gitc_utils.get_gitc_manifest_dir(): |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 150 | print("fatal: '%s' requires GITC to be available" % name, |
| 151 | file=sys.stderr) |
| 152 | return 1 |
| 153 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 154 | if isinstance(cmd, GitcClientCommand) and not gitc_client_name: |
| 155 | print("fatal: '%s' requires a GITC client" % name, |
| 156 | file=sys.stderr) |
| 157 | return 1 |
| 158 | |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 159 | try: |
| 160 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 161 | copts = cmd.ReadEnvironmentOptions(copts) |
| 162 | except NoManifestException as e: |
| 163 | print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)), |
| 164 | file=sys.stderr) |
| 165 | print('error: manifest missing or unreadable -- please run init', |
| 166 | file=sys.stderr) |
| 167 | return 1 |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 168 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 169 | if not gopts.no_pager and not isinstance(cmd, InteractiveCommand): |
| 170 | config = cmd.manifest.globalConfig |
| 171 | if gopts.pager: |
| 172 | use_pager = True |
| 173 | else: |
| 174 | use_pager = config.GetBoolean('pager.%s' % name) |
| 175 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 176 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | if use_pager: |
| 178 | RunPager(config) |
| 179 | |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 180 | start = time.time() |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 181 | cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start) |
| 182 | cmd.event_log.SetParent(cmd_event) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 183 | try: |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 184 | result = cmd.Execute(copts, cargs) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 185 | except (DownloadError, ManifestInvalidRevisionError, |
| 186 | NoManifestException) as e: |
| 187 | print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)), |
| 188 | file=sys.stderr) |
| 189 | if isinstance(e, NoManifestException): |
| 190 | print('error: manifest missing or unreadable -- please run init', |
| 191 | file=sys.stderr) |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 192 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 193 | except NoSuchProjectError as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 194 | if e.name: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 195 | 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] | 196 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 197 | print('error: no project in current directory', file=sys.stderr) |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 198 | result = 1 |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 199 | except InvalidProjectGroupsError as e: |
| 200 | if e.name: |
| 201 | print('error: project group must be enabled for project %s' % e.name, file=sys.stderr) |
| 202 | else: |
| 203 | print('error: project group must be enabled for the project in the current directory', file=sys.stderr) |
| 204 | result = 1 |
David Riley | aa90021 | 2017-04-05 13:50:52 -0700 | [diff] [blame] | 205 | except SystemExit as e: |
| 206 | if e.code: |
| 207 | result = e.code |
| 208 | raise |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 209 | finally: |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 210 | finish = time.time() |
| 211 | elapsed = finish - start |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 212 | hours, remainder = divmod(elapsed, 3600) |
| 213 | minutes, seconds = divmod(remainder, 60) |
| 214 | if gopts.time: |
| 215 | if hours == 0: |
| 216 | print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr) |
| 217 | else: |
| 218 | print('real\t%dh%dm%.3fs' % (hours, minutes, seconds), |
| 219 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 220 | |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 221 | cmd.event_log.FinishEvent(cmd_event, finish, |
| 222 | result is None or result == 0) |
| 223 | if gopts.event_log: |
| 224 | cmd.event_log.Write(os.path.abspath( |
| 225 | os.path.expanduser(gopts.event_log))) |
| 226 | |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 227 | return result |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 228 | |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 229 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 230 | def _MyRepoPath(): |
| 231 | return os.path.dirname(__file__) |
| 232 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 233 | |
| 234 | def _CheckWrapperVersion(ver, repo_path): |
| 235 | if not repo_path: |
| 236 | repo_path = '~/bin/repo' |
| 237 | |
| 238 | if not ver: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 239 | print('no --wrapper-version argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 240 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 241 | |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 242 | exp = Wrapper().VERSION |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 243 | ver = tuple(map(int, ver.split('.'))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 244 | if len(ver) == 1: |
| 245 | ver = (0, ver[0]) |
| 246 | |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 247 | exp_str = '.'.join(map(str, exp)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 248 | if exp[0] > ver[0] or ver < (0, 4): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 249 | print(""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 250 | !!! A new repo command (%5s) is available. !!! |
| 251 | !!! You must upgrade before you can continue: !!! |
| 252 | |
| 253 | cp %s %s |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 254 | """ % (exp_str, WrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 255 | sys.exit(1) |
| 256 | |
| 257 | if exp > ver: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 258 | print(""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | ... A new repo command (%5s) is available. |
| 260 | ... You should upgrade soon: |
| 261 | |
| 262 | cp %s %s |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 263 | """ % (exp_str, WrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 264 | |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 265 | def _CheckRepoDir(repo_dir): |
| 266 | if not repo_dir: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 267 | print('no --repo-dir argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 268 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 269 | |
| 270 | def _PruneOptions(argv, opt): |
| 271 | i = 0 |
| 272 | while i < len(argv): |
| 273 | a = argv[i] |
| 274 | if a == '--': |
| 275 | break |
| 276 | if a.startswith('--'): |
| 277 | eq = a.find('=') |
| 278 | if eq > 0: |
| 279 | a = a[0:eq] |
| 280 | if not opt.has_option(a): |
| 281 | del argv[i] |
| 282 | continue |
| 283 | i += 1 |
| 284 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 285 | _user_agent = None |
| 286 | |
| 287 | def _UserAgent(): |
| 288 | global _user_agent |
| 289 | |
| 290 | if _user_agent is None: |
| 291 | py_version = sys.version_info |
| 292 | |
| 293 | os_name = sys.platform |
| 294 | if os_name == 'linux2': |
| 295 | os_name = 'Linux' |
| 296 | elif os_name == 'win32': |
| 297 | os_name = 'Win32' |
| 298 | elif os_name == 'cygwin': |
| 299 | os_name = 'Cygwin' |
| 300 | elif os_name == 'darwin': |
| 301 | os_name = 'Darwin' |
| 302 | |
| 303 | p = GitCommand( |
| 304 | None, ['describe', 'HEAD'], |
| 305 | cwd = _MyRepoPath(), |
| 306 | capture_stdout = True) |
| 307 | if p.Wait() == 0: |
| 308 | repo_version = p.stdout |
| 309 | if len(repo_version) > 0 and repo_version[-1] == '\n': |
| 310 | repo_version = repo_version[0:-1] |
| 311 | if len(repo_version) > 0 and repo_version[0] == 'v': |
| 312 | repo_version = repo_version[1:] |
| 313 | else: |
| 314 | repo_version = 'unknown' |
| 315 | |
| 316 | _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( |
| 317 | repo_version, |
| 318 | os_name, |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 319 | '.'.join(map(str, git.version_tuple())), |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 320 | py_version[0], py_version[1], py_version[2]) |
| 321 | return _user_agent |
| 322 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 323 | class _UserAgentHandler(urllib.request.BaseHandler): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 324 | def http_request(self, req): |
| 325 | req.add_header('User-Agent', _UserAgent()) |
| 326 | return req |
| 327 | |
| 328 | def https_request(self, req): |
| 329 | req.add_header('User-Agent', _UserAgent()) |
| 330 | return req |
| 331 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 332 | def _AddPasswordFromUserInput(handler, msg, req): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 333 | # If repo could not find auth info from netrc, try to get it from user input |
| 334 | url = req.get_full_url() |
| 335 | user, password = handler.passwd.find_user_password(None, url) |
| 336 | if user is None: |
| 337 | print(msg) |
| 338 | try: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 339 | user = input('User: ') |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 340 | password = getpass.getpass() |
| 341 | except KeyboardInterrupt: |
| 342 | return |
| 343 | handler.passwd.add_password(None, url, user, password) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 344 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 345 | class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 346 | def http_error_401(self, req, fp, code, msg, headers): |
| 347 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 348 | return urllib.request.HTTPBasicAuthHandler.http_error_401( |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 349 | self, req, fp, code, msg, headers) |
| 350 | |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 351 | def http_error_auth_reqed(self, authreq, host, req, headers): |
| 352 | try: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 353 | old_add_header = req.add_header |
| 354 | def _add_header(name, val): |
| 355 | val = val.replace('\n', '') |
| 356 | old_add_header(name, val) |
| 357 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 358 | return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed( |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 359 | self, authreq, host, req, headers) |
| 360 | except: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 361 | reset = getattr(self, 'reset_retry_count', None) |
| 362 | if reset is not None: |
| 363 | reset() |
Shawn O. Pearce | b660539 | 2011-10-11 15:58:07 -0700 | [diff] [blame] | 364 | elif getattr(self, 'retried', None): |
| 365 | self.retried = 0 |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 366 | raise |
| 367 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 368 | class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 369 | def http_error_401(self, req, fp, code, msg, headers): |
| 370 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 371 | return urllib.request.HTTPDigestAuthHandler.http_error_401( |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 372 | self, req, fp, code, msg, headers) |
| 373 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 374 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 375 | try: |
| 376 | old_add_header = req.add_header |
| 377 | def _add_header(name, val): |
| 378 | val = val.replace('\n', '') |
| 379 | old_add_header(name, val) |
| 380 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 381 | return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed( |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 382 | self, auth_header, host, req, headers) |
| 383 | except: |
| 384 | reset = getattr(self, 'reset_retry_count', None) |
| 385 | if reset is not None: |
| 386 | reset() |
| 387 | elif getattr(self, 'retried', None): |
| 388 | self.retried = 0 |
| 389 | raise |
| 390 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 391 | class _KerberosAuthHandler(urllib.request.BaseHandler): |
| 392 | def __init__(self): |
| 393 | self.retried = 0 |
| 394 | self.context = None |
| 395 | self.handler_order = urllib.request.BaseHandler.handler_order - 50 |
| 396 | |
David Pursehouse | 65b0ba5 | 2018-06-24 16:21:51 +0900 | [diff] [blame^] | 397 | def http_error_401(self, req, fp, code, msg, headers): |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 398 | host = req.get_host() |
| 399 | retry = self.http_error_auth_reqed('www-authenticate', host, req, headers) |
| 400 | return retry |
| 401 | |
| 402 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 403 | try: |
| 404 | spn = "HTTP@%s" % host |
| 405 | authdata = self._negotiate_get_authdata(auth_header, headers) |
| 406 | |
| 407 | if self.retried > 3: |
| 408 | raise urllib.request.HTTPError(req.get_full_url(), 401, |
| 409 | "Negotiate auth failed", headers, None) |
| 410 | else: |
| 411 | self.retried += 1 |
| 412 | |
| 413 | neghdr = self._negotiate_get_svctk(spn, authdata) |
| 414 | if neghdr is None: |
| 415 | return None |
| 416 | |
| 417 | req.add_unredirected_header('Authorization', neghdr) |
| 418 | response = self.parent.open(req) |
| 419 | |
| 420 | srvauth = self._negotiate_get_authdata(auth_header, response.info()) |
| 421 | if self._validate_response(srvauth): |
| 422 | return response |
| 423 | except kerberos.GSSError: |
| 424 | return None |
| 425 | except: |
| 426 | self.reset_retry_count() |
| 427 | raise |
| 428 | finally: |
| 429 | self._clean_context() |
| 430 | |
| 431 | def reset_retry_count(self): |
| 432 | self.retried = 0 |
| 433 | |
| 434 | def _negotiate_get_authdata(self, auth_header, headers): |
| 435 | authhdr = headers.get(auth_header, None) |
| 436 | if authhdr is not None: |
| 437 | for mech_tuple in authhdr.split(","): |
| 438 | mech, __, authdata = mech_tuple.strip().partition(" ") |
| 439 | if mech.lower() == "negotiate": |
| 440 | return authdata.strip() |
| 441 | return None |
| 442 | |
| 443 | def _negotiate_get_svctk(self, spn, authdata): |
| 444 | if authdata is None: |
| 445 | return None |
| 446 | |
| 447 | result, self.context = kerberos.authGSSClientInit(spn) |
| 448 | if result < kerberos.AUTH_GSS_COMPLETE: |
| 449 | return None |
| 450 | |
| 451 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 452 | if result < kerberos.AUTH_GSS_CONTINUE: |
| 453 | return None |
| 454 | |
| 455 | response = kerberos.authGSSClientResponse(self.context) |
| 456 | return "Negotiate %s" % response |
| 457 | |
| 458 | def _validate_response(self, authdata): |
| 459 | if authdata is None: |
| 460 | return None |
| 461 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 462 | if result == kerberos.AUTH_GSS_COMPLETE: |
| 463 | return True |
| 464 | return None |
| 465 | |
| 466 | def _clean_context(self): |
| 467 | if self.context is not None: |
| 468 | kerberos.authGSSClientClean(self.context) |
| 469 | self.context = None |
| 470 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 471 | def init_http(): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 472 | handlers = [_UserAgentHandler()] |
| 473 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 474 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 475 | try: |
| 476 | n = netrc.netrc() |
| 477 | for host in n.hosts: |
| 478 | p = n.hosts[host] |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 479 | mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) |
| 480 | 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] | 481 | except netrc.NetrcParseError: |
| 482 | pass |
Shawn O. Pearce | 7b947de | 2011-09-23 11:50:31 -0700 | [diff] [blame] | 483 | except IOError: |
| 484 | pass |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 485 | handlers.append(_BasicAuthHandler(mgr)) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 486 | handlers.append(_DigestAuthHandler(mgr)) |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 487 | if kerberos: |
| 488 | handlers.append(_KerberosAuthHandler()) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 489 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 490 | if 'http_proxy' in os.environ: |
| 491 | url = os.environ['http_proxy'] |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 492 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 493 | if 'REPO_CURL_VERBOSE' in os.environ: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 494 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 495 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 496 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 497 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 498 | def _Main(argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 499 | result = 0 |
| 500 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 501 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 502 | opt.add_option("--repo-dir", dest="repodir", |
| 503 | help="path to .repo/") |
| 504 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 505 | help="version of the wrapper script") |
| 506 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 507 | help="location of the wrapper script") |
| 508 | _PruneOptions(argv, opt) |
| 509 | opt, argv = opt.parse_args(argv) |
| 510 | |
| 511 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 512 | _CheckRepoDir(opt.repodir) |
| 513 | |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 514 | Version.wrapper_version = opt.wrapper_version |
| 515 | Version.wrapper_path = opt.wrapper_path |
| 516 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 517 | repo = _Repo(opt.repodir) |
| 518 | try: |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 519 | try: |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 520 | init_ssh() |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 521 | init_http() |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 522 | result = repo._Run(argv) or 0 |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 523 | finally: |
| 524 | close_ssh() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 525 | except KeyboardInterrupt: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 526 | print('aborted by user', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 527 | result = 1 |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 528 | except ManifestParseError as mpe: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 529 | print('fatal: %s' % mpe, file=sys.stderr) |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 530 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 531 | except RepoChangedException as rce: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 532 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 533 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 534 | argv = list(sys.argv) |
| 535 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 536 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 537 | os.execv(__file__, argv) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 538 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 539 | print('fatal: cannot restart repo after upgrade', file=sys.stderr) |
| 540 | print('fatal: %s' % e, file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 541 | result = 128 |
| 542 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 543 | TerminatePager() |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 544 | sys.exit(result) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 545 | |
| 546 | if __name__ == '__main__': |
| 547 | _Main(sys.argv[1:]) |