blob: 31a18e18b51e8ed3ad709d15333348b68d04f196 [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
29
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070030from trace import SetTrace
Doug Anderson0048b692010-12-21 13:39:23 -080031from git_config import init_ssh, close_ssh
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080032from command import InteractiveCommand
33from command import MirrorSafeCommand
34from command import PagedCommand
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070035from editor import Editor
Shawn O. Pearce559b8462009-03-02 12:56:08 -080036from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037from error import NoSuchProjectError
38from error import RepoChangedException
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070039from manifest_xml import XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040from pager import RunPager
41
42from subcmds import all as all_commands
43
44global_options = optparse.OptionParser(
45 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
46 )
47global_options.add_option('-p', '--paginate',
48 dest='pager', action='store_true',
49 help='display command output in the pager')
50global_options.add_option('--no-pager',
51 dest='no_pager', action='store_true',
52 help='disable the pager')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070053global_options.add_option('--trace',
54 dest='trace', action='store_true',
55 help='trace git command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080056global_options.add_option('--version',
57 dest='show_version', action='store_true',
58 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070059
60class _Repo(object):
61 def __init__(self, repodir):
62 self.repodir = repodir
63 self.commands = all_commands
64
65 def _Run(self, argv):
66 name = None
67 glob = []
68
69 for i in xrange(0, len(argv)):
70 if not argv[i].startswith('-'):
71 name = argv[i]
72 if i > 0:
73 glob = argv[:i]
74 argv = argv[i + 1:]
75 break
76 if not name:
77 glob = argv
78 name = 'help'
79 argv = []
80 gopts, gargs = global_options.parse_args(glob)
81
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070082 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070083 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080084 if gopts.show_version:
85 if name == 'help':
86 name = 'version'
87 else:
88 print >>sys.stderr, 'fatal: invalid usage of --version'
89 sys.exit(1)
90
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070091 try:
92 cmd = self.commands[name]
93 except KeyError:
94 print >>sys.stderr,\
95 "repo: '%s' is not a repo command. See 'repo help'."\
96 % name
97 sys.exit(1)
98
99 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700100 cmd.manifest = XmlManifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700101 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800103 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
104 print >>sys.stderr, \
105 "fatal: '%s' requires a working directory"\
106 % name
107 sys.exit(1)
108
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700109 copts, cargs = cmd.OptionParser.parse_args(argv)
110
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
112 config = cmd.manifest.globalConfig
113 if gopts.pager:
114 use_pager = True
115 else:
116 use_pager = config.GetBoolean('pager.%s' % name)
117 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700118 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119 if use_pager:
120 RunPager(config)
121
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700122 try:
123 cmd.Execute(copts, cargs)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800124 except ManifestInvalidRevisionError, e:
125 print >>sys.stderr, 'error: %s' % str(e)
126 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700127 except NoSuchProjectError, e:
128 if e.name:
129 print >>sys.stderr, 'error: project %s not found' % e.name
130 else:
131 print >>sys.stderr, 'error: no project in current directory'
132 sys.exit(1)
133
134def _MyWrapperPath():
135 return os.path.join(os.path.dirname(__file__), 'repo')
136
137def _CurrentWrapperVersion():
138 VERSION = None
139 pat = re.compile(r'^VERSION *=')
140 fd = open(_MyWrapperPath())
141 for line in fd:
142 if pat.match(line):
143 fd.close()
144 exec line
145 return VERSION
146 raise NameError, 'No VERSION in repo script'
147
148def _CheckWrapperVersion(ver, repo_path):
149 if not repo_path:
150 repo_path = '~/bin/repo'
151
152 if not ver:
153 print >>sys.stderr, 'no --wrapper-version argument'
154 sys.exit(1)
155
156 exp = _CurrentWrapperVersion()
157 ver = tuple(map(lambda x: int(x), ver.split('.')))
158 if len(ver) == 1:
159 ver = (0, ver[0])
160
161 if exp[0] > ver[0] or ver < (0, 4):
162 exp_str = '.'.join(map(lambda x: str(x), exp))
163 print >>sys.stderr, """
164!!! A new repo command (%5s) is available. !!!
165!!! You must upgrade before you can continue: !!!
166
167 cp %s %s
168""" % (exp_str, _MyWrapperPath(), repo_path)
169 sys.exit(1)
170
171 if exp > ver:
172 exp_str = '.'.join(map(lambda x: str(x), exp))
173 print >>sys.stderr, """
174... A new repo command (%5s) is available.
175... You should upgrade soon:
176
177 cp %s %s
178""" % (exp_str, _MyWrapperPath(), repo_path)
179
180def _CheckRepoDir(dir):
181 if not dir:
182 print >>sys.stderr, 'no --repo-dir argument'
183 sys.exit(1)
184
185def _PruneOptions(argv, opt):
186 i = 0
187 while i < len(argv):
188 a = argv[i]
189 if a == '--':
190 break
191 if a.startswith('--'):
192 eq = a.find('=')
193 if eq > 0:
194 a = a[0:eq]
195 if not opt.has_option(a):
196 del argv[i]
197 continue
198 i += 1
199
200def _Main(argv):
201 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
202 opt.add_option("--repo-dir", dest="repodir",
203 help="path to .repo/")
204 opt.add_option("--wrapper-version", dest="wrapper_version",
205 help="version of the wrapper script")
206 opt.add_option("--wrapper-path", dest="wrapper_path",
207 help="location of the wrapper script")
208 _PruneOptions(argv, opt)
209 opt, argv = opt.parse_args(argv)
210
211 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
212 _CheckRepoDir(opt.repodir)
213
214 repo = _Repo(opt.repodir)
215 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700216 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800217 init_ssh()
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700218 repo._Run(argv)
219 finally:
220 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700221 except KeyboardInterrupt:
222 sys.exit(1)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800223 except RepoChangedException, rce:
224 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700225 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800226 argv = list(sys.argv)
227 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700228 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800229 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700230 except OSError, e:
231 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
232 print >>sys.stderr, 'fatal: %s' % e
233 sys.exit(128)
234
235if __name__ == '__main__':
236 _Main(sys.argv[1:])