blob: 48edc1cc1e2d809ba9cf21919c659167c23a8679 [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
25import optparse
26import os
27import re
28import sys
Shawn O. Pearce014d0602011-09-11 12:57:15 -070029import urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070031from trace import SetTrace
Doug Anderson0048b692010-12-21 13:39:23 -080032from git_config import init_ssh, close_ssh
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080033from command import InteractiveCommand
34from command import MirrorSafeCommand
35from command import PagedCommand
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070036from editor import Editor
Shawn O. Pearce559b8462009-03-02 12:56:08 -080037from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038from error import NoSuchProjectError
39from error import RepoChangedException
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070040from manifest_xml import XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070041from pager import RunPager
42
43from subcmds import all as all_commands
44
45global_options = optparse.OptionParser(
46 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
47 )
48global_options.add_option('-p', '--paginate',
49 dest='pager', action='store_true',
50 help='display command output in the pager')
51global_options.add_option('--no-pager',
52 dest='no_pager', action='store_true',
53 help='disable the pager')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070054global_options.add_option('--trace',
55 dest='trace', action='store_true',
56 help='trace git command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080057global_options.add_option('--version',
58 dest='show_version', action='store_true',
59 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070060
61class _Repo(object):
62 def __init__(self, repodir):
63 self.repodir = repodir
64 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -040065 # add 'branch' as an alias for 'branches'
66 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070067
68 def _Run(self, argv):
69 name = None
70 glob = []
71
72 for i in xrange(0, len(argv)):
73 if not argv[i].startswith('-'):
74 name = argv[i]
75 if i > 0:
76 glob = argv[:i]
77 argv = argv[i + 1:]
78 break
79 if not name:
80 glob = argv
81 name = 'help'
82 argv = []
83 gopts, gargs = global_options.parse_args(glob)
84
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070085 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070086 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080087 if gopts.show_version:
88 if name == 'help':
89 name = 'version'
90 else:
91 print >>sys.stderr, 'fatal: invalid usage of --version'
92 sys.exit(1)
93
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070094 try:
95 cmd = self.commands[name]
96 except KeyError:
97 print >>sys.stderr,\
98 "repo: '%s' is not a repo command. See 'repo help'."\
99 % name
100 sys.exit(1)
101
102 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700103 cmd.manifest = XmlManifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700104 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800106 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
107 print >>sys.stderr, \
108 "fatal: '%s' requires a working directory"\
109 % name
110 sys.exit(1)
111
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700112 copts, cargs = cmd.OptionParser.parse_args(argv)
113
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700114 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
115 config = cmd.manifest.globalConfig
116 if gopts.pager:
117 use_pager = True
118 else:
119 use_pager = config.GetBoolean('pager.%s' % name)
120 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700121 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700122 if use_pager:
123 RunPager(config)
124
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700125 try:
126 cmd.Execute(copts, cargs)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800127 except ManifestInvalidRevisionError, e:
128 print >>sys.stderr, 'error: %s' % str(e)
129 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700130 except NoSuchProjectError, e:
131 if e.name:
132 print >>sys.stderr, 'error: project %s not found' % e.name
133 else:
134 print >>sys.stderr, 'error: no project in current directory'
135 sys.exit(1)
136
137def _MyWrapperPath():
138 return os.path.join(os.path.dirname(__file__), 'repo')
139
140def _CurrentWrapperVersion():
141 VERSION = None
142 pat = re.compile(r'^VERSION *=')
143 fd = open(_MyWrapperPath())
144 for line in fd:
145 if pat.match(line):
146 fd.close()
147 exec line
148 return VERSION
149 raise NameError, 'No VERSION in repo script'
150
151def _CheckWrapperVersion(ver, repo_path):
152 if not repo_path:
153 repo_path = '~/bin/repo'
154
155 if not ver:
156 print >>sys.stderr, 'no --wrapper-version argument'
157 sys.exit(1)
158
159 exp = _CurrentWrapperVersion()
160 ver = tuple(map(lambda x: int(x), ver.split('.')))
161 if len(ver) == 1:
162 ver = (0, ver[0])
163
164 if exp[0] > ver[0] or ver < (0, 4):
165 exp_str = '.'.join(map(lambda x: str(x), exp))
166 print >>sys.stderr, """
167!!! A new repo command (%5s) is available. !!!
168!!! You must upgrade before you can continue: !!!
169
170 cp %s %s
171""" % (exp_str, _MyWrapperPath(), repo_path)
172 sys.exit(1)
173
174 if exp > ver:
175 exp_str = '.'.join(map(lambda x: str(x), exp))
176 print >>sys.stderr, """
177... A new repo command (%5s) is available.
178... You should upgrade soon:
179
180 cp %s %s
181""" % (exp_str, _MyWrapperPath(), repo_path)
182
183def _CheckRepoDir(dir):
184 if not dir:
185 print >>sys.stderr, 'no --repo-dir argument'
186 sys.exit(1)
187
188def _PruneOptions(argv, opt):
189 i = 0
190 while i < len(argv):
191 a = argv[i]
192 if a == '--':
193 break
194 if a.startswith('--'):
195 eq = a.find('=')
196 if eq > 0:
197 a = a[0:eq]
198 if not opt.has_option(a):
199 del argv[i]
200 continue
201 i += 1
202
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700203def init_http():
204 if 'http_proxy' in os.environ:
205 url = os.environ['http_proxy']
206 proxy_support = urllib2.ProxyHandler({'http': url, 'https': url})
207 urllib2.install_opener(urllib2.build_opener(proxy_support))
208
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700209def _Main(argv):
210 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
211 opt.add_option("--repo-dir", dest="repodir",
212 help="path to .repo/")
213 opt.add_option("--wrapper-version", dest="wrapper_version",
214 help="version of the wrapper script")
215 opt.add_option("--wrapper-path", dest="wrapper_path",
216 help="location of the wrapper script")
217 _PruneOptions(argv, opt)
218 opt, argv = opt.parse_args(argv)
219
220 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
221 _CheckRepoDir(opt.repodir)
222
223 repo = _Repo(opt.repodir)
224 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700225 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800226 init_ssh()
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700227 init_http()
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700228 repo._Run(argv)
229 finally:
230 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700231 except KeyboardInterrupt:
232 sys.exit(1)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800233 except RepoChangedException, rce:
234 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700235 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800236 argv = list(sys.argv)
237 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700238 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800239 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700240 except OSError, e:
241 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
242 print >>sys.stderr, 'fatal: %s' % e
243 sys.exit(128)
244
245if __name__ == '__main__':
246 _Main(sys.argv[1:])