utils.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright 2022 The Chromium 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. import os
  5. import pathlib
  6. import shutil
  7. import subprocess
  8. import sys
  9. DEPOT_TOOLS_ROOT = os.path.dirname(os.path.abspath(__file__))
  10. def depot_tools_version():
  11. depot_tools_root = os.path.dirname(os.path.abspath(__file__))
  12. try:
  13. commit_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'],
  14. cwd=depot_tools_root).decode(
  15. 'utf-8', 'ignore')
  16. return 'git-%s' % commit_hash
  17. except Exception:
  18. pass
  19. # git check failed, let's check last modification of frequently checked file
  20. try:
  21. mtime = os.path.getmtime(
  22. os.path.join(depot_tools_root, 'infra', 'config', 'recipes.cfg'))
  23. return 'recipes.cfg-%d' % (mtime)
  24. except Exception:
  25. return 'unknown'
  26. def depot_tools_config_dir():
  27. # Use depot tools path for mac, windows.
  28. if not sys.platform.startswith('linux'):
  29. return DEPOT_TOOLS_ROOT
  30. # Use $XDG_CONFIG_HOME/depot_tools or $HOME/.config/depot_tools on linux.
  31. config_root = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
  32. return os.path.join(config_root, 'depot_tools')
  33. def depot_tools_config_path(file):
  34. config_dir = depot_tools_config_dir()
  35. expected_path = os.path.join(config_dir, file)
  36. # Silently create config dir if necessary.
  37. pathlib.Path(config_dir).mkdir(parents=True, exist_ok=True)
  38. # Silently migrate cfg from legacy path if it exists.
  39. if not os.path.isfile(expected_path):
  40. legacy_path = os.path.join(DEPOT_TOOLS_ROOT, file)
  41. if os.path.isfile(legacy_path):
  42. shutil.move(legacy_path, expected_path)
  43. return expected_path