compare_bots.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python3
  2. """
  3. Script to verify that the 'bots' field in data/botPolicies.yaml
  4. has the same semantic contents as data/meta/default-config.yaml.
  5. CW: generated by AI
  6. """
  7. import yaml
  8. import sys
  9. import os
  10. import subprocess
  11. import difflib
  12. def load_yaml(file_path):
  13. """Load YAML file and return the data."""
  14. try:
  15. with open(file_path, 'r') as f:
  16. return yaml.safe_load(f)
  17. except Exception as e:
  18. print(f"Error loading {file_path}: {e}")
  19. sys.exit(1)
  20. def normalize_yaml(data):
  21. """Normalize YAML data by removing comments and standardizing structure."""
  22. # For lists, just return as is, since YAML comments are stripped by safe_load
  23. return data
  24. def get_repo_root():
  25. """Get the root directory of the git repository."""
  26. try:
  27. result = subprocess.run(['git', 'rev-parse', '--show-toplevel'], capture_output=True, text=True, check=True)
  28. return result.stdout.strip()
  29. except subprocess.CalledProcessError:
  30. print("Error: Not in a git repository")
  31. sys.exit(1)
  32. def main():
  33. # Get the git repository root
  34. repo_root = get_repo_root()
  35. # Paths relative to the repo root
  36. bot_policies_path = os.path.join(repo_root, 'data', 'botPolicies.yaml')
  37. default_config_path = os.path.join(repo_root, 'data', 'meta', 'default-config.yaml')
  38. # Load the files
  39. bot_policies = load_yaml(bot_policies_path)
  40. default_config = load_yaml(default_config_path)
  41. # Extract the 'bots' field from botPolicies.yaml
  42. if 'bots' not in bot_policies:
  43. print("Error: 'bots' field not found in botPolicies.yaml")
  44. sys.exit(1)
  45. bots_field = bot_policies['bots']
  46. # The default-config.yaml is a list directly
  47. default_bots = default_config
  48. # Normalize both
  49. normalized_bots = normalize_yaml(bots_field)
  50. normalized_default = normalize_yaml(default_bots)
  51. # Compare
  52. if normalized_bots == normalized_default:
  53. print("SUCCESS: The 'bots' field in botPolicies.yaml matches the contents of default-config.yaml")
  54. sys.exit(0)
  55. else:
  56. print("FAILURE: The 'bots' field in botPolicies.yaml does not match the contents of default-config.yaml")
  57. print("\nDiff:")
  58. bots_yaml = yaml.dump(normalized_bots, default_flow_style=False)
  59. default_yaml = yaml.dump(normalized_default, default_flow_style=False)
  60. diff = difflib.unified_diff(
  61. bots_yaml.splitlines(keepends=True),
  62. default_yaml.splitlines(keepends=True),
  63. fromfile='bots field in botPolicies.yaml',
  64. tofile='default-config.yaml'
  65. )
  66. print(''.join(diff))
  67. sys.exit(1)
  68. if __name__ == "__main__":
  69. main()