blob: f891a414837a507759bb3591b0cecf2b79300d1a [file] [log] [blame]
Takuto Ikuta3dab32e02023-01-12 18:52:001#!/usr/bin/env python3
Avi Drissman73a09d12022-09-08 20:33:382# Copyright 2011 The Chromium Authors
[email protected]bd2056392011-07-25 21:12:343# 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:0714
[email protected]bd2056392011-07-25 21:12:3415import os
16import sys
17
[email protected]76016172014-01-22 09:31:4018
Dan Jacquescea92c512017-06-02 23:59:1619# Path to //src
20SRC = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
21
22
[email protected]76016172014-01-22 09:31:4023def IsRealDepotTools(path):
cco3a661d6562017-05-22 17:03:2724 expanded_path = os.path.expanduser(path)
25 return os.path.isfile(os.path.join(expanded_path, 'gclient.py'))
[email protected]76016172014-01-22 09:31:4026
27
[email protected]bd2056392011-07-25 21:12:3428def add_depot_tools_to_path():
29 """Search for depot_tools and add it to sys.path."""
Dan Jacquescea92c512017-06-02 23:59:1630 # 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 Bjorge5064a4942017-10-10 21:36:3933 # 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 Jacquescea92c512017-06-02 23:59:1636 return deps_depot_tools
37
38 # Then look if depot_tools is already in PYTHONPATH.
[email protected]bd2056392011-07-25 21:12:3439 for i in sys.path:
[email protected]76016172014-01-22 09:31:4040 if i.rstrip(os.sep).endswith('depot_tools') and IsRealDepotTools(i):
[email protected]bd2056392011-07-25 21:12:3441 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]80a6b9c2014-03-04 21:17:1244 if IsRealDepotTools(i):
[email protected]bd2056392011-07-25 21:12:3445 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]bec7de42011-08-18 18:52:1949 previous_dir = os.path.abspath(__file__)
50 while root_dir and root_dir != previous_dir:
[email protected]76016172014-01-22 09:31:4051 i = os.path.join(root_dir, 'depot_tools')
52 if IsRealDepotTools(i):
[email protected]bd2056392011-07-25 21:12:3453 sys.path.append(i)
54 return i
[email protected]bec7de42011-08-18 18:52:1955 previous_dir = root_dir
[email protected]bd2056392011-07-25 21:12:3456 root_dir = os.path.dirname(root_dir)
Raul Tambre9e24293b2019-05-12 06:11:0757 print('Failed to find depot_tools', file=sys.stderr)
[email protected]bd2056392011-07-25 21:12:3458 return None
59
mcgrathrb729cfa2015-10-26 22:07:5160DEPOT_TOOLS_PATH = add_depot_tools_to_path()
[email protected]bd2056392011-07-25 21:12:3461
62# pylint: disable=W0611
63import breakpad
mcgrathrb729cfa2015-10-26 22:07:5164
65
66def main():
67 if DEPOT_TOOLS_PATH is None:
68 return 1
Raul Tambre9e24293b2019-05-12 06:11:0769 print(DEPOT_TOOLS_PATH)
mcgrathrb729cfa2015-10-26 22:07:5170 return 0
71
72
73if __name__ == '__main__':
74 sys.exit(main())