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