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 |
Mike Lockwood | 33f0e78 | 2009-07-14 15:23:39 -0400 | [diff] [blame] | 62 | # add 'branch' as an alias for 'branches' |
| 63 | all_commands['branch'] = all_commands['branches'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 82 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 83 | SetTrace() |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 84 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 91 | 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 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 101 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
| 102 | print >>sys.stderr, \ |
| 103 | "fatal: '%s' requires a working directory"\ |
| 104 | % name |
| 105 | sys.exit(1) |
| 106 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 107 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 108 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | if not gopts.no_pager and not isinstance(cmd, InteractiveCommand): |
| 110 | config = cmd.manifest.globalConfig |
| 111 | if gopts.pager: |
| 112 | use_pager = True |
| 113 | else: |
| 114 | use_pager = config.GetBoolean('pager.%s' % name) |
| 115 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 116 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 117 | if use_pager: |
| 118 | RunPager(config) |
| 119 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 120 | try: |
| 121 | cmd.Execute(copts, cargs) |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 122 | except ManifestInvalidRevisionError, e: |
| 123 | print >>sys.stderr, 'error: %s' % str(e) |
| 124 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 125 | except NoSuchProjectError, e: |
| 126 | if e.name: |
| 127 | print >>sys.stderr, 'error: project %s not found' % e.name |
| 128 | else: |
| 129 | print >>sys.stderr, 'error: no project in current directory' |
| 130 | sys.exit(1) |
| 131 | |
| 132 | def _MyWrapperPath(): |
| 133 | return os.path.join(os.path.dirname(__file__), 'repo') |
| 134 | |
| 135 | def _CurrentWrapperVersion(): |
| 136 | VERSION = None |
| 137 | pat = re.compile(r'^VERSION *=') |
| 138 | fd = open(_MyWrapperPath()) |
| 139 | for line in fd: |
| 140 | if pat.match(line): |
| 141 | fd.close() |
| 142 | exec line |
| 143 | return VERSION |
| 144 | raise NameError, 'No VERSION in repo script' |
| 145 | |
| 146 | def _CheckWrapperVersion(ver, repo_path): |
| 147 | if not repo_path: |
| 148 | repo_path = '~/bin/repo' |
| 149 | |
| 150 | if not ver: |
| 151 | print >>sys.stderr, 'no --wrapper-version argument' |
| 152 | sys.exit(1) |
| 153 | |
| 154 | exp = _CurrentWrapperVersion() |
| 155 | ver = tuple(map(lambda x: int(x), ver.split('.'))) |
| 156 | if len(ver) == 1: |
| 157 | ver = (0, ver[0]) |
| 158 | |
| 159 | if exp[0] > ver[0] or ver < (0, 4): |
| 160 | exp_str = '.'.join(map(lambda x: str(x), exp)) |
| 161 | print >>sys.stderr, """ |
| 162 | !!! A new repo command (%5s) is available. !!! |
| 163 | !!! You must upgrade before you can continue: !!! |
| 164 | |
| 165 | cp %s %s |
| 166 | """ % (exp_str, _MyWrapperPath(), repo_path) |
| 167 | sys.exit(1) |
| 168 | |
| 169 | if exp > ver: |
| 170 | exp_str = '.'.join(map(lambda x: str(x), exp)) |
| 171 | print >>sys.stderr, """ |
| 172 | ... A new repo command (%5s) is available. |
| 173 | ... You should upgrade soon: |
| 174 | |
| 175 | cp %s %s |
| 176 | """ % (exp_str, _MyWrapperPath(), repo_path) |
| 177 | |
| 178 | def _CheckRepoDir(dir): |
| 179 | if not dir: |
| 180 | print >>sys.stderr, 'no --repo-dir argument' |
| 181 | sys.exit(1) |
| 182 | |
| 183 | def _PruneOptions(argv, opt): |
| 184 | i = 0 |
| 185 | while i < len(argv): |
| 186 | a = argv[i] |
| 187 | if a == '--': |
| 188 | break |
| 189 | if a.startswith('--'): |
| 190 | eq = a.find('=') |
| 191 | if eq > 0: |
| 192 | a = a[0:eq] |
| 193 | if not opt.has_option(a): |
| 194 | del argv[i] |
| 195 | continue |
| 196 | i += 1 |
| 197 | |
| 198 | def _Main(argv): |
| 199 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 200 | opt.add_option("--repo-dir", dest="repodir", |
| 201 | help="path to .repo/") |
| 202 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 203 | help="version of the wrapper script") |
| 204 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 205 | help="location of the wrapper script") |
| 206 | _PruneOptions(argv, opt) |
| 207 | opt, argv = opt.parse_args(argv) |
| 208 | |
| 209 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 210 | _CheckRepoDir(opt.repodir) |
| 211 | |
| 212 | repo = _Repo(opt.repodir) |
| 213 | try: |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 214 | try: |
| 215 | repo._Run(argv) |
| 216 | finally: |
| 217 | close_ssh() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | except KeyboardInterrupt: |
| 219 | sys.exit(1) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 220 | except RepoChangedException, rce: |
| 221 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 222 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 223 | argv = list(sys.argv) |
| 224 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 225 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 226 | os.execv(__file__, argv) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 227 | except OSError, e: |
| 228 | print >>sys.stderr, 'fatal: cannot restart repo after upgrade' |
| 229 | print >>sys.stderr, 'fatal: %s' % e |
| 230 | sys.exit(128) |
| 231 | |
| 232 | if __name__ == '__main__': |
| 233 | _Main(sys.argv[1:]) |