find_depot_tools.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. # for py2/py3 compatibility
  7. from __future__ import print_function
  8. import os
  9. import sys
  10. def directory_really_is_depot_tools(directory):
  11. return os.path.isfile(os.path.join(directory, 'gclient.py'))
  12. def add_depot_tools_to_path():
  13. """Search for depot_tools and add it to sys.path."""
  14. # First look if depot_tools is already in PYTHONPATH.
  15. for i in sys.path:
  16. if i.rstrip(os.sep).endswith('depot_tools'):
  17. if directory_really_is_depot_tools(i):
  18. return i
  19. # Then look if depot_tools is in PATH, common case.
  20. for i in os.environ['PATH'].split(os.pathsep):
  21. if i.rstrip(os.sep).endswith('depot_tools'):
  22. if directory_really_is_depot_tools(i):
  23. sys.path.insert(0, i.rstrip(os.sep))
  24. return i
  25. # Rare case, it's not even in PATH, look upward up to root.
  26. root_dir = os.path.dirname(os.path.abspath(__file__))
  27. previous_dir = os.path.abspath(__file__)
  28. while root_dir and root_dir != previous_dir:
  29. if directory_really_is_depot_tools(os.path.join(root_dir, 'depot_tools')):
  30. i = os.path.join(root_dir, 'depot_tools')
  31. sys.path.insert(0, i)
  32. return i
  33. previous_dir = root_dir
  34. root_dir = os.path.dirname(root_dir)
  35. print('Failed to find depot_tools', file=sys.stderr)
  36. return None