The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | #!/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 | |
| 17 | magic='--calling-python-from-/bin/sh--' |
Shawn O. Pearce | 7542d66 | 2008-10-21 07:11:36 -0700 | [diff] [blame] | 18 | """exec" python -E "$0" "$@" """#$magic" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | if __name__ == '__main__': |
| 20 | import sys |
| 21 | if sys.argv[-1] == '#%s' % magic: |
| 22 | del sys.argv[-1] |
| 23 | del magic |
| 24 | |
| 25 | import optparse |
| 26 | import os |
| 27 | import re |
| 28 | import sys |
| 29 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 30 | from trace import SetTrace |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 31 | from git_config import close_ssh |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 32 | from command import InteractiveCommand |
| 33 | from command import MirrorSafeCommand |
| 34 | from command import PagedCommand |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 35 | from error import ManifestInvalidRevisionError |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | from error import NoSuchProjectError |
| 37 | from error import RepoChangedException |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | from pager import RunPager |
| 39 | |
| 40 | from subcmds import all as all_commands |
| 41 | |
| 42 | global_options = optparse.OptionParser( |
| 43 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" |
| 44 | ) |
| 45 | global_options.add_option('-p', '--paginate', |
| 46 | dest='pager', action='store_true', |
| 47 | help='display command output in the pager') |
| 48 | global_options.add_option('--no-pager', |
| 49 | dest='no_pager', action='store_true', |
| 50 | help='disable the pager') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 51 | global_options.add_option('--trace', |
| 52 | dest='trace', action='store_true', |
| 53 | help='trace git command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 54 | global_options.add_option('--version', |
| 55 | dest='show_version', action='store_true', |
| 56 | help='display this version of repo') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | |
| 58 | class _Repo(object): |
| 59 | def __init__(self, repodir): |
| 60 | self.repodir = repodir |
| 61 | self.commands = all_commands |
| 62 | |
| 63 | def _Run(self, argv): |
| 64 | name = None |
| 65 | glob = [] |
| 66 | |
| 67 | for i in xrange(0, len(argv)): |
| 68 | if not argv[i].startswith('-'): |
| 69 | name = argv[i] |
| 70 | if i > 0: |
| 71 | glob = argv[:i] |
| 72 | argv = argv[i + 1:] |
| 73 | break |
| 74 | if not name: |
| 75 | glob = argv |
| 76 | name = 'help' |
| 77 | argv = [] |
| 78 | gopts, gargs = global_options.parse_args(glob) |
| 79 | |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 80 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 81 | SetTrace() |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 82 | if gopts.show_version: |
| 83 | if name == 'help': |
| 84 | name = 'version' |
| 85 | else: |
| 86 | print >>sys.stderr, 'fatal: invalid usage of --version' |
| 87 | sys.exit(1) |
| 88 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | try: |
| 90 | cmd = self.commands[name] |
| 91 | except KeyError: |
| 92 | print >>sys.stderr,\ |
| 93 | "repo: '%s' is not a repo command. See 'repo help'."\ |
| 94 | % name |
| 95 | sys.exit(1) |
| 96 | |
| 97 | cmd.repodir = self.repodir |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 99 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
| 100 | print >>sys.stderr, \ |
| 101 | "fatal: '%s' requires a working directory"\ |
| 102 | % name |
| 103 | sys.exit(1) |
| 104 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 105 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 106 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | if not gopts.no_pager and not isinstance(cmd, InteractiveCommand): |
| 108 | config = cmd.manifest.globalConfig |
| 109 | if gopts.pager: |
| 110 | use_pager = True |
| 111 | else: |
| 112 | use_pager = config.GetBoolean('pager.%s' % name) |
| 113 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 114 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | if use_pager: |
| 116 | RunPager(config) |
| 117 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | try: |
| 119 | cmd.Execute(copts, cargs) |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 120 | except ManifestInvalidRevisionError, e: |
| 121 | print >>sys.stderr, 'error: %s' % str(e) |
| 122 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 123 | except NoSuchProjectError, e: |
| 124 | if e.name: |
| 125 | print >>sys.stderr, 'error: project %s not found' % e.name |
| 126 | else: |
| 127 | print >>sys.stderr, 'error: no project in current directory' |
| 128 | sys.exit(1) |
| 129 | |
| 130 | def _MyWrapperPath(): |
| 131 | return os.path.join(os.path.dirname(__file__), 'repo') |
| 132 | |
| 133 | def _CurrentWrapperVersion(): |
| 134 | VERSION = None |
| 135 | pat = re.compile(r'^VERSION *=') |
| 136 | fd = open(_MyWrapperPath()) |
| 137 | for line in fd: |
| 138 | if pat.match(line): |
| 139 | fd.close() |
| 140 | exec line |
| 141 | return VERSION |
| 142 | raise NameError, 'No VERSION in repo script' |
| 143 | |
| 144 | def _CheckWrapperVersion(ver, repo_path): |
| 145 | if not repo_path: |
| 146 | repo_path = '~/bin/repo' |
| 147 | |
| 148 | if not ver: |
| 149 | print >>sys.stderr, 'no --wrapper-version argument' |
| 150 | sys.exit(1) |
| 151 | |
| 152 | exp = _CurrentWrapperVersion() |
| 153 | ver = tuple(map(lambda x: int(x), ver.split('.'))) |
| 154 | if len(ver) == 1: |
| 155 | ver = (0, ver[0]) |
| 156 | |
| 157 | if exp[0] > ver[0] or ver < (0, 4): |
| 158 | exp_str = '.'.join(map(lambda x: str(x), exp)) |
| 159 | print >>sys.stderr, """ |
| 160 | !!! A new repo command (%5s) is available. !!! |
| 161 | !!! You must upgrade before you can continue: !!! |
| 162 | |
| 163 | cp %s %s |
| 164 | """ % (exp_str, _MyWrapperPath(), repo_path) |
| 165 | sys.exit(1) |
| 166 | |
| 167 | if exp > ver: |
| 168 | exp_str = '.'.join(map(lambda x: str(x), exp)) |
| 169 | print >>sys.stderr, """ |
| 170 | ... A new repo command (%5s) is available. |
| 171 | ... You should upgrade soon: |
| 172 | |
| 173 | cp %s %s |
| 174 | """ % (exp_str, _MyWrapperPath(), repo_path) |
| 175 | |
| 176 | def _CheckRepoDir(dir): |
| 177 | if not dir: |
| 178 | print >>sys.stderr, 'no --repo-dir argument' |
| 179 | sys.exit(1) |
| 180 | |
| 181 | def _PruneOptions(argv, opt): |
| 182 | i = 0 |
| 183 | while i < len(argv): |
| 184 | a = argv[i] |
| 185 | if a == '--': |
| 186 | break |
| 187 | if a.startswith('--'): |
| 188 | eq = a.find('=') |
| 189 | if eq > 0: |
| 190 | a = a[0:eq] |
| 191 | if not opt.has_option(a): |
| 192 | del argv[i] |
| 193 | continue |
| 194 | i += 1 |
| 195 | |
| 196 | def _Main(argv): |
| 197 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 198 | opt.add_option("--repo-dir", dest="repodir", |
| 199 | help="path to .repo/") |
| 200 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 201 | help="version of the wrapper script") |
| 202 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 203 | help="location of the wrapper script") |
| 204 | _PruneOptions(argv, opt) |
| 205 | opt, argv = opt.parse_args(argv) |
| 206 | |
| 207 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 208 | _CheckRepoDir(opt.repodir) |
| 209 | |
| 210 | repo = _Repo(opt.repodir) |
| 211 | try: |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 212 | try: |
| 213 | repo._Run(argv) |
| 214 | finally: |
| 215 | close_ssh() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 216 | except KeyboardInterrupt: |
| 217 | sys.exit(1) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 218 | except RepoChangedException, rce: |
| 219 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 221 | argv = list(sys.argv) |
| 222 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 223 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 224 | os.execv(__file__, argv) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 225 | except OSError, e: |
| 226 | print >>sys.stderr, 'fatal: cannot restart repo after upgrade' |
| 227 | print >>sys.stderr, 'fatal: %s' % e |
| 228 | sys.exit(128) |
| 229 | |
| 230 | if __name__ == '__main__': |
| 231 | _Main(sys.argv[1:]) |