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