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