utils.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. """ A collection of commonly used functions across depot_tools.
  5. """
  6. import logging
  7. import os
  8. import re
  9. import subprocess
  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 normpath(path):
  27. '''Version of os.path.normpath that also changes backward slashes to
  28. forward slashes when not running on Windows.
  29. '''
  30. # This is safe to always do because the Windows version of os.path.normpath
  31. # will replace forward slashes with backward slashes.
  32. path = path.replace(os.sep, '/')
  33. return os.path.normpath(path)
  34. def ListRelevantFilesInSourceCheckout(files, root, match_re, exclude_re):
  35. """Finds all files that apply to a given set of source files, e.g. PRESUBMIT.
  36. If inherit-review-settings-ok is present right under root, looks for matches
  37. in directories enclosing root.
  38. Args:
  39. files: An iterable container containing file paths.
  40. root: Path where to stop searching.
  41. match_re: regex to match filename
  42. exclude_re: regex to exclude filename
  43. Return:
  44. List of absolute paths of the existing PRESUBMIT.py scripts.
  45. """
  46. files = [normpath(os.path.join(root, f)) for f in files]
  47. # List all the individual directories containing files.
  48. directories = {os.path.dirname(f) for f in files}
  49. # Ignore root if inherit-review-settings-ok is present.
  50. if os.path.isfile(os.path.join(root, 'inherit-review-settings-ok')):
  51. root = None
  52. # Collect all unique directories that may contain PRESUBMIT.py.
  53. candidates = set()
  54. for directory in directories:
  55. while True:
  56. if directory in candidates:
  57. break
  58. candidates.add(directory)
  59. if directory == root:
  60. break
  61. parent_dir = os.path.dirname(directory)
  62. if parent_dir == directory:
  63. # We hit the system root directory.
  64. break
  65. directory = parent_dir
  66. # Look for PRESUBMIT.py in all candidate directories.
  67. results = []
  68. for directory in sorted(list(candidates)):
  69. try:
  70. for f in os.listdir(directory):
  71. p = os.path.join(directory, f)
  72. if os.path.isfile(p) and re.match(match_re,
  73. f) and not re.match(exclude_re, f):
  74. results.append(p)
  75. except OSError:
  76. pass
  77. logging.debug('Presubmit files: %s', ','.join(results))
  78. return results