repo 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. # Copyright 2020 The Chromium OS 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. """Wrapper around repo to auto-update depot_tools during sync.
  6. gclient keeps depot_tools up-to-date automatically for Chromium developers.
  7. We only want to do this on `repo sync` operations as that implies a network
  8. operation, and update_depot_tools is not cheap.
  9. """
  10. import os
  11. from pathlib import Path
  12. import subprocess
  13. import sys
  14. import gclient_utils
  15. # Some useful paths.
  16. DEPOT_TOOLS_DIR = Path(__file__).resolve().parent
  17. UPDATE_DEPOT_TOOLS = DEPOT_TOOLS_DIR / 'update_depot_tools'
  18. REPO = DEPOT_TOOLS_DIR / 'repo_launcher'
  19. def _UpdateDepotTools():
  20. """Help CrOS users keep their depot_tools checkouts up-to-date."""
  21. if os.getenv('DEPOT_TOOLS_UPDATE') == '0':
  22. return
  23. # We don't update the copy that's part of the CrOS repo client checkout.
  24. path = DEPOT_TOOLS_DIR
  25. while path != path.parent:
  26. if (path / '.repo').is_dir() and (path / 'chromite').is_dir():
  27. return
  28. if (path / '.citc').is_dir():
  29. return
  30. path = path.parent
  31. if UPDATE_DEPOT_TOOLS.exists():
  32. subprocess.run([UPDATE_DEPOT_TOOLS], check=True)
  33. else:
  34. print(
  35. f'warning: {UPDATE_DEPOT_TOOLS} does not exist; export '
  36. 'DEPOT_TOOLS_UPDATE=0 to disable.',
  37. file=sys.stderr)
  38. def main(argv):
  39. if gclient_utils.IsEnvCog():
  40. print('"repo" is not supported in non-git environment.',
  41. file=sys.stderr)
  42. return 1
  43. # This is a bit hacky, but should be "good enough". If repo itself gains
  44. # support for sync hooks, we could switch to that.
  45. if argv and argv[0] == 'sync':
  46. _UpdateDepotTools()
  47. os.execv(sys.executable, [sys.executable, str(REPO)] + argv)
  48. if __name__ == '__main__':
  49. sys.exit(main(sys.argv[1:]))