blob: 6236dd3dfa77cb9a549ce50c59dadbed659b4a58 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#!/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
17magic='--calling-python-from-/bin/sh--'
Shawn O. Pearce7542d662008-10-21 07:11:36 -070018"""exec" python -E "$0" "$@" """#$magic"
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019if __name__ == '__main__':
20 import sys
21 if sys.argv[-1] == '#%s' % magic:
22 del sys.argv[-1]
23del magic
24
JoonCheol Parke9860722012-10-11 02:31:44 +090025import getpass
Conley Owensc9129d92012-10-01 16:12:28 -070026import imp
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -070027import netrc
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070028import optparse
29import os
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030import sys
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070031import time
Sarah Owens1f7627f2012-10-31 09:21:55 -070032try:
33 import urllib2
34except ImportError:
35 # For python3
36 import urllib.request
37else:
38 # For python2
39 import imp
40 urllib = imp.new_module('urllib')
41 urllib.request = urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070042
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070043from trace import SetTrace
Shawn O. Pearce334851e2011-09-19 08:05:31 -070044from git_command import git, GitCommand
Doug Anderson0048b692010-12-21 13:39:23 -080045from git_config import init_ssh, close_ssh
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080046from command import InteractiveCommand
47from command import MirrorSafeCommand
Shawn O. Pearceecff4f12011-11-29 15:01:33 -080048from subcmds.version import Version
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070049from editor import Editor
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070050from error import DownloadError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080051from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070052from error import NoSuchProjectError
53from error import RepoChangedException
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070054from manifest_xml import XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070055from pager import RunPager
56
David Pursehouse5c6eeac2012-10-11 16:44:48 +090057from subcmds import all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070058
59global_options = optparse.OptionParser(
60 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
61 )
62global_options.add_option('-p', '--paginate',
63 dest='pager', action='store_true',
64 help='display command output in the pager')
65global_options.add_option('--no-pager',
66 dest='no_pager', action='store_true',
67 help='disable the pager')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070068global_options.add_option('--trace',
69 dest='trace', action='store_true',
70 help='trace git command execution')
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070071global_options.add_option('--time',
72 dest='time', action='store_true',
73 help='time repo command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080074global_options.add_option('--version',
75 dest='show_version', action='store_true',
76 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070077
78class _Repo(object):
79 def __init__(self, repodir):
80 self.repodir = repodir
81 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -040082 # add 'branch' as an alias for 'branches'
83 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070084
85 def _Run(self, argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -040086 result = 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070087 name = None
88 glob = []
89
90 for i in xrange(0, len(argv)):
91 if not argv[i].startswith('-'):
92 name = argv[i]
93 if i > 0:
94 glob = argv[:i]
95 argv = argv[i + 1:]
96 break
97 if not name:
98 glob = argv
99 name = 'help'
100 argv = []
David Pursehouse8a68ff92012-09-24 12:15:13 +0900101 gopts, _gargs = global_options.parse_args(glob)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -0700103 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700104 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800105 if gopts.show_version:
106 if name == 'help':
107 name = 'version'
108 else:
109 print >>sys.stderr, 'fatal: invalid usage of --version'
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400110 return 1
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800111
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700112 try:
113 cmd = self.commands[name]
114 except KeyError:
115 print >>sys.stderr,\
116 "repo: '%s' is not a repo command. See 'repo help'."\
117 % name
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400118 return 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119
120 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700121 cmd.manifest = XmlManifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700122 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700123
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800124 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
125 print >>sys.stderr, \
126 "fatal: '%s' requires a working directory"\
127 % name
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400128 return 1
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800129
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700130 copts, cargs = cmd.OptionParser.parse_args(argv)
131
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700132 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
133 config = cmd.manifest.globalConfig
134 if gopts.pager:
135 use_pager = True
136 else:
137 use_pager = config.GetBoolean('pager.%s' % name)
138 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700139 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140 if use_pager:
141 RunPager(config)
142
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700143 try:
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -0700144 start = time.time()
145 try:
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400146 result = cmd.Execute(copts, cargs)
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -0700147 finally:
148 elapsed = time.time() - start
149 hours, remainder = divmod(elapsed, 3600)
150 minutes, seconds = divmod(remainder, 60)
151 if gopts.time:
152 if hours == 0:
153 print >>sys.stderr, 'real\t%dm%.3fs' \
154 % (minutes, seconds)
155 else:
156 print >>sys.stderr, 'real\t%dh%dm%.3fs' \
157 % (hours, minutes, seconds)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700158 except DownloadError as e:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700159 print >>sys.stderr, 'error: %s' % str(e)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400160 return 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700161 except ManifestInvalidRevisionError as e:
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800162 print >>sys.stderr, 'error: %s' % str(e)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400163 return 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700164 except NoSuchProjectError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700165 if e.name:
166 print >>sys.stderr, 'error: project %s not found' % e.name
167 else:
168 print >>sys.stderr, 'error: no project in current directory'
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400169 return 1
170
171 return result
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700172
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700173def _MyRepoPath():
174 return os.path.dirname(__file__)
175
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700176def _MyWrapperPath():
177 return os.path.join(os.path.dirname(__file__), 'repo')
178
Conley Owensc9129d92012-10-01 16:12:28 -0700179_wrapper_module = None
180def WrapperModule():
181 global _wrapper_module
182 if not _wrapper_module:
183 _wrapper_module = imp.load_source('wrapper', _MyWrapperPath())
184 return _wrapper_module
185
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700186def _CurrentWrapperVersion():
Conley Owensc9129d92012-10-01 16:12:28 -0700187 return WrapperModule().VERSION
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700188
189def _CheckWrapperVersion(ver, repo_path):
190 if not repo_path:
191 repo_path = '~/bin/repo'
192
193 if not ver:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900194 print >>sys.stderr, 'no --wrapper-version argument'
195 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700196
197 exp = _CurrentWrapperVersion()
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900198 ver = tuple(map(int, ver.split('.')))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700199 if len(ver) == 1:
200 ver = (0, ver[0])
201
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900202 exp_str = '.'.join(map(str, exp))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700203 if exp[0] > ver[0] or ver < (0, 4):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700204 print >>sys.stderr, """
205!!! A new repo command (%5s) is available. !!!
206!!! You must upgrade before you can continue: !!!
207
208 cp %s %s
209""" % (exp_str, _MyWrapperPath(), repo_path)
210 sys.exit(1)
211
212 if exp > ver:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700213 print >>sys.stderr, """
214... A new repo command (%5s) is available.
215... You should upgrade soon:
216
217 cp %s %s
218""" % (exp_str, _MyWrapperPath(), repo_path)
219
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200220def _CheckRepoDir(repo_dir):
221 if not repo_dir:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900222 print >>sys.stderr, 'no --repo-dir argument'
223 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700224
225def _PruneOptions(argv, opt):
226 i = 0
227 while i < len(argv):
228 a = argv[i]
229 if a == '--':
230 break
231 if a.startswith('--'):
232 eq = a.find('=')
233 if eq > 0:
234 a = a[0:eq]
235 if not opt.has_option(a):
236 del argv[i]
237 continue
238 i += 1
239
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700240_user_agent = None
241
242def _UserAgent():
243 global _user_agent
244
245 if _user_agent is None:
246 py_version = sys.version_info
247
248 os_name = sys.platform
249 if os_name == 'linux2':
250 os_name = 'Linux'
251 elif os_name == 'win32':
252 os_name = 'Win32'
253 elif os_name == 'cygwin':
254 os_name = 'Cygwin'
255 elif os_name == 'darwin':
256 os_name = 'Darwin'
257
258 p = GitCommand(
259 None, ['describe', 'HEAD'],
260 cwd = _MyRepoPath(),
261 capture_stdout = True)
262 if p.Wait() == 0:
263 repo_version = p.stdout
264 if len(repo_version) > 0 and repo_version[-1] == '\n':
265 repo_version = repo_version[0:-1]
266 if len(repo_version) > 0 and repo_version[0] == 'v':
267 repo_version = repo_version[1:]
268 else:
269 repo_version = 'unknown'
270
271 _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
272 repo_version,
273 os_name,
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900274 '.'.join(map(str, git.version_tuple())),
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700275 py_version[0], py_version[1], py_version[2])
276 return _user_agent
277
Sarah Owens1f7627f2012-10-31 09:21:55 -0700278class _UserAgentHandler(urllib.request.BaseHandler):
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700279 def http_request(self, req):
280 req.add_header('User-Agent', _UserAgent())
281 return req
282
283 def https_request(self, req):
284 req.add_header('User-Agent', _UserAgent())
285 return req
286
JoonCheol Parke9860722012-10-11 02:31:44 +0900287def _AddPasswordFromUserInput(handler, msg, req):
288 # If repo could not find auth info from netrc, try to get it from user input
289 url = req.get_full_url()
290 user, password = handler.passwd.find_user_password(None, url)
291 if user is None:
292 print msg
293 try:
294 user = raw_input('User: ')
295 password = getpass.getpass()
296 except KeyboardInterrupt:
297 return
298 handler.passwd.add_password(None, url, user, password)
299
Sarah Owens1f7627f2012-10-31 09:21:55 -0700300class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
JoonCheol Parke9860722012-10-11 02:31:44 +0900301 def http_error_401(self, req, fp, code, msg, headers):
302 _AddPasswordFromUserInput(self, msg, req)
Sarah Owens1f7627f2012-10-31 09:21:55 -0700303 return urllib.request.HTTPBasicAuthHandler.http_error_401(
JoonCheol Parke9860722012-10-11 02:31:44 +0900304 self, req, fp, code, msg, headers)
305
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700306 def http_error_auth_reqed(self, authreq, host, req, headers):
307 try:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700308 old_add_header = req.add_header
309 def _add_header(name, val):
310 val = val.replace('\n', '')
311 old_add_header(name, val)
312 req.add_header = _add_header
Sarah Owens1f7627f2012-10-31 09:21:55 -0700313 return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed(
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700314 self, authreq, host, req, headers)
315 except:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700316 reset = getattr(self, 'reset_retry_count', None)
317 if reset is not None:
318 reset()
Shawn O. Pearceb6605392011-10-11 15:58:07 -0700319 elif getattr(self, 'retried', None):
320 self.retried = 0
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700321 raise
322
Sarah Owens1f7627f2012-10-31 09:21:55 -0700323class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
JoonCheol Parke9860722012-10-11 02:31:44 +0900324 def http_error_401(self, req, fp, code, msg, headers):
325 _AddPasswordFromUserInput(self, msg, req)
Sarah Owens1f7627f2012-10-31 09:21:55 -0700326 return urllib.request.HTTPDigestAuthHandler.http_error_401(
JoonCheol Parke9860722012-10-11 02:31:44 +0900327 self, req, fp, code, msg, headers)
328
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800329 def http_error_auth_reqed(self, auth_header, host, req, headers):
330 try:
331 old_add_header = req.add_header
332 def _add_header(name, val):
333 val = val.replace('\n', '')
334 old_add_header(name, val)
335 req.add_header = _add_header
Sarah Owens1f7627f2012-10-31 09:21:55 -0700336 return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800337 self, auth_header, host, req, headers)
338 except:
339 reset = getattr(self, 'reset_retry_count', None)
340 if reset is not None:
341 reset()
342 elif getattr(self, 'retried', None):
343 self.retried = 0
344 raise
345
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700346def init_http():
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700347 handlers = [_UserAgentHandler()]
348
Sarah Owens1f7627f2012-10-31 09:21:55 -0700349 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700350 try:
351 n = netrc.netrc()
352 for host in n.hosts:
353 p = n.hosts[host]
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800354 mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
355 mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700356 except netrc.NetrcParseError:
357 pass
Shawn O. Pearce7b947de2011-09-23 11:50:31 -0700358 except IOError:
359 pass
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700360 handlers.append(_BasicAuthHandler(mgr))
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800361 handlers.append(_DigestAuthHandler(mgr))
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700362
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700363 if 'http_proxy' in os.environ:
364 url = os.environ['http_proxy']
Sarah Owens1f7627f2012-10-31 09:21:55 -0700365 handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700366 if 'REPO_CURL_VERBOSE' in os.environ:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700367 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
368 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
369 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700370
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700371def _Main(argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400372 result = 0
373
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700374 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
375 opt.add_option("--repo-dir", dest="repodir",
376 help="path to .repo/")
377 opt.add_option("--wrapper-version", dest="wrapper_version",
378 help="version of the wrapper script")
379 opt.add_option("--wrapper-path", dest="wrapper_path",
380 help="location of the wrapper script")
381 _PruneOptions(argv, opt)
382 opt, argv = opt.parse_args(argv)
383
384 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
385 _CheckRepoDir(opt.repodir)
386
Shawn O. Pearceecff4f12011-11-29 15:01:33 -0800387 Version.wrapper_version = opt.wrapper_version
388 Version.wrapper_path = opt.wrapper_path
389
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700390 repo = _Repo(opt.repodir)
391 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700392 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800393 init_ssh()
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700394 init_http()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400395 result = repo._Run(argv) or 0
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700396 finally:
397 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700398 except KeyboardInterrupt:
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400399 result = 1
Sarah Owensa5be53f2012-09-09 15:37:57 -0700400 except RepoChangedException as rce:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800401 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700402 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800403 argv = list(sys.argv)
404 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700405 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800406 os.execv(__file__, argv)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700407 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700408 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
409 print >>sys.stderr, 'fatal: %s' % e
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400410 result = 128
411
412 sys.exit(result)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700413
414if __name__ == '__main__':
415 _Main(sys.argv[1:])