blob: db4fa0fb315d80b0c83419a118359bb1e22069bc [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
30from command import InteractiveCommand, PagedCommand
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070031from editor import Editor
Shawn O. Pearce559b8462009-03-02 12:56:08 -080032from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033from error import NoSuchProjectError
34from error import RepoChangedException
35from manifest import Manifest
36from pager import RunPager
37
38from subcmds import all as all_commands
39
40global_options = optparse.OptionParser(
41 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
42 )
43global_options.add_option('-p', '--paginate',
44 dest='pager', action='store_true',
45 help='display command output in the pager')
46global_options.add_option('--no-pager',
47 dest='no_pager', action='store_true',
48 help='disable the pager')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080049global_options.add_option('--version',
50 dest='show_version', action='store_true',
51 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070052
53class _Repo(object):
54 def __init__(self, repodir):
55 self.repodir = repodir
56 self.commands = all_commands
57
58 def _Run(self, argv):
59 name = None
60 glob = []
61
62 for i in xrange(0, len(argv)):
63 if not argv[i].startswith('-'):
64 name = argv[i]
65 if i > 0:
66 glob = argv[:i]
67 argv = argv[i + 1:]
68 break
69 if not name:
70 glob = argv
71 name = 'help'
72 argv = []
73 gopts, gargs = global_options.parse_args(glob)
74
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080075 if gopts.show_version:
76 if name == 'help':
77 name = 'version'
78 else:
79 print >>sys.stderr, 'fatal: invalid usage of --version'
80 sys.exit(1)
81
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082 try:
83 cmd = self.commands[name]
84 except KeyError:
85 print >>sys.stderr,\
86 "repo: '%s' is not a repo command. See 'repo help'."\
87 % name
88 sys.exit(1)
89
90 cmd.repodir = self.repodir
91 cmd.manifest = Manifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070092 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070093
94 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
95 config = cmd.manifest.globalConfig
96 if gopts.pager:
97 use_pager = True
98 else:
99 use_pager = config.GetBoolean('pager.%s' % name)
100 if use_pager is None:
101 use_pager = isinstance(cmd, PagedCommand)
102 if use_pager:
103 RunPager(config)
104
105 copts, cargs = cmd.OptionParser.parse_args(argv)
106 try:
107 cmd.Execute(copts, cargs)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800108 except ManifestInvalidRevisionError, e:
109 print >>sys.stderr, 'error: %s' % str(e)
110 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111 except NoSuchProjectError, e:
112 if e.name:
113 print >>sys.stderr, 'error: project %s not found' % e.name
114 else:
115 print >>sys.stderr, 'error: no project in current directory'
116 sys.exit(1)
117
118def _MyWrapperPath():
119 return os.path.join(os.path.dirname(__file__), 'repo')
120
121def _CurrentWrapperVersion():
122 VERSION = None
123 pat = re.compile(r'^VERSION *=')
124 fd = open(_MyWrapperPath())
125 for line in fd:
126 if pat.match(line):
127 fd.close()
128 exec line
129 return VERSION
130 raise NameError, 'No VERSION in repo script'
131
132def _CheckWrapperVersion(ver, repo_path):
133 if not repo_path:
134 repo_path = '~/bin/repo'
135
136 if not ver:
137 print >>sys.stderr, 'no --wrapper-version argument'
138 sys.exit(1)
139
140 exp = _CurrentWrapperVersion()
141 ver = tuple(map(lambda x: int(x), ver.split('.')))
142 if len(ver) == 1:
143 ver = (0, ver[0])
144
145 if exp[0] > ver[0] or ver < (0, 4):
146 exp_str = '.'.join(map(lambda x: str(x), exp))
147 print >>sys.stderr, """
148!!! A new repo command (%5s) is available. !!!
149!!! You must upgrade before you can continue: !!!
150
151 cp %s %s
152""" % (exp_str, _MyWrapperPath(), repo_path)
153 sys.exit(1)
154
155 if exp > ver:
156 exp_str = '.'.join(map(lambda x: str(x), exp))
157 print >>sys.stderr, """
158... A new repo command (%5s) is available.
159... You should upgrade soon:
160
161 cp %s %s
162""" % (exp_str, _MyWrapperPath(), repo_path)
163
164def _CheckRepoDir(dir):
165 if not dir:
166 print >>sys.stderr, 'no --repo-dir argument'
167 sys.exit(1)
168
169def _PruneOptions(argv, opt):
170 i = 0
171 while i < len(argv):
172 a = argv[i]
173 if a == '--':
174 break
175 if a.startswith('--'):
176 eq = a.find('=')
177 if eq > 0:
178 a = a[0:eq]
179 if not opt.has_option(a):
180 del argv[i]
181 continue
182 i += 1
183
184def _Main(argv):
185 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
186 opt.add_option("--repo-dir", dest="repodir",
187 help="path to .repo/")
188 opt.add_option("--wrapper-version", dest="wrapper_version",
189 help="version of the wrapper script")
190 opt.add_option("--wrapper-path", dest="wrapper_path",
191 help="location of the wrapper script")
192 _PruneOptions(argv, opt)
193 opt, argv = opt.parse_args(argv)
194
195 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
196 _CheckRepoDir(opt.repodir)
197
198 repo = _Repo(opt.repodir)
199 try:
200 repo._Run(argv)
201 except KeyboardInterrupt:
202 sys.exit(1)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800203 except RepoChangedException, rce:
204 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700205 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800206 argv = list(sys.argv)
207 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700208 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800209 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700210 except OSError, e:
211 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
212 print >>sys.stderr, 'fatal: %s' % e
213 sys.exit(128)
214
215if __name__ == '__main__':
216 _Main(sys.argv[1:])