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