import sys import os import time import scripts.json_hpe as json_hpe from pathlib import Path from scripts.export_schema_infra import main as schema_infra from scripts.export_rapport_uplink import main as rapport_uplink from scripts.export_rapport_stack import main as rapport_stack from scripts.export_rapport_interfaces import main as rapport_interfaces sys.path.append(os.path.dirname(__file__)) def verify_if_file_exists(name): base, ext = os.path.splitext(name) counter = 1 new_name = name while os.path.exists(new_name): new_name = f"{base}_{counter}{ext}" counter += 1 return new_name def main(): """Fonction principale du programme d'analyse de switches""" if len(sys.argv) < 3: print("Usage: python3 src/main.py [hpe|...] [-o ] [-u] [-d] [-s] [-i]") print("Options:") print(" Type of switch to process (hpe)") print(" -o Specify output JSON file name (optional)") print(" -d Generate infrastructure schema (optional)") print(" -u Generate uplink report (optional)") print(" -s Generate stack report (optional)") print(" -i Generate interface report (optional)") sys.exit(1) switch_type = sys.argv[1].lower() input_data = sys.argv[2] input_path = "src/input/" output_path = "src/output/" os.makedirs(output_path, exist_ok=True) if "-o" in sys.argv: o_index = sys.argv.index("-o") if o_index + 1 < len(sys.argv): output_file1_json = f"{output_path}{switch_type}_saved_{sys.argv[o_index + 1]}.json" output_file2_json = f"{output_path}{switch_type}_current_{sys.argv[o_index + 1]}.json" else: print("Erreur: nom de fichier de sortie manquant après '-o'.") sys.exit(1) else: timestamp = time.strftime("%Y%m%d") output_file1_json = f"{output_path}{switch_type}_current_{timestamp}.json" output_file2_json = f"{output_path}{switch_type}_saved_{timestamp}.json" output_file1_json = verify_if_file_exists(output_file1_json) output_file2_json = verify_if_file_exists(output_file2_json) if switch_type == "hpe": json_hpe.generate_json_hpe(input_data, output_file1_json, output_file2_json) else: print("Erreur: type de switch inconnu. Utilisez 'hpe'.") sys.exit(1) if "-d" in sys.argv: print(f"\nGénération du schéma d'infrastructure...") if "-o" in sys.argv: o_index = sys.argv.index("-o") if o_index + 1 < len(sys.argv): output_file_html = os.path.join(f"{output_path}schema_infra_{switch_type}_{sys.argv[o_index + 1]}.html") else: print("Erreur: nom de fichier de sortie manquant après '-o'.") sys.exit(1) else: timestamp = time.strftime("%Y%m%d") output_file_html = f"{output_path}schema_infra_{switch_type}_{timestamp}.html" output_file_html = verify_if_file_exists(output_file_html) html_file = schema_infra(output_path, output_file_html) print(f"✓ Processus terminé. Fichiers générés:\n - JSON: {output_file1_json} & {output_file2_json}\n - HTML: {html_file}") if "-u" in sys.argv: print(f"\nGénération du rapport d'uplink...") if "-o" in sys.argv: o_index = sys.argv.index("-o") if o_index + 1 < len(sys.argv): output_file_excel = os.path.join(f"{output_path}rapport_uplink_{switch_type}_{sys.argv[o_index + 1]}.xlsx") else: print("Erreur: nom de fichier de sortie manquant après '-o'.") sys.exit(1) else: timestamp = time.strftime("%Y%m%d") output_file_excel = f"{output_path}rapport_uplink_{switch_type}_{timestamp}.xlsx" output_file_excel = verify_if_file_exists(output_file_excel) excel_file = rapport_uplink(output_path, output_file_excel) print(f"✓ Processus terminé. Fichiers générés:\n - JSON: {output_file1_json} & {output_file2_json}\n - Excel: {excel_file}") if "-s" in sys.argv: print(f"\nGénération du rapport de stack...") if "-o" in sys.argv: o_index = sys.argv.index("-o") if o_index + 1 < len(sys.argv): output_file_excel = os.path.join(f"{output_path}rapport_stack_{switch_type}_{sys.argv[o_index + 1]}.xlsx") else: print("Erreur: nom de fichier de sortie manquant après '-o'.") sys.exit(1) else: timestamp = time.strftime("%Y%m%d") output_file_excel = f"{output_path}rapport_stack_{switch_type}_{timestamp}.xlsx" output_file_excel = verify_if_file_exists(output_file_excel) excel_file = rapport_stack(output_path, output_file_excel) print(f"✓ Processus terminé. Fichiers générés:\n - JSON: {output_file1_json} & {output_file2_json}\n - Excel: {excel_file}") if "-i" in sys.argv: print(f"\nGénération du rapport d'interfaces...") if "-o" in sys.argv: o_index = sys.argv.index("-o") if o_index + 1 < len(sys.argv): output_file_excel = os.path.join(f"{output_path}rapport_interfaces_{switch_type}_{sys.argv[o_index + 1]}.xlsx") else: print("Erreur: nom de fichier de sortie manquant après '-o'.") sys.exit(1) else: timestamp = time.strftime("%Y%m%d") output_file_excel = f"{output_path}rapport_interfaces_{switch_type}_{timestamp}.xlsx" output_file_excel = verify_if_file_exists(output_file_excel) excel_file = rapport_interfaces(output_path, output_file_excel) print(f"✓ Processus terminé. Fichiers générés:\n - JSON: {output_file1_json} & {output_file2_json}\n - Excel: {excel_file}") if __name__ == "__main__": main()