OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Wrapper for the chromite shell. | 6 """Wrapper for the chromite shell. |
7 | 7 |
8 This script is intended to run in several ways: | 8 This script is intended to run in several ways: |
9 - Outside the chroot, it should be _copied_ to someplace that is in the | 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 | 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. | 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 | 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 | 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 | 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 | 15 real 'chromite/shell/main.py' based on the environment variable |
16 CROS_WORKON_SRCROOT, so it doesn't matter what the CWD is. | 16 CROS_WORKON_SRCROOT, so it doesn't matter what the CWD is. |
17 | 17 |
18 If you're looking at a copy and want to know where the original looks at, look | 18 If you're looking at a copy and want to know where the original looks at, look |
19 here: | 19 here: |
20 http://git.chromium.org/gitweb/?p=chromite.git;a=blob;f=bin/chromite | 20 http://git.chromium.org/gitweb/?p=chromite.git;a=blob;f=bin/chromite |
21 | 21 |
22 Since this script is _copied_, it should remain small and not use internal libs. | 22 Since this script is _copied_, it should remain small and not use internal libs. |
23 """ | 23 """ |
24 | 24 |
25 # Python imports. | 25 # Python imports. |
26 import os | 26 import os |
27 import sys | 27 import sys |
28 | 28 |
29 | |
30 def ReportChromiteError(message): | |
diandersAtChromium
2011/02/02 23:15:28
nit: Should probably start w/ _, since this is pri
| |
31 print >>sys.stderr, ( | |
32 "ERROR: Couldn't find the chromite tool.\n\n" + message + | |
33 "\n\nFor details and help, see:\n" | |
34 " http://www.chromium.org/chromium-os/developer-guide" | |
35 ) | |
36 | |
37 | |
38 def TryChromiteShell(dir_): | |
diandersAtChromium
2011/02/02 23:15:28
nit: Should probably start w/ _, since this is pri
| |
39 sys.path[1] = dir_ | |
40 try: | |
41 import chromite.shell.main | |
42 return chromite.shell.main.main | |
43 except ImportError: | |
44 return None | |
45 | |
46 | |
29 if __name__ == '__main__': | 47 if __name__ == '__main__': |
30 # Look relative to CROS_WORKON_SRCROOT if that variable exists. This is | 48 sys.path.insert(1, None) |
diandersAtChromium
2011/02/02 23:15:28
A few issues here:
1. We don't need to insert any
| |
31 # the "inside the chroot" case. | 49 # Look relative to CROS_WORKON_SRCROOT if that variable exists. |
50 # Inside the chroot, this variable should always be set; outside | |
51 # the setting is optional. | |
32 if 'CROS_WORKON_SRCROOT' in os.environ: | 52 if 'CROS_WORKON_SRCROOT' in os.environ: |
33 chromite_path = os.path.join(os.environ['CROS_WORKON_SRCROOT'], | 53 main_function = TryChromiteShell(os.environ['CROS_WORKON_SRCROOT']) |
34 'chromite', 'shell', 'main.py') | 54 if main_function is None: |
35 if os.path.isfile(chromite_path): | 55 ReportChromiteError( |
36 # Exec the script, which will never return. | 56 "There is no chromite package in the path indicated by\n" |
37 os.execv(chromite_path, sys.argv) | 57 "CROS_WORKON_SRCROOT. The variable may be set wrong, or you\n" |
58 "may need to update your chroot.") | |
59 sys.exit(1) | |
60 else: | |
61 # We're outside the chroot and CROS_WORKON_SRCROOT isn't set. | |
62 # Search upward until the parent dir doesn't change (on Linux, | |
63 # that means we're at '/'). | |
64 dir_ = os.getcwd() | |
65 prev_dir = None | |
66 while dir_ != prev_dir: | |
67 main_function = TryChromiteShell(dir_) | |
68 if main_function is not None: | |
69 break | |
70 prev_dir = dir_ | |
71 dir_ = os.path.dirname(dir_) | |
38 else: | 72 else: |
39 print ( | 73 ReportChromiteError( |
40 "ERROR: Couldn't find the chromite tool.\n" | 74 "Please change to a directory inside your Chromium OS source tree\n" |
41 "\n" | 75 "and retry.") |
42 "You may need to update your chroot. If you need help, see:\n" | |
43 " http://www.chromium.org/chromium-os/developer-guide" | |
44 ) | |
45 sys.exit(1) | 76 sys.exit(1) |
46 | 77 |
47 # Outside the chroot, search upward until the "parent" dir doesn't change | 78 main_function() |
diandersAtChromium
2011/02/02 23:15:28
Please indent by 2 spaces so this is in the __name
| |
48 # (on Linux, that means we're at '/'). That is an error case. | 79 sys.exit(0) |
diandersAtChromium
2011/02/02 23:15:28
Why is sys.exit needed?
| |
49 dir = os.getcwd() | |
50 prev_dir = None | |
51 while dir != prev_dir: | |
52 chromite_path = os.path.join(dir, 'chromite', 'shell', 'main.py') | |
53 if os.path.isfile(chromite_path): | |
54 # Add the directory above chromite to PYTHONPATH before executing, so | |
55 # that "import chromite.abc.xyz" works... | |
56 env = os.environ.copy() | |
57 if 'PYTHONPATH' in env: | |
58 env['PYTHONPATH'] += ':%s' % dir | |
59 else: | |
60 env['PYTHONPATH'] = dir | |
61 | |
62 # Exec the script, which will never return. | |
63 os.execve(chromite_path, sys.argv, env) | |
64 | |
65 prev_dir = dir | |
66 dir = os.path.dirname(dir) | |
67 | |
68 # TODO(dianders): Should we actually print out the 'repo init' call that | |
69 # the user should use? | |
70 print ( | |
71 "ERROR: Couldn't find the chromite tool.\n" | |
72 "\n" | |
73 "Please change to a directory inside your Chromium OS source tree\n" | |
74 "and retry. If you need to setup a Chromium OS source tree, see:\n" | |
75 " http://www.chromium.org/chromium-os/developer-guide" | |
76 ) | |
77 sys.exit(1) | |
OLD | NEW |