OLD | NEW |
(Empty) | |
| 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 runs the right tool based on argv[0]. |
| 10 |
| 11 It is intended to run in several ways: |
| 12 - Outside the chroot, it should be _copied_ to someplace that is in the |
| 13 path (like depot_tools). It will search for the right chromite by looking |
| 14 for a file 'chromite/shell/main.py' upward based on the CWD. |
| 15 - Inside the chroot, it might be _either_ copied to someplace in the path (since |
| 16 depot_tools is in the path in the chroot) or it might run from chromite/bin |
| 17 directly, which should be in the PATH. In any case, we'll look for the |
| 18 real 'chromite/shell/main.py' based on the environment variable |
| 19 CROS_WORKON_SRCROOT, so it doesn't matter what the CWD is. |
| 20 |
| 21 If you're looking at a copy and want to know where the original looks at, look |
| 22 here: |
| 23 http://git.chromium.org/gitweb/?p=chromite.git;a=blob;f=bin/chromite |
| 24 |
| 25 Since this script is _copied_, it should remain small and not use internal libs. |
| 26 |
| 27 """ |
| 28 |
| 29 import os |
| 30 import sys |
| 31 |
| 32 |
| 33 def Search(path): |
| 34 """Return an iterator of lists of places to look for chromite.""" |
| 35 |
| 36 if os.path.exists('/etc/debian_chroot'): |
| 37 # We're in the chroot. Chromite should be in the python path inside the |
| 38 # chroot, so we don't do any searching. NOTE that we purposely don't want |
| 39 # CROS_WORKON_SRCROOT in the python path. |
| 40 yield [] |
| 41 else: |
| 42 # Look in $CROS_WORKON_SRCROOT first. The idea is that a user would set |
| 43 # this manually if they wanted to specify a particular version of chromite. |
| 44 if 'CROS_WORKON_SRCROOT' in os.environ: |
| 45 yield [os.environ['CROS_WORKON_SRCROOT']] |
| 46 |
| 47 # Search upward until we either end up with a blank dir or the "parent" dir |
| 48 # doesn't change. |
| 49 prev_path = None |
| 50 while path and path != prev_path: |
| 51 yield [path] |
| 52 path, prev_path = os.path.dirname(path), path |
| 53 |
| 54 |
| 55 def main(): |
| 56 # Decide what the name of the called binary will be. |
| 57 our_name = os.path.basename(sys.argv[0]) |
| 58 # Some tools map to an arbitrary path, rather than to a .py file of the same |
| 59 # exact name. By default, we pick the tool from chromite/bin/ |
| 60 TOOLS_MAP = { |
| 61 'chromite' : 'shell.main', |
| 62 'cbuildbot' : 'buildbot.cbuildbot', |
| 63 } |
| 64 our_name = TOOLS_MAP.get(our_name, 'bin.' + our_name) |
| 65 |
| 66 for path in Search(os.getcwd()): |
| 67 sys.path = path + sys.path |
| 68 try: |
| 69 exec 'import chromite.' + our_name + ' as our_tool' |
| 70 break |
| 71 except ImportError, e: |
| 72 # Just in case there is actually something wrong with Chromite, print |
| 73 # a sensible error. We match only the end of the string so that we can |
| 74 # handle an error within the chromite directory. |
| 75 # The full error is 'No module named (chromite.)shell.main' |
| 76 # Note: If you hit the directory containing chromite on the way up, then |
| 77 # the error will be 'No module named shell.main' so we must check only the |
| 78 # shell.main part. |
| 79 if not str(e).endswith(our_name): |
| 80 raise |
| 81 |
| 82 # We've got different modules named chromite in the tree, pulling in the |
| 83 # wrong one will break the right one. So unload it. |
| 84 if 'chromite' in sys.modules: |
| 85 del sys.modules['chromite'] |
| 86 sys.path = sys.path[len(path):] |
| 87 else: |
| 88 # TODO(dianders): Should we actually print out the 'repo init' call that |
| 89 # the user should use? |
| 90 sys.stderr.write( |
| 91 "ERROR: Couldn't find the chromite tool.\n" |
| 92 "\n" |
| 93 "Please change to a directory inside your Chromium OS source tree\n" |
| 94 "and retry. If you need to setup a Chromium OS source tree, see:\n" |
| 95 " http://www.chromium.org/chromium-os/developer-guide\n") |
| 96 return 1 |
| 97 return our_tool.main() |
| 98 |
| 99 |
| 100 if __name__ == '__main__': |
| 101 sys.exit(main()) |
OLD | NEW |