mcgrathr | 3ffcc86 | 2015-10-27 07:07:51 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2011 The Chromium 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 | """Small utility function to find depot_tools and add it to the python path. |
| 6 | |
| 7 | Will throw an ImportError exception if depot_tools can't be found since it |
| 8 | imports breakpad. |
| 9 | |
| 10 | This can also be used as a standalone script to print out the depot_tools |
| 11 | directory location. |
| 12 | """ |
| 13 | |
| 14 | import os |
| 15 | import sys |
| 16 | |
| 17 | |
Dan Jacques | dbf5f8d | 2017-06-03 08:59:16 +0900 | [diff] [blame] | 18 | # Path to //src |
| 19 | SRC = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) |
| 20 | |
| 21 | |
mcgrathr | 3ffcc86 | 2015-10-27 07:07:51 +0900 | [diff] [blame] | 22 | def IsRealDepotTools(path): |
cco3 | ffc2dd3 | 2017-05-23 02:03:27 +0900 | [diff] [blame] | 23 | expanded_path = os.path.expanduser(path) |
| 24 | return os.path.isfile(os.path.join(expanded_path, 'gclient.py')) |
mcgrathr | 3ffcc86 | 2015-10-27 07:07:51 +0900 | [diff] [blame] | 25 | |
| 26 | |
| 27 | def add_depot_tools_to_path(): |
| 28 | """Search for depot_tools and add it to sys.path.""" |
Dan Jacques | dbf5f8d | 2017-06-03 08:59:16 +0900 | [diff] [blame] | 29 | # First, check if we have a DEPS'd in "depot_tools". |
| 30 | deps_depot_tools = os.path.join(SRC, 'third_party', 'depot_tools') |
| 31 | if IsRealDepotTools(deps_depot_tools): |
Mike Bjorge | 9783fdc | 2017-10-11 06:36:39 +0900 | [diff] [blame] | 32 | # Put the pinned version at the start of the sys.path, in case there |
| 33 | # are other non-pinned versions already on the sys.path. |
| 34 | sys.path.insert(0, deps_depot_tools) |
Dan Jacques | dbf5f8d | 2017-06-03 08:59:16 +0900 | [diff] [blame] | 35 | return deps_depot_tools |
| 36 | |
| 37 | # Then look if depot_tools is already in PYTHONPATH. |
mcgrathr | 3ffcc86 | 2015-10-27 07:07:51 +0900 | [diff] [blame] | 38 | for i in sys.path: |
| 39 | if i.rstrip(os.sep).endswith('depot_tools') and IsRealDepotTools(i): |
| 40 | return i |
| 41 | # Then look if depot_tools is in PATH, common case. |
| 42 | for i in os.environ['PATH'].split(os.pathsep): |
| 43 | if IsRealDepotTools(i): |
| 44 | sys.path.append(i.rstrip(os.sep)) |
| 45 | return i |
| 46 | # Rare case, it's not even in PATH, look upward up to root. |
| 47 | root_dir = os.path.dirname(os.path.abspath(__file__)) |
| 48 | previous_dir = os.path.abspath(__file__) |
| 49 | while root_dir and root_dir != previous_dir: |
| 50 | i = os.path.join(root_dir, 'depot_tools') |
| 51 | if IsRealDepotTools(i): |
| 52 | sys.path.append(i) |
| 53 | return i |
| 54 | previous_dir = root_dir |
| 55 | root_dir = os.path.dirname(root_dir) |
| 56 | print >> sys.stderr, 'Failed to find depot_tools' |
| 57 | return None |
| 58 | |
| 59 | DEPOT_TOOLS_PATH = add_depot_tools_to_path() |
| 60 | |
| 61 | # pylint: disable=W0611 |
| 62 | import breakpad |
| 63 | |
| 64 | |
| 65 | def main(): |
| 66 | if DEPOT_TOOLS_PATH is None: |
| 67 | return 1 |
| 68 | print DEPOT_TOOLS_PATH |
| 69 | return 0 |
| 70 | |
| 71 | |
| 72 | if __name__ == '__main__': |
| 73 | sys.exit(main()) |