|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 3 | +# Use of this source code is governed by a BSD-style license that can be |
| 4 | +# found in the LICENSE file. |
| 5 | + |
| 6 | +"""Wrapper for chromite tools. |
| 7 | +
|
| 8 | +The script is intend to be symlinked to any number of chromite tools, attempts |
| 9 | +to find the path for chromite, and hands off to the right tool via exec if |
| 10 | +possible. |
| 11 | +
|
| 12 | +It is intended to used strictly outside of the chroot. |
| 13 | +
|
| 14 | +If you're looking at a copy and want to know where the original looks at, look |
| 15 | +here: |
| 16 | + http://git.chromium.org/gitweb/?p=chromite.git;a=blob;f=bin/chromite |
| 17 | +
|
| 18 | +Since this script is _copied_, it should remain small and not use internal libs. |
| 19 | +
|
| 20 | +""" |
| 21 | + |
| 22 | +import errno |
| 23 | +import os |
| 24 | +import sys |
| 25 | + |
| 26 | +# Due to historical reasons, and the fact depot_tools ToT is used by older |
| 27 | +# factory branches (lacking chromite script cleanups), note we have to |
| 28 | +# fallback to some odd import locations. This is the only reason for the |
| 29 | +# fallback code- any/all new scripts symlinked to this script *must* exist |
| 30 | +# in chromite/bin/ . |
| 31 | + |
| 32 | +def _FindRoot(path): |
| 33 | + """Find the root of a repo checkout""" |
| 34 | + path = os.path.abspath(path) |
| 35 | + while path != '/': |
| 36 | + # Look for the chromite repository itself- it's always been at the root |
| 37 | + # of a repo checkout. |
| 38 | + if all(os.path.exists(os.path.join(path, x)) |
| 39 | + for x in ['.repo', 'chromite/.git']): |
| 40 | + return path |
| 41 | + path = os.path.dirname(path) |
| 42 | + return None |
| 43 | + |
| 44 | + |
| 45 | +def _MissingErrorOut(target): |
| 46 | + sys.stderr.write( |
| 47 | +"""ERROR: Couldn't find the chromite tool %s. |
| 48 | +
|
| 49 | +Please change to a directory inside your Chromium OS source tree |
| 50 | +and retry. If you need to setup a Chromium OS source tree, see |
| 51 | + http://www.chromium.org/chromium-os/developer-guide |
| 52 | +""" % target) |
| 53 | + return 127 |
| 54 | + |
| 55 | + |
| 56 | +def main(): |
| 57 | + root = _FindRoot(os.getcwd()) |
| 58 | + target = os.path.basename(sys.argv[0]) |
| 59 | + if root is None: |
| 60 | + return _MissingErrorOut(target) |
| 61 | + |
| 62 | + path = os.path.join(root, 'chromite/bin', target) |
| 63 | + try: |
| 64 | + os.execv(path, [path] + sys.argv[1:]) |
| 65 | + except EnvironmentError, e: |
| 66 | + if e.errno not in (errno.ENOENT, errno.EPERM): |
| 67 | + raise |
| 68 | + |
| 69 | + # Reaching here means it's either a bad target, or we're working against |
| 70 | + # an old (pre 6be2efcf5bb575b03862113eec097c44d8d7f93e) revision of |
| 71 | + # chromite. Fallback to trying to import it; this code works at least as |
| 72 | + # far back as branch 0.11.241.B; likely further. |
| 73 | + |
| 74 | + if target == 'cbuildbot': |
| 75 | + target = 'chromite.buildbot.cbuildbot' |
| 76 | + else: |
| 77 | + target = 'chromite.bin.%s' % (target,) |
| 78 | + |
| 79 | + # Adjust the path importation so we can import our our target. |
| 80 | + sys.path.insert(0, root) |
| 81 | + |
| 82 | + try: |
| 83 | + module = __import__(target, fromlist=['main']) |
| 84 | + except ImportError: |
| 85 | + return _MissingErrorOut(target) |
| 86 | + return module.main() |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + sys.exit(main()) |
0 commit comments