[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 | # Use of this source code is governed by a BSD-style license that can be | ||||
3 | # found in the LICENSE file. | ||||
4 | """Small utility function to find depot_tools and add it to the python path. | ||||
5 | |||||
6 | Will throw an ImportError exception if depot_tools can't be found since it | ||||
7 | imports breakpad. | ||||
8 | """ | ||||
9 | |||||
10 | import os | ||||
11 | import sys | ||||
12 | |||||
[email protected] | 7601617 | 2014-01-22 09:31:40 | [diff] [blame] | 13 | |
14 | def IsRealDepotTools(path): | ||||
jamesr | 380a535 | 2014-10-23 00:02:37 | [diff] [blame] | 15 | return os.path.isfile(os.path.join(path, 'gclient.py')) |
[email protected] | 7601617 | 2014-01-22 09:31:40 | [diff] [blame] | 16 | |
17 | |||||
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 18 | def add_depot_tools_to_path(): |
19 | """Search for depot_tools and add it to sys.path.""" | ||||
20 | # First look if depot_tools is already in PYTHONPATH. | ||||
21 | for i in sys.path: | ||||
[email protected] | 7601617 | 2014-01-22 09:31:40 | [diff] [blame] | 22 | if i.rstrip(os.sep).endswith('depot_tools') and IsRealDepotTools(i): |
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 23 | return i |
24 | # Then look if depot_tools is in PATH, common case. | ||||
25 | for i in os.environ['PATH'].split(os.pathsep): | ||||
[email protected] | 80a6b9c | 2014-03-04 21:17:12 | [diff] [blame] | 26 | if IsRealDepotTools(i): |
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 27 | sys.path.append(i.rstrip(os.sep)) |
28 | return i | ||||
29 | # Rare case, it's not even in PATH, look upward up to root. | ||||
30 | root_dir = os.path.dirname(os.path.abspath(__file__)) | ||||
[email protected] | bec7de4 | 2011-08-18 18:52:19 | [diff] [blame] | 31 | previous_dir = os.path.abspath(__file__) |
32 | while root_dir and root_dir != previous_dir: | ||||
[email protected] | 7601617 | 2014-01-22 09:31:40 | [diff] [blame] | 33 | i = os.path.join(root_dir, 'depot_tools') |
34 | if IsRealDepotTools(i): | ||||
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 35 | sys.path.append(i) |
36 | return i | ||||
[email protected] | bec7de4 | 2011-08-18 18:52:19 | [diff] [blame] | 37 | previous_dir = root_dir |
[email protected] | bd205639 | 2011-07-25 21:12:34 | [diff] [blame] | 38 | root_dir = os.path.dirname(root_dir) |
39 | print >> sys.stderr, 'Failed to find depot_tools' | ||||
40 | return None | ||||
41 | |||||
42 | add_depot_tools_to_path() | ||||
43 | |||||
44 | # pylint: disable=W0611 | ||||
45 | import breakpad |