Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env 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 chromite tools. | 6 """Wrapper for chromite tools. |
| 7 | 7 |
| 8 The script is intend to be symlinked to any number of chromite tools, attempts | 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 | 9 to find the path for chromite, and hands off to the right tool via exec if |
| 10 possible. | 10 possible. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 import errno | 22 import errno |
| 23 import os | 23 import os |
| 24 import sys | 24 import sys |
| 25 | 25 |
| 26 # Due to historical reasons, and the fact depot_tools ToT is used by older | 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 | 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 | 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 | 29 # fallback code- any/all new scripts symlinked to this script *must* exist |
| 30 # in chromite/bin/ . | 30 # in chromite/bin/ . |
| 31 | 31 |
| 32 def _FindRoot(path): | 32 def _FindChromite(path): |
| 33 """Find the root of a repo checkout""" | 33 """Find the chromite dir in a repo or gclient checkout.""" |
| 34 path = os.path.abspath(path) | 34 path = os.path.abspath(path) |
| 35 # Depending on the checkout type (whether repo chromeos or gclient chrome) | |
| 36 # Chromite lives in a different location. | |
| 37 roots = { | |
| 38 '.repo': 'chromite/.git', | |
|
davidjames
2013/02/07 00:40:41
Should these be in a list instead of a dictionary
| |
| 39 '.gclient': 'src/third_party/chromite/.git' | |
| 40 } | |
| 41 | |
| 35 while path != '/': | 42 while path != '/': |
| 36 # Look for the chromite repository itself- it's always been at the root | 43 for root, chromite_git_dir in roots.iteritems(): |
| 37 # of a repo checkout. | 44 if all(os.path.exists(os.path.join(path, x)) |
| 38 if all(os.path.exists(os.path.join(path, x)) | 45 for x in [root, chromite_git_dir]): |
| 39 for x in ['.repo', 'chromite/.git']): | 46 return os.path.dirname(os.path.join(path, chromite_git_dir)) |
| 40 return path | |
| 41 path = os.path.dirname(path) | 47 path = os.path.dirname(path) |
| 42 return None | 48 return None |
| 43 | 49 |
| 44 | 50 |
| 45 def _MissingErrorOut(target): | 51 def _MissingErrorOut(target): |
| 46 sys.stderr.write( | 52 sys.stderr.write( |
| 47 """ERROR: Couldn't find the chromite tool %s. | 53 """ERROR: Couldn't find the chromite tool %s. |
| 48 | 54 |
| 49 Please change to a directory inside your Chromium OS source tree | 55 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 | 56 and retry. If you need to setup a Chromium OS source tree, see |
| 51 http://www.chromium.org/chromium-os/developer-guide | 57 http://www.chromium.org/chromium-os/developer-guide |
| 52 """ % target) | 58 """ % target) |
| 53 return 127 | 59 return 127 |
| 54 | 60 |
| 55 | 61 |
| 56 def main(): | 62 def main(): |
| 57 root = _FindRoot(os.getcwd()) | 63 chromite_dir = _FindChromite(os.getcwd()) |
| 58 target = os.path.basename(sys.argv[0]) | 64 target = os.path.basename(sys.argv[0]) |
| 59 if root is None: | 65 if chromite_dir is None: |
| 60 return _MissingErrorOut(target) | 66 return _MissingErrorOut(target) |
| 61 | 67 |
| 62 path = os.path.join(root, 'chromite/bin', target) | 68 path = os.path.join(chromite_dir, 'bin', target) |
| 63 try: | 69 try: |
| 64 os.execv(path, [path] + sys.argv[1:]) | 70 os.execv(path, [path] + sys.argv[1:]) |
| 65 except EnvironmentError, e: | 71 except EnvironmentError, e: |
| 66 if e.errno not in (errno.ENOENT, errno.EPERM): | 72 if e.errno not in (errno.ENOENT, errno.EPERM): |
| 67 raise | 73 raise |
| 68 | 74 |
| 69 # Reaching here means it's either a bad target, or we're working against | 75 # Reaching here means it's either a bad target, or we're working against |
| 70 # an old (pre 6be2efcf5bb575b03862113eec097c44d8d7f93e) revision of | 76 # an old (pre 6be2efcf5bb575b03862113eec097c44d8d7f93e) revision of |
| 71 # chromite. Fallback to trying to import it; this code works at least as | 77 # chromite. Fallback to trying to import it; this code works at least as |
| 72 # far back as branch 0.11.241.B; likely further. | 78 # far back as branch 0.11.241.B; likely further. |
| 73 | 79 |
| 74 if target == 'cbuildbot': | 80 if target == 'cbuildbot': |
| 75 target = 'chromite.buildbot.cbuildbot' | 81 target = 'chromite.buildbot.cbuildbot' |
| 76 else: | 82 else: |
| 77 target = 'chromite.bin.%s' % (target,) | 83 target = 'chromite.bin.%s' % (target,) |
| 78 | 84 |
| 79 # Adjust the path importation so we can import our our target. | 85 # Adjust the path importation so we can import our our target. |
| 80 sys.path.insert(0, root) | 86 sys.path.insert(0, os.path.dirname(chromite_dir)) |
| 81 | 87 |
| 82 try: | 88 try: |
| 83 module = __import__(target, fromlist=['main']) | 89 module = __import__(target, fromlist=['main']) |
| 84 except ImportError: | 90 except ImportError: |
| 85 return _MissingErrorOut(target) | 91 return _MissingErrorOut(target) |
| 86 return module.main() | 92 return module.main() |
| 87 | 93 |
| 88 if __name__ == '__main__': | 94 if __name__ == '__main__': |
| 89 sys.exit(main()) | 95 sys.exit(main()) |
| OLD | NEW |