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 |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 25 | try: |
| 26 | import urllib2 |
| 27 | except ImportError: |
| 28 | # For python3 |
| 29 | import urllib.request |
| 30 | else: |
| 31 | # For python2 |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 32 | urllib = imp.new_module('urllib') |
| 33 | urllib.request = urllib2 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 35 | from trace import SetTrace |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 36 | from git_command import git, GitCommand |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 37 | from git_config import init_ssh, close_ssh |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 38 | from command import InteractiveCommand |
| 39 | from command import MirrorSafeCommand |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 40 | from subcmds.version import Version |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 41 | from editor import Editor |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 42 | from error import DownloadError |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 43 | from error import ManifestInvalidRevisionError |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 44 | from error import ManifestParseError |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 45 | from error import NoSuchProjectError |
| 46 | from error import RepoChangedException |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 47 | from manifest_xml import XmlManifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | from pager import RunPager |
| 49 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 50 | from subcmds import all_commands |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | |
| 52 | global_options = optparse.OptionParser( |
| 53 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" |
| 54 | ) |
| 55 | global_options.add_option('-p', '--paginate', |
| 56 | dest='pager', action='store_true', |
| 57 | help='display command output in the pager') |
| 58 | global_options.add_option('--no-pager', |
| 59 | dest='no_pager', action='store_true', |
| 60 | help='disable the pager') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 61 | global_options.add_option('--trace', |
| 62 | dest='trace', action='store_true', |
| 63 | help='trace git command execution') |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 64 | global_options.add_option('--time', |
| 65 | dest='time', action='store_true', |
| 66 | help='time repo command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 67 | global_options.add_option('--version', |
| 68 | dest='show_version', action='store_true', |
| 69 | help='display this version of repo') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | |
| 71 | class _Repo(object): |
| 72 | def __init__(self, repodir): |
| 73 | self.repodir = repodir |
| 74 | self.commands = all_commands |
Mike Lockwood | 2bf9db0 | 2009-07-14 15:23:39 -0400 | [diff] [blame] | 75 | # add 'branch' as an alias for 'branches' |
| 76 | all_commands['branch'] = all_commands['branches'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 77 | |
| 78 | def _Run(self, argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 79 | result = 0 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | name = None |
| 81 | glob = [] |
| 82 | |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 83 | for i in range(len(argv)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 84 | if not argv[i].startswith('-'): |
| 85 | name = argv[i] |
| 86 | if i > 0: |
| 87 | glob = argv[:i] |
| 88 | argv = argv[i + 1:] |
| 89 | break |
| 90 | if not name: |
| 91 | glob = argv |
| 92 | name = 'help' |
| 93 | argv = [] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 94 | gopts, _gargs = global_options.parse_args(glob) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 96 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 97 | SetTrace() |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 98 | if gopts.show_version: |
| 99 | if name == 'help': |
| 100 | name = 'version' |
| 101 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 102 | print('fatal: invalid usage of --version', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 103 | return 1 |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 104 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | try: |
| 106 | cmd = self.commands[name] |
| 107 | except KeyError: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 108 | print("repo: '%s' is not a repo command. See 'repo help'." % name, |
| 109 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 110 | return 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | |
| 112 | cmd.repodir = self.repodir |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 113 | cmd.manifest = XmlManifest(cmd.repodir) |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 114 | Editor.globalConfig = cmd.manifest.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 116 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 117 | print("fatal: '%s' requires a working directory" % name, |
| 118 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 119 | return 1 |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 120 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 121 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 122 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 123 | if not gopts.no_pager and not isinstance(cmd, InteractiveCommand): |
| 124 | config = cmd.manifest.globalConfig |
| 125 | if gopts.pager: |
| 126 | use_pager = True |
| 127 | else: |
| 128 | use_pager = config.GetBoolean('pager.%s' % name) |
| 129 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 130 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 131 | if use_pager: |
| 132 | RunPager(config) |
| 133 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 134 | try: |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 135 | start = time.time() |
| 136 | try: |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 137 | result = cmd.Execute(copts, cargs) |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 138 | finally: |
| 139 | elapsed = time.time() - start |
| 140 | hours, remainder = divmod(elapsed, 3600) |
| 141 | minutes, seconds = divmod(remainder, 60) |
| 142 | if gopts.time: |
| 143 | if hours == 0: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 144 | print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr) |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 145 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 146 | print('real\t%dh%dm%.3fs' % (hours, minutes, seconds), |
| 147 | file=sys.stderr) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 148 | except DownloadError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 149 | print('error: %s' % str(e), file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 150 | return 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 151 | except ManifestInvalidRevisionError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 152 | print('error: %s' % str(e), file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 153 | return 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 154 | except NoSuchProjectError as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | if e.name: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 156 | 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] | 157 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 158 | print('error: no project in current directory', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 159 | return 1 |
| 160 | |
| 161 | return result |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 163 | def _MyRepoPath(): |
| 164 | return os.path.dirname(__file__) |
| 165 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 166 | def _MyWrapperPath(): |
| 167 | return os.path.join(os.path.dirname(__file__), 'repo') |
| 168 | |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 169 | _wrapper_module = None |
| 170 | def WrapperModule(): |
| 171 | global _wrapper_module |
| 172 | if not _wrapper_module: |
| 173 | _wrapper_module = imp.load_source('wrapper', _MyWrapperPath()) |
| 174 | return _wrapper_module |
| 175 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 176 | def _CurrentWrapperVersion(): |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 177 | return WrapperModule().VERSION |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 178 | |
| 179 | def _CheckWrapperVersion(ver, repo_path): |
| 180 | if not repo_path: |
| 181 | repo_path = '~/bin/repo' |
| 182 | |
| 183 | if not ver: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 184 | print('no --wrapper-version argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 185 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 186 | |
| 187 | exp = _CurrentWrapperVersion() |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 188 | ver = tuple(map(int, ver.split('.'))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 189 | if len(ver) == 1: |
| 190 | ver = (0, ver[0]) |
| 191 | |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 192 | exp_str = '.'.join(map(str, exp)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 193 | if exp[0] > ver[0] or ver < (0, 4): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 194 | print(""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 195 | !!! A new repo command (%5s) is available. !!! |
| 196 | !!! You must upgrade before you can continue: !!! |
| 197 | |
| 198 | cp %s %s |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 199 | """ % (exp_str, _MyWrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 200 | sys.exit(1) |
| 201 | |
| 202 | if exp > ver: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 203 | print(""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | ... A new repo command (%5s) is available. |
| 205 | ... You should upgrade soon: |
| 206 | |
| 207 | cp %s %s |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 208 | """ % (exp_str, _MyWrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 209 | |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 210 | def _CheckRepoDir(repo_dir): |
| 211 | if not repo_dir: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 212 | print('no --repo-dir argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 213 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 214 | |
| 215 | def _PruneOptions(argv, opt): |
| 216 | i = 0 |
| 217 | while i < len(argv): |
| 218 | a = argv[i] |
| 219 | if a == '--': |
| 220 | break |
| 221 | if a.startswith('--'): |
| 222 | eq = a.find('=') |
| 223 | if eq > 0: |
| 224 | a = a[0:eq] |
| 225 | if not opt.has_option(a): |
| 226 | del argv[i] |
| 227 | continue |
| 228 | i += 1 |
| 229 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 230 | _user_agent = None |
| 231 | |
| 232 | def _UserAgent(): |
| 233 | global _user_agent |
| 234 | |
| 235 | if _user_agent is None: |
| 236 | py_version = sys.version_info |
| 237 | |
| 238 | os_name = sys.platform |
| 239 | if os_name == 'linux2': |
| 240 | os_name = 'Linux' |
| 241 | elif os_name == 'win32': |
| 242 | os_name = 'Win32' |
| 243 | elif os_name == 'cygwin': |
| 244 | os_name = 'Cygwin' |
| 245 | elif os_name == 'darwin': |
| 246 | os_name = 'Darwin' |
| 247 | |
| 248 | p = GitCommand( |
| 249 | None, ['describe', 'HEAD'], |
| 250 | cwd = _MyRepoPath(), |
| 251 | capture_stdout = True) |
| 252 | if p.Wait() == 0: |
| 253 | repo_version = p.stdout |
| 254 | if len(repo_version) > 0 and repo_version[-1] == '\n': |
| 255 | repo_version = repo_version[0:-1] |
| 256 | if len(repo_version) > 0 and repo_version[0] == 'v': |
| 257 | repo_version = repo_version[1:] |
| 258 | else: |
| 259 | repo_version = 'unknown' |
| 260 | |
| 261 | _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( |
| 262 | repo_version, |
| 263 | os_name, |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 264 | '.'.join(map(str, git.version_tuple())), |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 265 | py_version[0], py_version[1], py_version[2]) |
| 266 | return _user_agent |
| 267 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 268 | class _UserAgentHandler(urllib.request.BaseHandler): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 269 | def http_request(self, req): |
| 270 | req.add_header('User-Agent', _UserAgent()) |
| 271 | return req |
| 272 | |
| 273 | def https_request(self, req): |
| 274 | req.add_header('User-Agent', _UserAgent()) |
| 275 | return req |
| 276 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 277 | def _AddPasswordFromUserInput(handler, msg, req): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame^] | 278 | # If repo could not find auth info from netrc, try to get it from user input |
| 279 | url = req.get_full_url() |
| 280 | user, password = handler.passwd.find_user_password(None, url) |
| 281 | if user is None: |
| 282 | print(msg) |
| 283 | try: |
| 284 | user = raw_input('User: ') |
| 285 | password = getpass.getpass() |
| 286 | except KeyboardInterrupt: |
| 287 | return |
| 288 | handler.passwd.add_password(None, url, user, password) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 289 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 290 | class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 291 | def http_error_401(self, req, fp, code, msg, headers): |
| 292 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 293 | return urllib.request.HTTPBasicAuthHandler.http_error_401( |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 294 | self, req, fp, code, msg, headers) |
| 295 | |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 296 | def http_error_auth_reqed(self, authreq, host, req, headers): |
| 297 | try: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 298 | old_add_header = req.add_header |
| 299 | def _add_header(name, val): |
| 300 | val = val.replace('\n', '') |
| 301 | old_add_header(name, val) |
| 302 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 303 | return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed( |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 304 | self, authreq, host, req, headers) |
| 305 | except: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 306 | reset = getattr(self, 'reset_retry_count', None) |
| 307 | if reset is not None: |
| 308 | reset() |
Shawn O. Pearce | b660539 | 2011-10-11 15:58:07 -0700 | [diff] [blame] | 309 | elif getattr(self, 'retried', None): |
| 310 | self.retried = 0 |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 311 | raise |
| 312 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 313 | class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 314 | def http_error_401(self, req, fp, code, msg, headers): |
| 315 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 316 | return urllib.request.HTTPDigestAuthHandler.http_error_401( |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 317 | self, req, fp, code, msg, headers) |
| 318 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 319 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 320 | try: |
| 321 | old_add_header = req.add_header |
| 322 | def _add_header(name, val): |
| 323 | val = val.replace('\n', '') |
| 324 | old_add_header(name, val) |
| 325 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 326 | return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed( |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 327 | self, auth_header, host, req, headers) |
| 328 | except: |
| 329 | reset = getattr(self, 'reset_retry_count', None) |
| 330 | if reset is not None: |
| 331 | reset() |
| 332 | elif getattr(self, 'retried', None): |
| 333 | self.retried = 0 |
| 334 | raise |
| 335 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 336 | def init_http(): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 337 | handlers = [_UserAgentHandler()] |
| 338 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 339 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 340 | try: |
| 341 | n = netrc.netrc() |
| 342 | for host in n.hosts: |
| 343 | p = n.hosts[host] |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 344 | mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) |
| 345 | 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] | 346 | except netrc.NetrcParseError: |
| 347 | pass |
Shawn O. Pearce | 7b947de | 2011-09-23 11:50:31 -0700 | [diff] [blame] | 348 | except IOError: |
| 349 | pass |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 350 | handlers.append(_BasicAuthHandler(mgr)) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 351 | handlers.append(_DigestAuthHandler(mgr)) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 352 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 353 | if 'http_proxy' in os.environ: |
| 354 | url = os.environ['http_proxy'] |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 355 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 356 | if 'REPO_CURL_VERBOSE' in os.environ: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 357 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 358 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 359 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 360 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 361 | def _Main(argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 362 | result = 0 |
| 363 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 364 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 365 | opt.add_option("--repo-dir", dest="repodir", |
| 366 | help="path to .repo/") |
| 367 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 368 | help="version of the wrapper script") |
| 369 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 370 | help="location of the wrapper script") |
| 371 | _PruneOptions(argv, opt) |
| 372 | opt, argv = opt.parse_args(argv) |
| 373 | |
| 374 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 375 | _CheckRepoDir(opt.repodir) |
| 376 | |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 377 | Version.wrapper_version = opt.wrapper_version |
| 378 | Version.wrapper_path = opt.wrapper_path |
| 379 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 380 | repo = _Repo(opt.repodir) |
| 381 | try: |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 382 | try: |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 383 | init_ssh() |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 384 | init_http() |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 385 | result = repo._Run(argv) or 0 |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 386 | finally: |
| 387 | close_ssh() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 388 | except KeyboardInterrupt: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 389 | print('aborted by user', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 390 | result = 1 |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 391 | except ManifestParseError as mpe: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 392 | print('fatal: %s' % mpe, file=sys.stderr) |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 393 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 394 | except RepoChangedException as rce: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 395 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 396 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 397 | argv = list(sys.argv) |
| 398 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 399 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 400 | os.execv(__file__, argv) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 401 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 402 | print('fatal: cannot restart repo after upgrade', file=sys.stderr) |
| 403 | print('fatal: %s' % e, file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 404 | result = 128 |
| 405 | |
| 406 | sys.exit(result) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 407 | |
| 408 | if __name__ == '__main__': |
| 409 | _Main(sys.argv[1:]) |