• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 the V8 project 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
7# for py2/py3 compatibility
8from __future__ import print_function
9
10import os
11import sys
12
13
14def directory_really_is_depot_tools(directory):
15  return os.path.isfile(os.path.join(directory, 'gclient.py'))
16
17
18def 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:
22    if i.rstrip(os.sep).endswith('depot_tools'):
23      if directory_really_is_depot_tools(i):
24        return i
25
26  # Then look if depot_tools is in PATH, common case.
27  for i in os.environ['PATH'].split(os.pathsep):
28    if i.rstrip(os.sep).endswith('depot_tools'):
29      if directory_really_is_depot_tools(i):
30        sys.path.insert(0, i.rstrip(os.sep))
31        return i
32  # Rare case, it's not even in PATH, look upward up to root.
33  root_dir = os.path.dirname(os.path.abspath(__file__))
34  previous_dir = os.path.abspath(__file__)
35  while root_dir and root_dir != previous_dir:
36    if directory_really_is_depot_tools(os.path.join(root_dir, 'depot_tools')):
37      i = os.path.join(root_dir, 'depot_tools')
38      sys.path.insert(0, i)
39      return i
40    previous_dir = root_dir
41    root_dir = os.path.dirname(root_dir)
42  print('Failed to find depot_tools', file=sys.stderr)
43  return None
44