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 | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 35 | from editor import Editor |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 36 | from error import ManifestInvalidRevisionError |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | from error import NoSuchProjectError |
| 38 | from error import RepoChangedException |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame^] | 39 | from manifest_xml import XmlManifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | from pager import RunPager |
| 41 | |
| 42 | from subcmds import all as all_commands |
| 43 | |
| 44 | global_options = optparse.OptionParser( |
| 45 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" |
| 46 | ) |
| 47 | global_options.add_option('-p', '--paginate', |
| 48 | dest='pager', action='store_true', |
| 49 | help='display command output in the pager') |
| 50 | global_options.add_option('--no-pager', |
| 51 | dest='no_pager', action='store_true', |
| 52 | help='disable the pager') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 53 | global_options.add_option('--trace', |
| 54 | dest='trace', action='store_true', |
| 55 | help='trace git command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 56 | global_options.add_option('--version', |
| 57 | dest='show_version', action='store_true', |
| 58 | help='display this version of repo') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 59 | |
| 60 | class _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. 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 |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame^] | 100 | cmd.manifest = XmlManifest(cmd.repodir) |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 101 | Editor.globalConfig = cmd.manifest.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 102 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 103 | 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. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 109 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 110 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | 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. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 118 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | if use_pager: |
| 120 | RunPager(config) |
| 121 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 122 | try: |
| 123 | cmd.Execute(copts, cargs) |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 124 | except ManifestInvalidRevisionError, e: |
| 125 | print >>sys.stderr, 'error: %s' % str(e) |
| 126 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | 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 | |
| 134 | def _MyWrapperPath(): |
| 135 | return os.path.join(os.path.dirname(__file__), 'repo') |
| 136 | |
| 137 | def _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 | |
| 148 | def _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 | |
| 180 | def _CheckRepoDir(dir): |
| 181 | if not dir: |
| 182 | print >>sys.stderr, 'no --repo-dir argument' |
| 183 | sys.exit(1) |
| 184 | |
| 185 | def _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 | |
| 200 | def _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. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 216 | try: |
| 217 | repo._Run(argv) |
| 218 | finally: |
| 219 | close_ssh() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | except KeyboardInterrupt: |
| 221 | sys.exit(1) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 222 | except RepoChangedException, rce: |
| 223 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 224 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 225 | argv = list(sys.argv) |
| 226 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 227 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 228 | os.execv(__file__, argv) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 229 | except OSError, e: |
| 230 | print >>sys.stderr, 'fatal: cannot restart repo after upgrade' |
| 231 | print >>sys.stderr, 'fatal: %s' % e |
| 232 | sys.exit(128) |
| 233 | |
| 234 | if __name__ == '__main__': |
| 235 | _Main(sys.argv[1:]) |