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 | |||||
14 | import os | ||||
15 | import sys | ||||
16 | |||||
[email protected] | 7601617 | 2014-01-22 09:31:40 | [diff] [blame] | 17 | |
18 | def IsRealDepotTools(path): | ||||
jamesr | 380a535 | 2014-10-23 00:02:37 | [diff] [blame] | 19 | return os.path.isfile(os.path.join(path, 'gclient.py')) |
[email protected] | 7601617 | 2014-01-22 09:31:40 | [diff] [blame] | 20 | |
21 | |||||
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 22 | def add_depot_tools_to_path(): |
23 | """Search for depot_tools and add it to sys.path.""" | ||||
24 | # First look if depot_tools is already in PYTHONPATH. | ||||
25 | for i in sys.path: | ||||
[email protected] | 7601617 | 2014-01-22 09:31:40 | [diff] [blame] | 26 | if i.rstrip(os.sep).endswith('depot_tools') and IsRealDepotTools(i): |
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 27 | return i |
28 | # Then look if depot_tools is in PATH, common case. | ||||
29 | for i in os.environ['PATH'].split(os.pathsep): | ||||
[email protected] | 80a6b9c | 2014-03-04 21:17:12 | [diff] [blame] | 30 | if IsRealDepotTools(i): |
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 31 | sys.path.append(i.rstrip(os.sep)) |
32 | return i | ||||
33 | # Rare case, it's not even in PATH, look upward up to root. | ||||
34 | root_dir = os.path.dirname(os.path.abspath(__file__)) | ||||
[email protected] | bec7de4 | 2011-08-18 18:52:19 | [diff] [blame] | 35 | previous_dir = os.path.abspath(__file__) |
36 | while root_dir and root_dir != previous_dir: | ||||
[email protected] | 7601617 | 2014-01-22 09:31:40 | [diff] [blame] | 37 | i = os.path.join(root_dir, 'depot_tools') |
38 | if IsRealDepotTools(i): | ||||
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 39 | sys.path.append(i) |
40 | return i | ||||
[email protected] | bec7de4 | 2011-08-18 18:52:19 | [diff] [blame] | 41 | previous_dir = root_dir |
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 42 | root_dir = os.path.dirname(root_dir) |
43 | print >> sys.stderr, 'Failed to find depot_tools' | ||||
44 | return None | ||||
45 | |||||
mcgrathr | b729cfa | 2015-10-26 22:07:51 | [diff] [blame] | 46 | DEPOT_TOOLS_PATH = add_depot_tools_to_path() |
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 47 | |
48 | # pylint: disable=W0611 | ||||
49 | import breakpad | ||||
mcgrathr | b729cfa | 2015-10-26 22:07:51 | [diff] [blame] | 50 | |
51 | |||||
52 | def main(): | ||||
53 | if DEPOT_TOOLS_PATH is None: | ||||
54 | return 1 | ||||
55 | print DEPOT_TOOLS_PATH | ||||
56 | return 0 | ||||
57 | |||||
58 | |||||
59 | if __name__ == '__main__': | ||||
60 | sys.exit(main()) |