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