1. import argparse
  2. import configparser
  3. import sys
  4. def compare_configs(config_files, flag1, flag2):
  5. """Compares values in configuration files based on optional flags."""
  6. if not config_files:
  7. print("Error: No configuration files provided.")
  8. sys.exit(1)
  9. config_values = {}
  10. for file_path in config_files:
  11. try:
  12. config = configparser.ConfigParser()
  13. config.read(file_path)
  14. config_values[file_path] = config
  15. except Exception as e:
  16. print(f"Error reading {file_path}: {e}")
  17. sys.exit(1)
  18. if flag1 and flag2:
  19. # Compare values based on flags
  20. if not compare_values_with_flags(config_values, flag1, flag2):
  21. print("Configuration values differ based on flags.")
  22. return False # Indicate a difference
  23. else:
  24. print("Configuration values match based on flags.")
  25. return True # Indicate no difference
  26. else:
  27. # Compare all values
  28. if not compare_all_values(config_values):
  29. print("Configuration values differ.")
  30. return False # Indicate a difference
  31. else:
  32. print("Configuration values match.")
  33. return True # Indicate no difference
  34. def compare_values_with_flags(config_values, flag1, flag2):
  35. """Compares values based on specified flags."""
  36. if flag1 == "cpu_threshold" and flag2 == "memory_limit":
  37. #Example comparison
  38. try:
  39. threshold1 = float(config_values[list(config_values.keys())[0]].get("cpu_threshold", "0.0"))
  40. limit1 = int(config_values[list(config_values.keys())[1]].get("memory_limit", "1024"))
  41. if threshold1 > 0.8 and limit1 < 512:
  42. return False
  43. except ValueError:
  44. print("Invalid numeric value in config files.")
  45. return False
  46. return True
  47. else:
  48. print("Invalid flag combination.")
  49. return False
  50. def compare_all_values(config_values):
  51. """Compares all values across configuration files."""
  52. first_config = config_values[list(config_values.keys())[0]]
  53. for section in first_config:
  54. for key in first_config[section]:
  55. value = first_config[section][key]
  56. is_consistent = True
  57. for file_path in config_values:
  58. try:
  59. config = configparser.ConfigParser()
  60. config.read(file_path)
  61. if section in config and key in config[section]:
  62. if config[section][key] != value:
  63. is_consistent = False
  64. break
  65. else:
  66. is_consistent = False
  67. break
  68. except Exception as e:
  69. print(f"Error reading {file_path}: {e}")
  70. return False
  71. if not is_consistent:
  72. return False
  73. return True
  74. def main():
  75. parser = argparse.ArgumentParser(description="Compare configuration files.")
  76. parser.add_argument("config_files", nargs="+", help="Paths to configuration files.")
  77. parser.add_argument("--cpu_threshold", action="store_true", help="Compare CPU threshold values.")
  78. parser.add_argument("--memory_limit", action="store_true", help="Compare memory limit values.")
  79. args = parser.parse_args()
  80. compare_configs(args.config_files, args.cpu_threshold, args.memory_limit)
  81. if __name__ == "__main__":
  82. main()

Add your comment