xref: /aosp_15_r20/external/angle/build/find_depot_tools.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*8975f5c5SAndroid Build Coastguard Worker# Copyright 2011 The Chromium Authors
3*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker"""Small utility function to find depot_tools and add it to the python path.
6*8975f5c5SAndroid Build Coastguard Worker
7*8975f5c5SAndroid Build Coastguard WorkerWill throw an ImportError exception if depot_tools can't be found since it
8*8975f5c5SAndroid Build Coastguard Workerimports breakpad.
9*8975f5c5SAndroid Build Coastguard Worker
10*8975f5c5SAndroid Build Coastguard WorkerThis can also be used as a standalone script to print out the depot_tools
11*8975f5c5SAndroid Build Coastguard Workerdirectory location.
12*8975f5c5SAndroid Build Coastguard Worker"""
13*8975f5c5SAndroid Build Coastguard Worker
14*8975f5c5SAndroid Build Coastguard Worker
15*8975f5c5SAndroid Build Coastguard Workerimport os
16*8975f5c5SAndroid Build Coastguard Workerimport sys
17*8975f5c5SAndroid Build Coastguard Worker
18*8975f5c5SAndroid Build Coastguard Worker
19*8975f5c5SAndroid Build Coastguard Worker# Path to //src
20*8975f5c5SAndroid Build Coastguard WorkerSRC = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
21*8975f5c5SAndroid Build Coastguard Worker
22*8975f5c5SAndroid Build Coastguard Worker
23*8975f5c5SAndroid Build Coastguard Workerdef IsRealDepotTools(path):
24*8975f5c5SAndroid Build Coastguard Worker  expanded_path = os.path.expanduser(path)
25*8975f5c5SAndroid Build Coastguard Worker  return os.path.isfile(os.path.join(expanded_path, 'gclient.py'))
26*8975f5c5SAndroid Build Coastguard Worker
27*8975f5c5SAndroid Build Coastguard Worker
28*8975f5c5SAndroid Build Coastguard Workerdef add_depot_tools_to_path():
29*8975f5c5SAndroid Build Coastguard Worker  """Search for depot_tools and add it to sys.path."""
30*8975f5c5SAndroid Build Coastguard Worker  # First, check if we have a DEPS'd in "depot_tools".
31*8975f5c5SAndroid Build Coastguard Worker  deps_depot_tools = os.path.join(SRC, 'third_party', 'depot_tools')
32*8975f5c5SAndroid Build Coastguard Worker  if IsRealDepotTools(deps_depot_tools):
33*8975f5c5SAndroid Build Coastguard Worker    # Put the pinned version at the start of the sys.path, in case there
34*8975f5c5SAndroid Build Coastguard Worker    # are other non-pinned versions already on the sys.path.
35*8975f5c5SAndroid Build Coastguard Worker    sys.path.insert(0, deps_depot_tools)
36*8975f5c5SAndroid Build Coastguard Worker    return deps_depot_tools
37*8975f5c5SAndroid Build Coastguard Worker
38*8975f5c5SAndroid Build Coastguard Worker  # Then look if depot_tools is already in PYTHONPATH.
39*8975f5c5SAndroid Build Coastguard Worker  for i in sys.path:
40*8975f5c5SAndroid Build Coastguard Worker    if i.rstrip(os.sep).endswith('depot_tools') and IsRealDepotTools(i):
41*8975f5c5SAndroid Build Coastguard Worker      return i
42*8975f5c5SAndroid Build Coastguard Worker  # Then look if depot_tools is in PATH, common case.
43*8975f5c5SAndroid Build Coastguard Worker  for i in os.environ['PATH'].split(os.pathsep):
44*8975f5c5SAndroid Build Coastguard Worker    if IsRealDepotTools(i):
45*8975f5c5SAndroid Build Coastguard Worker      sys.path.append(i.rstrip(os.sep))
46*8975f5c5SAndroid Build Coastguard Worker      return i
47*8975f5c5SAndroid Build Coastguard Worker  # Rare case, it's not even in PATH, look upward up to root.
48*8975f5c5SAndroid Build Coastguard Worker  root_dir = os.path.dirname(os.path.abspath(__file__))
49*8975f5c5SAndroid Build Coastguard Worker  previous_dir = os.path.abspath(__file__)
50*8975f5c5SAndroid Build Coastguard Worker  while root_dir and root_dir != previous_dir:
51*8975f5c5SAndroid Build Coastguard Worker    i = os.path.join(root_dir, 'depot_tools')
52*8975f5c5SAndroid Build Coastguard Worker    if IsRealDepotTools(i):
53*8975f5c5SAndroid Build Coastguard Worker      sys.path.append(i)
54*8975f5c5SAndroid Build Coastguard Worker      return i
55*8975f5c5SAndroid Build Coastguard Worker    previous_dir = root_dir
56*8975f5c5SAndroid Build Coastguard Worker    root_dir = os.path.dirname(root_dir)
57*8975f5c5SAndroid Build Coastguard Worker  print('Failed to find depot_tools', file=sys.stderr)
58*8975f5c5SAndroid Build Coastguard Worker  return None
59*8975f5c5SAndroid Build Coastguard Worker
60*8975f5c5SAndroid Build Coastguard WorkerDEPOT_TOOLS_PATH = add_depot_tools_to_path()
61*8975f5c5SAndroid Build Coastguard Worker
62*8975f5c5SAndroid Build Coastguard Worker# pylint: disable=W0611
63*8975f5c5SAndroid Build Coastguard Workerimport breakpad
64*8975f5c5SAndroid Build Coastguard Worker
65*8975f5c5SAndroid Build Coastguard Worker
66*8975f5c5SAndroid Build Coastguard Workerdef main():
67*8975f5c5SAndroid Build Coastguard Worker  if DEPOT_TOOLS_PATH is None:
68*8975f5c5SAndroid Build Coastguard Worker    return 1
69*8975f5c5SAndroid Build Coastguard Worker  print(DEPOT_TOOLS_PATH)
70*8975f5c5SAndroid Build Coastguard Worker  return 0
71*8975f5c5SAndroid Build Coastguard Worker
72*8975f5c5SAndroid Build Coastguard Worker
73*8975f5c5SAndroid Build Coastguard Workerif __name__ == '__main__':
74*8975f5c5SAndroid Build Coastguard Worker  sys.exit(main())
75