blob: fda81038d05766364fb0cdbcbf72166a73038ae9 [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
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -070025import netrc
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026import optparse
27import os
28import re
29import sys
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070030import time
Shawn O. Pearce014d0602011-09-11 12:57:15 -070031import urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070033from trace import SetTrace
Shawn O. Pearce334851e2011-09-19 08:05:31 -070034from git_command import git, GitCommand
Doug Anderson0048b692010-12-21 13:39:23 -080035from git_config import init_ssh, close_ssh
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080036from command import InteractiveCommand
37from command import MirrorSafeCommand
38from command import PagedCommand
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070039from editor import Editor
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070040from error import DownloadError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080041from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070042from error import NoSuchProjectError
43from error import RepoChangedException
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070044from manifest_xml import XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070045from pager import RunPager
46
47from subcmds import all as all_commands
48
49global_options = optparse.OptionParser(
50 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
51 )
52global_options.add_option('-p', '--paginate',
53 dest='pager', action='store_true',
54 help='display command output in the pager')
55global_options.add_option('--no-pager',
56 dest='no_pager', action='store_true',
57 help='disable the pager')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070058global_options.add_option('--trace',
59 dest='trace', action='store_true',
60 help='trace git command execution')
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070061global_options.add_option('--time',
62 dest='time', action='store_true',
63 help='time repo command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080064global_options.add_option('--version',
65 dest='show_version', action='store_true',
66 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070067
68class _Repo(object):
69 def __init__(self, repodir):
70 self.repodir = repodir
71 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -040072 # add 'branch' as an alias for 'branches'
73 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070074
75 def _Run(self, argv):
76 name = None
77 glob = []
78
79 for i in xrange(0, len(argv)):
80 if not argv[i].startswith('-'):
81 name = argv[i]
82 if i > 0:
83 glob = argv[:i]
84 argv = argv[i + 1:]
85 break
86 if not name:
87 glob = argv
88 name = 'help'
89 argv = []
90 gopts, gargs = global_options.parse_args(glob)
91
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070092 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070093 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080094 if gopts.show_version:
95 if name == 'help':
96 name = 'version'
97 else:
98 print >>sys.stderr, 'fatal: invalid usage of --version'
99 sys.exit(1)
100
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101 try:
102 cmd = self.commands[name]
103 except KeyError:
104 print >>sys.stderr,\
105 "repo: '%s' is not a repo command. See 'repo help'."\
106 % name
107 sys.exit(1)
108
109 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700110 cmd.manifest = XmlManifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700111 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700112
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800113 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
114 print >>sys.stderr, \
115 "fatal: '%s' requires a working directory"\
116 % name
117 sys.exit(1)
118
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700119 copts, cargs = cmd.OptionParser.parse_args(argv)
120
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700121 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
122 config = cmd.manifest.globalConfig
123 if gopts.pager:
124 use_pager = True
125 else:
126 use_pager = config.GetBoolean('pager.%s' % name)
127 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700128 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129 if use_pager:
130 RunPager(config)
131
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700132 try:
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -0700133 start = time.time()
134 try:
135 cmd.Execute(copts, cargs)
136 finally:
137 elapsed = time.time() - start
138 hours, remainder = divmod(elapsed, 3600)
139 minutes, seconds = divmod(remainder, 60)
140 if gopts.time:
141 if hours == 0:
142 print >>sys.stderr, 'real\t%dm%.3fs' \
143 % (minutes, seconds)
144 else:
145 print >>sys.stderr, 'real\t%dh%dm%.3fs' \
146 % (hours, minutes, seconds)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700147 except DownloadError, e:
148 print >>sys.stderr, 'error: %s' % str(e)
149 sys.exit(1)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800150 except ManifestInvalidRevisionError, e:
151 print >>sys.stderr, 'error: %s' % str(e)
152 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700153 except NoSuchProjectError, e:
154 if e.name:
155 print >>sys.stderr, 'error: project %s not found' % e.name
156 else:
157 print >>sys.stderr, 'error: no project in current directory'
158 sys.exit(1)
159
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700160def _MyRepoPath():
161 return os.path.dirname(__file__)
162
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700163def _MyWrapperPath():
164 return os.path.join(os.path.dirname(__file__), 'repo')
165
166def _CurrentWrapperVersion():
167 VERSION = None
168 pat = re.compile(r'^VERSION *=')
169 fd = open(_MyWrapperPath())
170 for line in fd:
171 if pat.match(line):
172 fd.close()
173 exec line
174 return VERSION
175 raise NameError, 'No VERSION in repo script'
176
177def _CheckWrapperVersion(ver, repo_path):
178 if not repo_path:
179 repo_path = '~/bin/repo'
180
181 if not ver:
182 print >>sys.stderr, 'no --wrapper-version argument'
183 sys.exit(1)
184
185 exp = _CurrentWrapperVersion()
186 ver = tuple(map(lambda x: int(x), ver.split('.')))
187 if len(ver) == 1:
188 ver = (0, ver[0])
189
190 if exp[0] > ver[0] or ver < (0, 4):
191 exp_str = '.'.join(map(lambda x: str(x), exp))
192 print >>sys.stderr, """
193!!! A new repo command (%5s) is available. !!!
194!!! You must upgrade before you can continue: !!!
195
196 cp %s %s
197""" % (exp_str, _MyWrapperPath(), repo_path)
198 sys.exit(1)
199
200 if exp > ver:
201 exp_str = '.'.join(map(lambda x: str(x), exp))
202 print >>sys.stderr, """
203... A new repo command (%5s) is available.
204... You should upgrade soon:
205
206 cp %s %s
207""" % (exp_str, _MyWrapperPath(), repo_path)
208
209def _CheckRepoDir(dir):
210 if not dir:
211 print >>sys.stderr, 'no --repo-dir argument'
212 sys.exit(1)
213
214def _PruneOptions(argv, opt):
215 i = 0
216 while i < len(argv):
217 a = argv[i]
218 if a == '--':
219 break
220 if a.startswith('--'):
221 eq = a.find('=')
222 if eq > 0:
223 a = a[0:eq]
224 if not opt.has_option(a):
225 del argv[i]
226 continue
227 i += 1
228
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700229_user_agent = None
230
231def _UserAgent():
232 global _user_agent
233
234 if _user_agent is None:
235 py_version = sys.version_info
236
237 os_name = sys.platform
238 if os_name == 'linux2':
239 os_name = 'Linux'
240 elif os_name == 'win32':
241 os_name = 'Win32'
242 elif os_name == 'cygwin':
243 os_name = 'Cygwin'
244 elif os_name == 'darwin':
245 os_name = 'Darwin'
246
247 p = GitCommand(
248 None, ['describe', 'HEAD'],
249 cwd = _MyRepoPath(),
250 capture_stdout = True)
251 if p.Wait() == 0:
252 repo_version = p.stdout
253 if len(repo_version) > 0 and repo_version[-1] == '\n':
254 repo_version = repo_version[0:-1]
255 if len(repo_version) > 0 and repo_version[0] == 'v':
256 repo_version = repo_version[1:]
257 else:
258 repo_version = 'unknown'
259
260 _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
261 repo_version,
262 os_name,
263 '.'.join(map(lambda d: str(d), git.version_tuple())),
264 py_version[0], py_version[1], py_version[2])
265 return _user_agent
266
267class _UserAgentHandler(urllib2.BaseHandler):
268 def http_request(self, req):
269 req.add_header('User-Agent', _UserAgent())
270 return req
271
272 def https_request(self, req):
273 req.add_header('User-Agent', _UserAgent())
274 return req
275
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700276class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler):
277 def http_error_auth_reqed(self, authreq, host, req, headers):
278 try:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700279 old_add_header = req.add_header
280 def _add_header(name, val):
281 val = val.replace('\n', '')
282 old_add_header(name, val)
283 req.add_header = _add_header
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700284 return urllib2.AbstractBasicAuthHandler.http_error_auth_reqed(
285 self, authreq, host, req, headers)
286 except:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700287 reset = getattr(self, 'reset_retry_count', None)
288 if reset is not None:
289 reset()
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700290 raise
291
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700292def init_http():
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700293 handlers = [_UserAgentHandler()]
294
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700295 mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
296 try:
297 n = netrc.netrc()
298 for host in n.hosts:
299 p = n.hosts[host]
300 mgr.add_password(None, 'http://%s/' % host, p[0], p[2])
301 mgr.add_password(None, 'https://%s/' % host, p[0], p[2])
302 except netrc.NetrcParseError:
303 pass
Shawn O. Pearce7b947de2011-09-23 11:50:31 -0700304 except IOError:
305 pass
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700306 handlers.append(_BasicAuthHandler(mgr))
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700307
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700308 if 'http_proxy' in os.environ:
309 url = os.environ['http_proxy']
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700310 handlers.append(urllib2.ProxyHandler({'http': url, 'https': url}))
311 if 'REPO_CURL_VERBOSE' in os.environ:
312 handlers.append(urllib2.HTTPHandler(debuglevel=1))
313 handlers.append(urllib2.HTTPSHandler(debuglevel=1))
314 urllib2.install_opener(urllib2.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700315
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700316def _Main(argv):
317 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
318 opt.add_option("--repo-dir", dest="repodir",
319 help="path to .repo/")
320 opt.add_option("--wrapper-version", dest="wrapper_version",
321 help="version of the wrapper script")
322 opt.add_option("--wrapper-path", dest="wrapper_path",
323 help="location of the wrapper script")
324 _PruneOptions(argv, opt)
325 opt, argv = opt.parse_args(argv)
326
327 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
328 _CheckRepoDir(opt.repodir)
329
330 repo = _Repo(opt.repodir)
331 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700332 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800333 init_ssh()
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700334 init_http()
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700335 repo._Run(argv)
336 finally:
337 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700338 except KeyboardInterrupt:
339 sys.exit(1)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800340 except RepoChangedException, rce:
341 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700342 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800343 argv = list(sys.argv)
344 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700345 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800346 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700347 except OSError, e:
348 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
349 print >>sys.stderr, 'fatal: %s' % e
350 sys.exit(128)
351
352if __name__ == '__main__':
353 _Main(sys.argv[1:])