Add directory support + error handling
This commit is contained in:
parent
ecf6289da3
commit
f04e89cf3f
2 changed files with 68 additions and 29 deletions
19
main.py
19
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,14 +24,16 @@ 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:
|
||||||
|
return []
|
||||||
|
|
||||||
errors = []
|
errors = []
|
||||||
for error_match in error_matches:
|
for error_match in error_matches:
|
||||||
filename = error_match.group("filename")
|
filename = error_match.group("filename")
|
||||||
|
@ -50,18 +52,19 @@ def parse_clang_format_output(output) -> Union[None, list[ClangError]]:
|
||||||
|
|
||||||
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:])
|
||||||
|
for file in files:
|
||||||
|
clang_format_output = clang_format(file)
|
||||||
parsed_output = parse_clang_format_output(clang_format_output)
|
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"à la ligne {error.line_number}, "
|
||||||
f"caractère {error.column_number} : {error.warning_message}"
|
f"caractère {error.column_number} : {error.warning_message}"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print("Aucun avertissement trouvé.")
|
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