-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.py
88 lines (69 loc) · 2.37 KB
/
lint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import subprocess
from potholeClassifier import logger
import logging
from colorama import Fore, Style
from dotenv import load_dotenv
from typing import List
logging.basicConfig(
level=logging.INFO,
format='[%(levelname)s %(asctime)s %(filename)s]: %(message)s:')
load_dotenv()
ROOT_DIR = os.getenv("ROOT_DIR")
def get_all_python_files(directory: str) -> List[str]:
"""
Get all Python files within a given directory and its subdirectories.
Args:
directory (str): The directory to search for Python files.
Returns:
List[str]: A list of file paths to Python files.
"""
python_files = []
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.py'):
python_files.append(os.path.join(root, file))
return python_files
def execute_command(command: List[str]) -> subprocess.CompletedProcess:
"""
Execute a command in the shell.
Args:
command (List[str]): The command and its arguments to execute.
Returns:
subprocess.CompletedProcess: The completed subprocess.
"""
try:
process = subprocess.run(command, capture_output=True, text=True)
return process
except FileNotFoundError as e:
logger.error(f"Command '{command}' not found: {e}")
raise e
def lint_python_file(file: str) -> None:
"""
Lint a Python file using autopep8 and flake8.
Args:
file (str): Path to the Python file to lint.
"""
autopep8_process = execute_command(
['autopep8', '--in-place', '--aggressive', '--aggressive', file])
if autopep8_process.returncode != 0:
logger.error(f"Autopep8 failed to lint file: {file}")
logger.error(autopep8_process.stdout)
return
flake8_process = execute_command(['flake8', file])
if flake8_process.returncode != 0:
logger.warning(f"Flake8 found errors in file: {file}")
logger.warning(flake8_process.stdout)
print(f"{Fore.RED}{flake8_process.stdout}{Style.RESET_ALL}")
else:
logger.info(f"Linted file: {file}")
def main():
"""
Main function to lint all Python files in the project directory.
"""
project_directory = ROOT_DIR
all_python_files = get_all_python_files(project_directory)
for file in all_python_files:
lint_python_file(file)
if __name__ == "__main__":
main()