146 lines
5.7 KiB
Python
146 lines
5.7 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import re
|
|
import sys
|
|
from collections import defaultdict
|
|
|
|
def extract_paths(file_path):
|
|
"""Extrait tous les chemins du fichier rapport_template.md"""
|
|
paths = []
|
|
try:
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
for line in f:
|
|
# Extraire les lignes qui commencent par "Corpus/"
|
|
if line.strip().startswith("Corpus/"):
|
|
paths.append(line.strip())
|
|
except Exception as e:
|
|
print(f"Erreur lors de la lecture du fichier {file_path}: {e}")
|
|
sys.exit(1)
|
|
|
|
return paths
|
|
|
|
def check_paths(paths, base_dir):
|
|
"""Vérifie si les chemins existent dans le système de fichiers"""
|
|
results = {
|
|
"existing": [],
|
|
"missing": [],
|
|
"problematic": [] # Chemins qui pourraient nécessiter des corrections
|
|
}
|
|
|
|
for path in paths:
|
|
# Vérifier si le chemin est absolu ou relatif
|
|
abs_path = os.path.join(base_dir, path)
|
|
|
|
if os.path.exists(abs_path):
|
|
results["existing"].append(path)
|
|
else:
|
|
# Essayer de détecter des problèmes potentiels
|
|
problem_detected = False
|
|
|
|
# Vérifier les chemins avec "Fiche minerai" ou "Fiche fabrication"
|
|
if "Fiche minerai" in path or "Fiche fabrication" in path:
|
|
# Problème courant: mauvaise casse ou absence du mot "minerai"
|
|
path_lower = path.lower()
|
|
if "minerai" not in path_lower and "/minerai/" in path_lower:
|
|
corrected_path = path.replace("/Fiche ", "/Fiche minerai ")
|
|
if os.path.exists(os.path.join(base_dir, corrected_path)):
|
|
results["problematic"].append((path, corrected_path, "Mot 'minerai' manquant"))
|
|
problem_detected = True
|
|
|
|
# Vérifier les chemins SSD
|
|
if "SSD25" in path:
|
|
corrected_path = path.replace("SSD25", "SSD 2.5")
|
|
if os.path.exists(os.path.join(base_dir, corrected_path)):
|
|
results["problematic"].append((path, corrected_path, "Format 'SSD25' au lieu de 'SSD 2.5'"))
|
|
problem_detected = True
|
|
|
|
# Si aucun problème spécifique n'a été détecté, marquer comme manquant
|
|
if not problem_detected:
|
|
results["missing"].append(path)
|
|
|
|
return results
|
|
|
|
def find_similar_paths(missing_path, base_dir):
|
|
"""Essaie de trouver des chemins similaires pour aider à diagnostiquer le problème"""
|
|
missing_parts = missing_path.split('/')
|
|
similar_paths = []
|
|
|
|
# Rechercher dans les sous-répertoires correspondants
|
|
search_dir = os.path.join(base_dir, *missing_parts[:-1])
|
|
if os.path.exists(search_dir):
|
|
for file in os.listdir(search_dir):
|
|
if file.endswith('.md'):
|
|
similar_path = os.path.join(search_dir, file).replace(base_dir + '/', '')
|
|
similar_paths.append(similar_path)
|
|
|
|
# Si aucun chemin similaire n'est trouvé, remonter d'un niveau
|
|
if not similar_paths and len(missing_parts) > 2:
|
|
parent_dir = os.path.join(base_dir, *missing_parts[:-2])
|
|
if os.path.exists(parent_dir):
|
|
for dir_name in os.listdir(parent_dir):
|
|
if dir_name.lower() in missing_parts[-2].lower():
|
|
dir_path = os.path.join(parent_dir, dir_name)
|
|
if os.path.isdir(dir_path):
|
|
for file in os.listdir(dir_path):
|
|
if file.endswith('.md'):
|
|
similar_path = os.path.join(dir_path, file).replace(base_dir + '/', '')
|
|
similar_paths.append(similar_path)
|
|
|
|
return similar_paths
|
|
|
|
def main():
|
|
# Vérifier que nous sommes dans le bon répertoire
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
base_dir = script_dir
|
|
|
|
# Chemin vers le rapport_template.md
|
|
template_path = os.path.join(base_dir, "Corpus", "rapport_template.md")
|
|
|
|
if not os.path.exists(template_path):
|
|
print(f"Erreur: Le fichier {template_path} n'existe pas.")
|
|
sys.exit(1)
|
|
|
|
print("=== Vérification des chemins dans rapport_template.md ===")
|
|
|
|
# Extraire les chemins
|
|
paths = extract_paths(template_path)
|
|
print(f"Nombre total de chemins trouvés: {len(paths)}")
|
|
|
|
# Vérifier les chemins
|
|
results = check_paths(paths, base_dir)
|
|
|
|
# Afficher les résultats
|
|
print("\n=== Résultats ===")
|
|
print(f"Chemins existants: {len(results['existing'])}")
|
|
print(f"Chemins manquants: {len(results['missing'])}")
|
|
print(f"Chemins problématiques: {len(results['problematic'])}")
|
|
|
|
# Afficher les chemins manquants
|
|
if results["missing"]:
|
|
print("\n=== Chemins manquants ===")
|
|
for path in results["missing"]:
|
|
print(f"- {path}")
|
|
similar = find_similar_paths(path, base_dir)
|
|
if similar:
|
|
print(" Chemins similaires trouvés:")
|
|
for sim_path in similar[:3]: # Limiter à 3 suggestions
|
|
print(f" * {sim_path}")
|
|
|
|
# Afficher les chemins problématiques avec suggestions
|
|
if results["problematic"]:
|
|
print("\n=== Chemins problématiques ===")
|
|
for orig, corrected, reason in results["problematic"]:
|
|
print(f"- {orig}")
|
|
print(f" Suggestion: {corrected}")
|
|
print(f" Raison: {reason}")
|
|
|
|
# Résumé
|
|
if not results["missing"] and not results["problematic"]:
|
|
print("\nTous les chemins dans le rapport sont valides !")
|
|
else:
|
|
print("\nDes chemins problématiques ont été détectés. Veuillez corriger les erreurs.")
|
|
|
|
if __name__ == "__main__":
|
|
main() |