blob: 49a9138ec31c57eb751f3757490c9a35e165ad52 [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
Raul Tambre9e24293b2019-05-12 06:11:0714from __future__ import print_function
15
[email protected]bd2056392011-07-25 21:12:3416import os
17import sys
18
[email protected]76016172014-01-22 09:31:4019
Dan Jacquescea92c512017-06-02 23:59:1620# Path to //src
21SRC = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
22
23
[email protected]76016172014-01-22 09:31:4024def IsRealDepotTools(path):
cco3a661d6562017-05-22 17:03:2725 expanded_path = os.path.expanduser(path)
26 return os.path.isfile(os.path.join(expanded_path, 'gclient.py'))
[email protected]76016172014-01-22 09:31:4027
28
[email protected]bd2056392011-07-25 21:12:3429def add_depot_tools_to_path():
30 """Search for depot_tools and add it to sys.path."""
Dan Jacquescea92c512017-06-02 23:59:1631 # 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 Bjorge5064a4942017-10-10 21:36:3934 # 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 Jacquescea92c512017-06-02 23:59:1637 return deps_depot_tools
38
39 # Then look if depot_tools is already in PYTHONPATH.
[email protected]bd2056392011-07-25 21:12:3440 for i in sys.path:
[email protected]76016172014-01-22 09:31:4041 if i.rstrip(os.sep).endswith('depot_tools') and IsRealDepotTools(i):
[email protected]bd2056392011-07-25 21:12:3442 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]80a6b9c2014-03-04 21:17:1245 if IsRealDepotTools(i):
[email protected]bd2056392011-07-25 21:12:3446 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]bec7de42011-08-18 18:52:1950 previous_dir = os.path.abspath(__file__)
51 while root_dir and root_dir != previous_dir:
[email protected]76016172014-01-22 09:31:4052 i = os.path.join(root_dir, 'depot_tools')
53 if IsRealDepotTools(i):
[email protected]bd2056392011-07-25 21:12:3454 sys.path.append(i)
55 return i
[email protected]bec7de42011-08-18 18:52:1956 previous_dir = root_dir
[email protected]bd2056392011-07-25 21:12:3457 root_dir = os.path.dirname(root_dir)
Raul Tambre9e24293b2019-05-12 06:11:0758 print('Failed to find depot_tools', file=sys.stderr)
[email protected]bd2056392011-07-25 21:12:3459 return None
60
mcgrathrb729cfa2015-10-26 22:07:5161DEPOT_TOOLS_PATH = add_depot_tools_to_path()
[email protected]bd2056392011-07-25 21:12:3462
63# pylint: disable=W0611
64import breakpad
mcgrathrb729cfa2015-10-26 22:07:5165
66
67def main():
68 if DEPOT_TOOLS_PATH is None:
69 return 1
Raul Tambre9e24293b2019-05-12 06:11:0770 print(DEPOT_TOOLS_PATH)
mcgrathrb729cfa2015-10-26 22:07:5171 return 0
72
73
74if __name__ == '__main__':
75 sys.exit(main())