66 lines
2 KiB
Python
66 lines
2 KiB
Python
import re
|
|
import subprocess
|
|
import sys
|
|
|
|
from src.errors import ClangError
|
|
from src.utils import analyze_args
|
|
|
|
|
|
def clang_format(file_path) -> str:
|
|
command = ["clang-format", "--dry-run", file_path]
|
|
try:
|
|
output = subprocess.check_output(command, stderr=subprocess.STDOUT, text=True)
|
|
except subprocess.CalledProcessError as e:
|
|
output = e.output
|
|
except FileNotFoundError as e:
|
|
print(f"Commande non trouvé : {e.filename}", file=sys.stderr)
|
|
print(
|
|
"clang est requis pour utilisé ce programme, installé-le via "
|
|
f"votre gestionnaire de paquet : {e.filename}",
|
|
file=sys.stderr,
|
|
)
|
|
exit(1)
|
|
|
|
return output
|
|
|
|
|
|
def parse_clang_format_output(output) -> list[ClangError]:
|
|
error_pattern = (
|
|
r"(?P<filename>.+):(?P<line_number>\d+):(?P<column_number>\d+):"
|
|
r" warning: code should be clang-formatted \[(?P<warning_message>.+)\]"
|
|
)
|
|
error_matches = re.finditer(error_pattern, output)
|
|
|
|
errors = []
|
|
for error_match in error_matches:
|
|
filename = str(error_match.group("filename"))
|
|
line_number = int(error_match.group("line_number"))
|
|
column_number = int(error_match.group("column_number"))
|
|
warning_message = str(error_match.group("warning_message"))
|
|
|
|
errors.append(
|
|
ClangError(
|
|
filename,
|
|
line_number,
|
|
column_number,
|
|
warning_message[1:],
|
|
)
|
|
)
|
|
|
|
return errors
|
|
|
|
|
|
if __name__ == "__main__":
|
|
files = analyze_args(sys.argv[1:])
|
|
for file in files:
|
|
clang_format_output = clang_format(file)
|
|
parsed_output = parse_clang_format_output(clang_format_output)
|
|
|
|
if parsed_output:
|
|
for error in parsed_output:
|
|
print(
|
|
f"[{error.warning_message}] Avertissement dans {error.filename} "
|
|
f"à la ligne {error.line_number}, caractère {error.column_number}."
|
|
)
|
|
else:
|
|
print(f"Aucun avertissement trouvé dans {file}.")
|