-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecks.py
More file actions
130 lines (102 loc) · 4.26 KB
/
checks.py
File metadata and controls
130 lines (102 loc) · 4.26 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Copyright (C) 2025 Intel Corporation
# SPDX-License-Identifier: MIT
"""Code standards utilities."""
import logging
import sys
from subprocess import run
from .configure import delete_config_files, create_config_files
from ..utils import get_root_dir, set_up_logging, set_cwd
logger = logging.getLogger("mfd-code-quality.code_standard")
def _test_flake8() -> bool:
"""
Run flake8 tests.
:return: True if test completed successfully, False - otherwise.
"""
flake_run_outcome = run((sys.executable, "-m", "flake8"), cwd=get_root_dir())
return flake_run_outcome.returncode == 0
def _test_ruff_format() -> bool:
"""
Run ruff format check.
:return: True if there is nothing to format, False - otherwise.
"""
logger.info("Checking 'ruff format --check'...")
ruff_format_outcome = run(
(sys.executable, "-m", "ruff", "format", "--check"), capture_output=True, text=True, cwd=get_root_dir()
)
logger.info(f"Output: {ruff_format_outcome.stdout.strip()}")
return ruff_format_outcome.returncode == 0
def _test_ruff_check() -> bool:
"""
Run ruff linter check.
:return: True if ruff check did not find any issues, False - otherwise.
"""
logger.info("Checking 'ruff check'...")
ruff_run_outcome = run((sys.executable, "-m", "ruff", "check"), capture_output=True, text=True, cwd=get_root_dir())
logger.info(f"Output: {ruff_run_outcome.stdout.strip()}")
return ruff_run_outcome.returncode == 0
def _get_available_code_standard_module() -> str:
"""
Get available code standard module which is installed in python.
It will be either flake8 or ruff.
:return: flake8 or ruff
:raises Exception: When no code standard module is available
"""
code_standard_modules = ["ruff", "flake8"]
commands = [("uv", "pip", "list"), (sys.executable, "-m", "pip", "list")]
for cmd in commands:
try:
pip_list = run(cmd, capture_output=True, text=True, cwd=get_root_dir())
except Exception as e: # noqa
logger.debug(f"Error occurred while running {cmd}:\n{e}")
continue
if pip_list.returncode != 0:
continue
for code_standard_module in code_standard_modules:
if f"{code_standard_module} " in pip_list.stdout:
logger.info(f"{code_standard_module.capitalize()} will be used for code standard check.")
return code_standard_module
raise Exception("No code standard module is available! [flake8 or ruff]")
def _run_code_standard_tests(with_configs: bool = True) -> bool:
"""
Run code standard tests.
:param with_configs: Should we create configuration files before running checks.
:return: True if all tests passed, False otherwise.
"""
set_up_logging()
set_cwd()
code_standard_module = None
try:
results = []
code_standard_module = _get_available_code_standard_module()
if code_standard_module == "ruff":
if with_configs:
logger.debug("Prepare configuration files required for checks.")
create_config_files()
results.append(_test_ruff_format())
results.append(_test_ruff_check())
elif code_standard_module == "flake8":
results.append(_test_flake8())
return_val = all(results)
if return_val:
message = "Code standard check PASSED."
else:
if code_standard_module == "ruff":
logger.info(
"Ruff check was called correctly, however check failed.\n"
"For fixing code standard call 'mfd-code-format' first.\n"
"If you want to see more details what is wrong, call 'ruff format --diff'.\n"
)
message = "Code standard check FAILED."
logger.info(message)
finally:
if code_standard_module == "ruff" and with_configs:
logger.debug("Delete configuration files.")
delete_config_files()
return return_val
def run_checks(with_configs: bool = True) -> None:
"""
Run code standard tests.
:param with_configs: Should we create configuration files before running checks.
"""
return_val = _run_code_standard_tests(with_configs)
sys.exit(0 if return_val else 1)