-
Notifications
You must be signed in to change notification settings - Fork 0
/
autopep.py
87 lines (67 loc) · 2.25 KB
/
autopep.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
import os
import subprocess
import logging
from typing import List
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
logging.basicConfig(
level=logging.INFO,
format='[%(levelname)s %(asctime)s %(filename)s]: %(message)s:')
def get_all_python_files(directory: Path) -> List[Path]:
"""
Get all Python files within a given directory and its subdirectories.
Args:
directory (Path): The directory to search for Python files.
Returns:
List[Path]: A list of file paths to Python files.
"""
return list(directory.rglob('*.py'))
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:
logging.error(f"Command '{command}' not found: {e}")
raise e
def run_autopep8(file: Path) -> None:
"""
Run autopep8 on a Python file.
Args:
file (Path): Path to the Python file to format.
"""
autopep8_process = execute_command(
['autopep8', '--in-place', '--aggressive', '--aggressive', str(file)])
if autopep8_process.returncode != 0:
logging.warning(autopep8_process.stdout)
def lint_python_file(file: Path) -> None:
"""
Lint a Python file using autopep8.
Args:
file (Path): Path to the Python file to lint.
"""
run_autopep8(file)
def main():
"""
Main function to lint all Python files in the project directory.
"""
all_python_files = get_all_python_files(Path(os.getcwd()))
with ThreadPoolExecutor() as executor:
future_to_file = {
executor.submit(
lint_python_file,
file): file for file in all_python_files}
for future in as_completed(future_to_file):
file = future_to_file[future]
try:
future.result()
except Exception as e:
logging.error(f"Error linting file {file}: {e}")
if __name__ == "__main__":
main()