Add directory support + error handling
This commit is contained in:
parent
ecf6289da3
commit
f04e89cf3f
2 changed files with 68 additions and 29 deletions
61
main.py
61
main.py
|
@ -1,9 +1,9 @@
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
from src.errors import ClangError
|
from src.errors import ClangError
|
||||||
|
from src.utils import analyze_args
|
||||||
|
|
||||||
|
|
||||||
def clang_format(file_path):
|
def clang_format(file_path):
|
||||||
|
@ -24,44 +24,47 @@ def clang_format(file_path):
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def parse_clang_format_output(output) -> Union[None, list[ClangError]]:
|
def parse_clang_format_output(output) -> list[ClangError]:
|
||||||
error_pattern = (
|
error_pattern = (
|
||||||
r"(?P<filename>.+):(?P<line_number>\d+):(?P<column_number>\d+):"
|
r"(?P<filename>.+):(?P<line_number>\d+):(?P<column_number>\d+):"
|
||||||
r" warning: code should be clang-formatted \[(?P<warning_message>.+)\]"
|
r" warning: code should be clang-formatted \[(?P<warning_message>.+)\]"
|
||||||
)
|
)
|
||||||
error_matches = re.finditer(error_pattern, output)
|
error_matches = re.finditer(error_pattern, output)
|
||||||
|
|
||||||
if error_matches:
|
if not error_matches:
|
||||||
errors = []
|
return []
|
||||||
for error_match in error_matches:
|
|
||||||
filename = error_match.group("filename")
|
|
||||||
line_number = int(error_match.group("line_number"))
|
|
||||||
column_number = int(error_match.group("column_number"))
|
|
||||||
warning_message = error_match.group("warning_message")
|
|
||||||
|
|
||||||
errors.append(
|
errors = []
|
||||||
ClangError(
|
for error_match in error_matches:
|
||||||
filename,
|
filename = error_match.group("filename")
|
||||||
line_number,
|
line_number = int(error_match.group("line_number"))
|
||||||
column_number,
|
column_number = int(error_match.group("column_number"))
|
||||||
warning_message[1:],
|
warning_message = error_match.group("warning_message")
|
||||||
)
|
|
||||||
|
errors.append(
|
||||||
|
ClangError(
|
||||||
|
filename,
|
||||||
|
line_number,
|
||||||
|
column_number,
|
||||||
|
warning_message[1:],
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
clang_format_output = clang_format("tests/1.c")
|
files = analyze_args(sys.argv[1:])
|
||||||
parsed_output = parse_clang_format_output(clang_format_output)
|
for file in files:
|
||||||
|
clang_format_output = clang_format(file)
|
||||||
|
parsed_output = parse_clang_format_output(clang_format_output)
|
||||||
|
|
||||||
if parsed_output:
|
if parsed_output:
|
||||||
for error in parsed_output:
|
for error in parsed_output:
|
||||||
print(
|
print(
|
||||||
f"Avertissement dans {error.filename} à la ligne {error.line_number},"
|
f"Avertissement dans {error.filename} "
|
||||||
f" caractère {error.column_number} : {error.warning_message}"
|
f"à la ligne {error.line_number}, "
|
||||||
)
|
f"caractère {error.column_number} : {error.warning_message}"
|
||||||
else:
|
)
|
||||||
print("Aucun avertissement trouvé.")
|
else:
|
||||||
|
print(f"Aucun avertissement trouvé dans {file}.")
|
||||||
|
|
36
src/utils.py
Normal file
36
src/utils.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def analyze_args(args: list):
|
||||||
|
"""Renvoie la liste des fichiers"""
|
||||||
|
if len(args) > 1:
|
||||||
|
print("Trop d'arguments renseignés.", file=sys.stderr)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
if os.path.isdir(args[0]):
|
||||||
|
args = iterate_over_directory(args[0])
|
||||||
|
else:
|
||||||
|
if not list(filter(lambda file: os.path.exists(file), args)):
|
||||||
|
print("Le fichier n'existe pas.", file=sys.stderr)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
files = list(filter(check_ext, args))
|
||||||
|
|
||||||
|
if not files:
|
||||||
|
print(f"Aucun fichiers supporté ({', '.join(args)}).", file=sys.stderr)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
return files
|
||||||
|
|
||||||
|
|
||||||
|
def check_ext(filename: str):
|
||||||
|
"""Vérifie qu'un fichier est supporté en se basant sur son extension"""
|
||||||
|
supported_exts = ".c"
|
||||||
|
|
||||||
|
return filename.endswith(supported_exts)
|
||||||
|
|
||||||
|
|
||||||
|
def iterate_over_directory(dirname: str):
|
||||||
|
"""Trouve tout les fichiers d'un dossier"""
|
||||||
|
return next(os.walk(dirname))[2]
|
Reference in a new issue