find_depot_tools.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. # 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. Will throw an ImportError exception if depot_tools can't be found since it
  7. imports breakpad.
  8. This can also be used as a standalone script to print out the depot_tools
  9. directory location.
  10. """
  11. from __future__ import print_function
  12. import os
  13. import sys
  14. # Path to //src
  15. SRC = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
  16. def IsRealDepotTools(path):
  17. expanded_path = os.path.expanduser(path)
  18. return os.path.isfile(os.path.join(expanded_path, 'gclient.py'))
  19. def add_depot_tools_to_path():
  20. """Search for depot_tools and add it to sys.path."""
  21. # First, check if we have a DEPS'd in "depot_tools".
  22. deps_depot_tools = os.path.join(SRC, 'third_party', 'depot_tools')
  23. if IsRealDepotTools(deps_depot_tools):
  24. # Put the pinned version at the start of the sys.path, in case there
  25. # are other non-pinned versions already on the sys.path.
  26. sys.path.insert(0, deps_depot_tools)
  27. return deps_depot_tools
  28. # Then look if depot_tools is already in PYTHONPATH.
  29. for i in sys.path:
  30. if i.rstrip(os.sep).endswith('depot_tools') and IsRealDepotTools(i):
  31. return i
  32. # Then look if depot_tools is in PATH, common case.
  33. for i in os.environ['PATH'].split(os.pathsep):
  34. if IsRealDepotTools(i):
  35. sys.path.append(i.rstrip(os.sep))
  36. return i
  37. # Rare case, it's not even in PATH, look upward up to root.
  38. root_dir = os.path.dirname(os.path.abspath(__file__))
  39. previous_dir = os.path.abspath(__file__)
  40. while root_dir and root_dir != previous_dir:
  41. i = os.path.join(root_dir, 'depot_tools')
  42. if IsRealDepotTools(i):
  43. sys.path.append(i)
  44. return i
  45. previous_dir = root_dir
  46. root_dir = os.path.dirname(root_dir)
  47. print('Failed to find depot_tools', file=sys.stderr)
  48. return None
  49. DEPOT_TOOLS_PATH = add_depot_tools_to_path()
  50. # pylint: disable=W0611
  51. import breakpad
  52. def main():
  53. if DEPOT_TOOLS_PATH is None:
  54. return 1
  55. print(DEPOT_TOOLS_PATH)
  56. return 0
  57. if __name__ == '__main__':
  58. sys.exit(main())