blob: 1c34fea6f6c6e1fd5aa8b7f48311502477397d42 [file] [log] [blame]
mcgrathrb729cfa2015-10-26 22:07:511#!/usr/bin/env python
[email protected]bd2056392011-07-25 21:12:342# 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
7Will throw an ImportError exception if depot_tools can't be found since it
8imports breakpad.
mcgrathrb729cfa2015-10-26 22:07:519
10This can also be used as a standalone script to print out the depot_tools
11directory location.
[email protected]bd2056392011-07-25 21:12:3412"""
13
14import os
15import sys
16
[email protected]76016172014-01-22 09:31:4017
18def IsRealDepotTools(path):
jamesr380a5352014-10-23 00:02:3719 return os.path.isfile(os.path.join(path, 'gclient.py'))
[email protected]76016172014-01-22 09:31:4020
21
[email protected]bd2056392011-07-25 21:12:3422def 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]76016172014-01-22 09:31:4026 if i.rstrip(os.sep).endswith('depot_tools') and IsRealDepotTools(i):
[email protected]bd2056392011-07-25 21:12:3427 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]80a6b9c2014-03-04 21:17:1230 if IsRealDepotTools(i):
[email protected]bd2056392011-07-25 21:12:3431 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]bec7de42011-08-18 18:52:1935 previous_dir = os.path.abspath(__file__)
36 while root_dir and root_dir != previous_dir:
[email protected]76016172014-01-22 09:31:4037 i = os.path.join(root_dir, 'depot_tools')
38 if IsRealDepotTools(i):
[email protected]bd2056392011-07-25 21:12:3439 sys.path.append(i)
40 return i
[email protected]bec7de42011-08-18 18:52:1941 previous_dir = root_dir
[email protected]bd2056392011-07-25 21:12:3442 root_dir = os.path.dirname(root_dir)
43 print >> sys.stderr, 'Failed to find depot_tools'
44 return None
45
mcgrathrb729cfa2015-10-26 22:07:5146DEPOT_TOOLS_PATH = add_depot_tools_to_path()
[email protected]bd2056392011-07-25 21:12:3447
48# pylint: disable=W0611
49import breakpad
mcgrathrb729cfa2015-10-26 22:07:5150
51
52def main():
53 if DEPOT_TOOLS_PATH is None:
54 return 1
55 print DEPOT_TOOLS_PATH
56 return 0
57
58
59if __name__ == '__main__':
60 sys.exit(main())