blob: 858ed936c2e2a55a21116c14a16768eb6000b979 [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. Pearce559b8462009-03-02 12:56:08 -080040from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070041from error import NoSuchProjectError
42from error import RepoChangedException
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070043from manifest_xml import XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070044from pager import RunPager
45
46from subcmds import all as all_commands
47
48global_options = optparse.OptionParser(
49 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
50 )
51global_options.add_option('-p', '--paginate',
52 dest='pager', action='store_true',
53 help='display command output in the pager')
54global_options.add_option('--no-pager',
55 dest='no_pager', action='store_true',
56 help='disable the pager')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070057global_options.add_option('--trace',
58 dest='trace', action='store_true',
59 help='trace git command execution')
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070060global_options.add_option('--time',
61 dest='time', action='store_true',
62 help='time repo command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080063global_options.add_option('--version',
64 dest='show_version', action='store_true',
65 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066
67class _Repo(object):
68 def __init__(self, repodir):
69 self.repodir = repodir
70 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -040071 # add 'branch' as an alias for 'branches'
72 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070073
74 def _Run(self, argv):
75 name = None
76 glob = []
77
78 for i in xrange(0, len(argv)):
79 if not argv[i].startswith('-'):
80 name = argv[i]
81 if i > 0:
82 glob = argv[:i]
83 argv = argv[i + 1:]
84 break
85 if not name:
86 glob = argv
87 name = 'help'
88 argv = []
89 gopts, gargs = global_options.parse_args(glob)
90
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070091 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070092 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080093 if gopts.show_version:
94 if name == 'help':
95 name = 'version'
96 else:
97 print >>sys.stderr, 'fatal: invalid usage of --version'
98 sys.exit(1)
99
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700100 try:
101 cmd = self.commands[name]
102 except KeyError:
103 print >>sys.stderr,\
104 "repo: '%s' is not a repo command. See 'repo help'."\
105 % name
106 sys.exit(1)
107
108 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700109 cmd.manifest = XmlManifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700110 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800112 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
113 print >>sys.stderr, \
114 "fatal: '%s' requires a working directory"\
115 % name
116 sys.exit(1)
117
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700118 copts, cargs = cmd.OptionParser.parse_args(argv)
119
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700120 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
121 config = cmd.manifest.globalConfig
122 if gopts.pager:
123 use_pager = True
124 else:
125 use_pager = config.GetBoolean('pager.%s' % name)
126 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700127 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128 if use_pager:
129 RunPager(config)
130
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131 try:
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -0700132 start = time.time()
133 try:
134 cmd.Execute(copts, cargs)
135 finally:
136 elapsed = time.time() - start
137 hours, remainder = divmod(elapsed, 3600)
138 minutes, seconds = divmod(remainder, 60)
139 if gopts.time:
140 if hours == 0:
141 print >>sys.stderr, 'real\t%dm%.3fs' \
142 % (minutes, seconds)
143 else:
144 print >>sys.stderr, 'real\t%dh%dm%.3fs' \
145 % (hours, minutes, seconds)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800146 except ManifestInvalidRevisionError, e:
147 print >>sys.stderr, 'error: %s' % str(e)
148 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700149 except NoSuchProjectError, e:
150 if e.name:
151 print >>sys.stderr, 'error: project %s not found' % e.name
152 else:
153 print >>sys.stderr, 'error: no project in current directory'
154 sys.exit(1)
155
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700156def _MyRepoPath():
157 return os.path.dirname(__file__)
158
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700159def _MyWrapperPath():
160 return os.path.join(os.path.dirname(__file__), 'repo')
161
162def _CurrentWrapperVersion():
163 VERSION = None
164 pat = re.compile(r'^VERSION *=')
165 fd = open(_MyWrapperPath())
166 for line in fd:
167 if pat.match(line):
168 fd.close()
169 exec line
170 return VERSION
171 raise NameError, 'No VERSION in repo script'
172
173def _CheckWrapperVersion(ver, repo_path):
174 if not repo_path:
175 repo_path = '~/bin/repo'
176
177 if not ver:
178 print >>sys.stderr, 'no --wrapper-version argument'
179 sys.exit(1)
180
181 exp = _CurrentWrapperVersion()
182 ver = tuple(map(lambda x: int(x), ver.split('.')))
183 if len(ver) == 1:
184 ver = (0, ver[0])
185
186 if exp[0] > ver[0] or ver < (0, 4):
187 exp_str = '.'.join(map(lambda x: str(x), exp))
188 print >>sys.stderr, """
189!!! A new repo command (%5s) is available. !!!
190!!! You must upgrade before you can continue: !!!
191
192 cp %s %s
193""" % (exp_str, _MyWrapperPath(), repo_path)
194 sys.exit(1)
195
196 if exp > ver:
197 exp_str = '.'.join(map(lambda x: str(x), exp))
198 print >>sys.stderr, """
199... A new repo command (%5s) is available.
200... You should upgrade soon:
201
202 cp %s %s
203""" % (exp_str, _MyWrapperPath(), repo_path)
204
205def _CheckRepoDir(dir):
206 if not dir:
207 print >>sys.stderr, 'no --repo-dir argument'
208 sys.exit(1)
209
210def _PruneOptions(argv, opt):
211 i = 0
212 while i < len(argv):
213 a = argv[i]
214 if a == '--':
215 break
216 if a.startswith('--'):
217 eq = a.find('=')
218 if eq > 0:
219 a = a[0:eq]
220 if not opt.has_option(a):
221 del argv[i]
222 continue
223 i += 1
224
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700225_user_agent = None
226
227def _UserAgent():
228 global _user_agent
229
230 if _user_agent is None:
231 py_version = sys.version_info
232
233 os_name = sys.platform
234 if os_name == 'linux2':
235 os_name = 'Linux'
236 elif os_name == 'win32':
237 os_name = 'Win32'
238 elif os_name == 'cygwin':
239 os_name = 'Cygwin'
240 elif os_name == 'darwin':
241 os_name = 'Darwin'
242
243 p = GitCommand(
244 None, ['describe', 'HEAD'],
245 cwd = _MyRepoPath(),
246 capture_stdout = True)
247 if p.Wait() == 0:
248 repo_version = p.stdout
249 if len(repo_version) > 0 and repo_version[-1] == '\n':
250 repo_version = repo_version[0:-1]
251 if len(repo_version) > 0 and repo_version[0] == 'v':
252 repo_version = repo_version[1:]
253 else:
254 repo_version = 'unknown'
255
256 _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
257 repo_version,
258 os_name,
259 '.'.join(map(lambda d: str(d), git.version_tuple())),
260 py_version[0], py_version[1], py_version[2])
261 return _user_agent
262
263class _UserAgentHandler(urllib2.BaseHandler):
264 def http_request(self, req):
265 req.add_header('User-Agent', _UserAgent())
266 return req
267
268 def https_request(self, req):
269 req.add_header('User-Agent', _UserAgent())
270 return req
271
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700272def init_http():
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700273 handlers = [_UserAgentHandler()]
274
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700275 mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
276 try:
277 n = netrc.netrc()
278 for host in n.hosts:
279 p = n.hosts[host]
280 mgr.add_password(None, 'http://%s/' % host, p[0], p[2])
281 mgr.add_password(None, 'https://%s/' % host, p[0], p[2])
282 except netrc.NetrcParseError:
283 pass
284 handlers.append(urllib2.HTTPBasicAuthHandler(mgr))
285
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700286 if 'http_proxy' in os.environ:
287 url = os.environ['http_proxy']
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700288 handlers.append(urllib2.ProxyHandler({'http': url, 'https': url}))
289 if 'REPO_CURL_VERBOSE' in os.environ:
290 handlers.append(urllib2.HTTPHandler(debuglevel=1))
291 handlers.append(urllib2.HTTPSHandler(debuglevel=1))
292 urllib2.install_opener(urllib2.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700293
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700294def _Main(argv):
295 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
296 opt.add_option("--repo-dir", dest="repodir",
297 help="path to .repo/")
298 opt.add_option("--wrapper-version", dest="wrapper_version",
299 help="version of the wrapper script")
300 opt.add_option("--wrapper-path", dest="wrapper_path",
301 help="location of the wrapper script")
302 _PruneOptions(argv, opt)
303 opt, argv = opt.parse_args(argv)
304
305 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
306 _CheckRepoDir(opt.repodir)
307
308 repo = _Repo(opt.repodir)
309 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700310 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800311 init_ssh()
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700312 init_http()
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700313 repo._Run(argv)
314 finally:
315 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700316 except KeyboardInterrupt:
317 sys.exit(1)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800318 except RepoChangedException, rce:
319 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700320 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800321 argv = list(sys.argv)
322 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700323 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800324 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700325 except OSError, e:
326 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
327 print >>sys.stderr, 'fatal: %s' % e
328 sys.exit(128)
329
330if __name__ == '__main__':
331 _Main(sys.argv[1:])