Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

lint.py 2.4 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...