scan.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. # Copyright 2023 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. import argparse
  6. from collections import defaultdict
  7. import os
  8. import sys
  9. _THIS_DIR = os.path.abspath(os.path.dirname(__file__))
  10. # The repo's root directory.
  11. _ROOT_DIR = os.path.abspath(os.path.join(_THIS_DIR, ".."))
  12. # Add the repo's root directory for clearer imports.
  13. sys.path.insert(0, _ROOT_DIR)
  14. import metadata.discover
  15. import metadata.validate
  16. def parse_args() -> argparse.Namespace:
  17. """Helper to parse args to this script."""
  18. parser = argparse.ArgumentParser()
  19. repo_root_dir = parser.add_argument(
  20. "repo_root_dir",
  21. help=("The path to the repository's root directory, which will be "
  22. "scanned for Chromium metadata files, e.g. '~/chromium/src'."),
  23. )
  24. args = parser.parse_args()
  25. # Check the repo root directory exists.
  26. src_dir = os.path.abspath(args.repo_root_dir)
  27. if not os.path.exists(src_dir) or not os.path.isdir(src_dir):
  28. raise argparse.ArgumentError(
  29. repo_root_dir,
  30. f"Invalid repository root directory '{src_dir}' - not found",
  31. )
  32. return args
  33. def main() -> None:
  34. """Runs validation on all metadata files within the directory
  35. specified by the repo_root_dir arg.
  36. """
  37. config = parse_args()
  38. src_dir = os.path.abspath(config.repo_root_dir)
  39. metadata_files = metadata.discover.find_metadata_files(src_dir)
  40. file_count = len(metadata_files)
  41. print(f"Found {file_count} metadata files.")
  42. invalid_file_count = 0
  43. # Key is constructed from the result severity and reason;
  44. # Value is a list of files affected by that reason at that severity.
  45. all_reasons = defaultdict(list)
  46. for filepath in metadata_files:
  47. file_results = metadata.validate.validate_file(filepath,
  48. repo_root_dir=src_dir)
  49. invalid = False
  50. if file_results:
  51. relpath = os.path.relpath(filepath, start=src_dir)
  52. print(f"\n{len(file_results)} problem(s) in {relpath}:")
  53. for result in file_results:
  54. print(f" {result}")
  55. summary_key = "{severity} - {reason}".format(
  56. severity=result.get_severity_prefix(),
  57. reason=result.get_reason())
  58. all_reasons[summary_key].append(relpath)
  59. if result.is_fatal():
  60. invalid = True
  61. if invalid:
  62. invalid_file_count += 1
  63. print("\n\nDone.\nSummary:")
  64. for summary_key, affected_files in all_reasons.items():
  65. count = len(affected_files)
  66. plural = "s" if count > 1 else ""
  67. print(f"\n {count} file{plural}: {summary_key}")
  68. for affected_file in affected_files:
  69. print(f" {affected_file}")
  70. print(f"\n\n{invalid_file_count} / {file_count} metadata files are "
  71. "invalid, i.e. the file has at least one fatal validation issue.")
  72. if __name__ == "__main__":
  73. main()