patchwise 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- patchwise/__init__.py +18 -0
- patchwise/logger_setup.py +81 -0
- patchwise/main.py +101 -0
- patchwise/patch_review/__init__.py +171 -0
- patchwise/patch_review/ai_review/__init__.py +2 -0
- patchwise/patch_review/ai_review/ai_code_review.py +965 -0
- patchwise/patch_review/ai_review/ai_review.py +204 -0
- patchwise/patch_review/ai_review/llm_commit_audit.py +60 -0
- patchwise/patch_review/decorators.py +50 -0
- patchwise/patch_review/kernel_tree.py +122 -0
- patchwise/patch_review/patch_review.py +317 -0
- patchwise/patch_review/static_analysis/__init__.py +2 -0
- patchwise/patch_review/static_analysis/checkpatch.py +48 -0
- patchwise/patch_review/static_analysis/coccicheck.py +81 -0
- patchwise/patch_review/static_analysis/dt_check.py +130 -0
- patchwise/patch_review/static_analysis/dtbs_check.py +85 -0
- patchwise/patch_review/static_analysis/sparse.py +150 -0
- patchwise/patch_review/static_analysis/static_analysis.py +48 -0
- patchwise-0.2.0.dist-info/METADATA +19 -0
- patchwise-0.2.0.dist-info/RECORD +24 -0
- patchwise-0.2.0.dist-info/WHEEL +5 -0
- patchwise-0.2.0.dist-info/entry_points.txt +2 -0
- patchwise-0.2.0.dist-info/licenses/LICENSE.txt +26 -0
- patchwise-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
|
|
2
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
3
|
+
|
|
4
|
+
import abc
|
|
5
|
+
import inspect
|
|
6
|
+
import logging
|
|
7
|
+
import os
|
|
8
|
+
import re
|
|
9
|
+
import subprocess
|
|
10
|
+
import shutil
|
|
11
|
+
import sys
|
|
12
|
+
import time
|
|
13
|
+
from typing import Any, List, Union
|
|
14
|
+
from git import Repo
|
|
15
|
+
from git.exc import GitCommandError
|
|
16
|
+
from git.objects.commit import Commit
|
|
17
|
+
from packaging.version import Version, InvalidVersion
|
|
18
|
+
from .. import KERNEL_PATH, SANDBOX_BIN, SANDBOX_PATH, PACKAGE_NAME, PACKAGE_PATH
|
|
19
|
+
from .kernel_tree import BRANCH_NAME
|
|
20
|
+
|
|
21
|
+
PATCH_PATH = PACKAGE_PATH / "patches"
|
|
22
|
+
BUILD_DIR = SANDBOX_PATH / "build"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Dependency:
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
name: str,
|
|
29
|
+
min_version: Union[int, float, str, None] = None,
|
|
30
|
+
max_version: Union[int, float, str, None] = None,
|
|
31
|
+
):
|
|
32
|
+
self.name = name
|
|
33
|
+
self.logger = logging.getLogger(
|
|
34
|
+
f"{PACKAGE_NAME}.{__name__.lower()}.{self.name}"
|
|
35
|
+
)
|
|
36
|
+
self.min_version: Version | None = None
|
|
37
|
+
self.max_version: Version | None = None
|
|
38
|
+
if min_version is not None:
|
|
39
|
+
self.min_version = Version(str(min_version))
|
|
40
|
+
if max_version is not None:
|
|
41
|
+
self.max_version = Version(str(max_version))
|
|
42
|
+
|
|
43
|
+
def version_in_range(self, version: Version) -> bool:
|
|
44
|
+
if (self.min_version is not None and version < self.min_version) or (self.max_version is not None and version > self.max_version):
|
|
45
|
+
return False
|
|
46
|
+
return True
|
|
47
|
+
|
|
48
|
+
def get_version(self) -> Version:
|
|
49
|
+
out = subprocess.check_output([self.name, "--version"], text=True)
|
|
50
|
+
match = re.search(r"(\d+\.\d+\.\d+)", out)
|
|
51
|
+
version = Version(match.group(1))
|
|
52
|
+
|
|
53
|
+
return version
|
|
54
|
+
|
|
55
|
+
def check(self) -> None:
|
|
56
|
+
cmd_path = shutil.which(self.name)
|
|
57
|
+
if cmd_path is None or not os.access(cmd_path, os.X_OK):
|
|
58
|
+
raise ImportError(
|
|
59
|
+
f"{self.name} is not installed or not executable. Please install {self.name}."
|
|
60
|
+
)
|
|
61
|
+
self.logger.debug(f"{self.name} is installed and executable at {cmd_path}.")
|
|
62
|
+
|
|
63
|
+
if self.min_version is None and self.max_version is None:
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
minmax: list[str] = []
|
|
67
|
+
if self.min_version:
|
|
68
|
+
minmax.append(f">= {self.min_version}")
|
|
69
|
+
if self.max_version:
|
|
70
|
+
minmax.append(f"<= {self.max_version}")
|
|
71
|
+
minmax_str = ", ".join(minmax)
|
|
72
|
+
|
|
73
|
+
try:
|
|
74
|
+
version = self.get_version()
|
|
75
|
+
if not self.version_in_range(version):
|
|
76
|
+
raise ImportError(
|
|
77
|
+
f"{self.name} version {{{version}}} does not meet the version requirements ({minmax_str})."
|
|
78
|
+
)
|
|
79
|
+
self.logger.debug(
|
|
80
|
+
f"{self.name} version {{{version}}} is installed and meets the version requirements ({minmax_str})."
|
|
81
|
+
)
|
|
82
|
+
except (FileNotFoundError, subprocess.CalledProcessError, InvalidVersion):
|
|
83
|
+
raise ImportError(
|
|
84
|
+
f"{self.name} is not installed or not working. Please install {self.name} ({minmax_str})."
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def install_from_pkg_manager(self) -> None:
|
|
88
|
+
pkg_managers: list[tuple[str, list[str] | None, list[str]]] = [
|
|
89
|
+
("apt-get", ["sudo", "apt-get", "update"], ["sudo", "apt-get", "install", "-y", self.name]),
|
|
90
|
+
("dnf", None, ["sudo", "dnf", "install", "-y", self.name]),
|
|
91
|
+
("yum", None, ["sudo", "yum", "install", "-y", self.name]),
|
|
92
|
+
("zypper", None, ["sudo", "zypper", "install", "-y", self.name]),
|
|
93
|
+
("pacman", None, ["sudo", "pacman", "-Sy", self.name]),
|
|
94
|
+
]
|
|
95
|
+
for mgr, pre, install_cmd in pkg_managers:
|
|
96
|
+
if shutil.which(mgr):
|
|
97
|
+
try:
|
|
98
|
+
if pre:
|
|
99
|
+
subprocess.run(pre, check=True)
|
|
100
|
+
subprocess.run(install_cmd, check=True)
|
|
101
|
+
break
|
|
102
|
+
except Exception:
|
|
103
|
+
continue
|
|
104
|
+
|
|
105
|
+
def _do_install(self) -> None:
|
|
106
|
+
"""
|
|
107
|
+
Subclasses must implement this method to install the dependency.
|
|
108
|
+
"""
|
|
109
|
+
method_name = (
|
|
110
|
+
f"{self.__class__.__name__}.{inspect.currentframe().f_code.co_name}"
|
|
111
|
+
)
|
|
112
|
+
raise NotImplementedError(
|
|
113
|
+
f"{self.__class__.__name__} must implement {method_name}."
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
def install(self) -> None:
|
|
117
|
+
try:
|
|
118
|
+
self.check()
|
|
119
|
+
except ImportError as e:
|
|
120
|
+
self.logger.warning(f"{e}")
|
|
121
|
+
self.logger.info("Preparing to install...")
|
|
122
|
+
self._do_install()
|
|
123
|
+
self.check()
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class PatchReview(abc.ABC):
|
|
127
|
+
|
|
128
|
+
# Subclasses must define a list of Dependency objects
|
|
129
|
+
DEPENDENCIES: list[Dependency]
|
|
130
|
+
|
|
131
|
+
@classmethod
|
|
132
|
+
def get_logger(cls) -> logging.Logger:
|
|
133
|
+
return logging.getLogger(f"{PACKAGE_NAME}.{cls.__name__.lower()}")
|
|
134
|
+
|
|
135
|
+
def __init__(self, commit: Commit, base_commit: Commit | None = None):
|
|
136
|
+
self.logger = self.get_logger()
|
|
137
|
+
self.__class__.verify_dependencies()
|
|
138
|
+
self.repo = Repo(KERNEL_PATH)
|
|
139
|
+
self.commit = commit
|
|
140
|
+
# The default for base_commit is the parent of the commit if not provided
|
|
141
|
+
# TODO alternatively use FETCH_HEAD after a git fetch
|
|
142
|
+
self.base_commit = base_commit or commit.parents[0]
|
|
143
|
+
self.build_dir = BUILD_DIR / str(self.commit.hexsha)
|
|
144
|
+
self.build_dir.mkdir(parents=True, exist_ok=True)
|
|
145
|
+
self.apply_patches([self.commit])
|
|
146
|
+
self.rebase_commit = self.repo.head.commit
|
|
147
|
+
self.setup()
|
|
148
|
+
|
|
149
|
+
_sandbox_path_added = False
|
|
150
|
+
|
|
151
|
+
@staticmethod
|
|
152
|
+
def add_sandbox_to_path():
|
|
153
|
+
"""
|
|
154
|
+
Adds the sandbox bin directory to the PATH environment variable if not already present.
|
|
155
|
+
"""
|
|
156
|
+
if not PatchReview._sandbox_path_added:
|
|
157
|
+
current_path = os.environ.get("PATH", "")
|
|
158
|
+
if SANDBOX_BIN not in current_path.split(":"):
|
|
159
|
+
os.environ["PATH"] = str(SANDBOX_BIN) + ":" + current_path
|
|
160
|
+
PatchReview.get_logger().debug(f"Added {SANDBOX_BIN} to PATH.")
|
|
161
|
+
else:
|
|
162
|
+
PatchReview.get_logger().debug(f"{SANDBOX_BIN} already in PATH.")
|
|
163
|
+
PatchReview._sandbox_path_added = True
|
|
164
|
+
else:
|
|
165
|
+
PatchReview.get_logger().debug("Sandbox path already added to PATH.")
|
|
166
|
+
|
|
167
|
+
@classmethod
|
|
168
|
+
def verify_dependencies(cls, install: bool = False) -> None:
|
|
169
|
+
"""
|
|
170
|
+
Verifies that all dependencies are installed and meet the minimum version requirements.
|
|
171
|
+
"""
|
|
172
|
+
cls.add_sandbox_to_path()
|
|
173
|
+
|
|
174
|
+
if not getattr(cls, "_dependencies_verified", False):
|
|
175
|
+
for dependency in cls.DEPENDENCIES:
|
|
176
|
+
if install:
|
|
177
|
+
dependency.install()
|
|
178
|
+
else:
|
|
179
|
+
dependency.check()
|
|
180
|
+
cls.get_logger().debug(f"{cls.__name__} dependencies are installed.")
|
|
181
|
+
setattr(cls, "_dependencies_verified", True)
|
|
182
|
+
|
|
183
|
+
def git_abort(self) -> None:
|
|
184
|
+
"""
|
|
185
|
+
Abort any ongoing git operations.
|
|
186
|
+
"""
|
|
187
|
+
self.logger.debug("Attempting to abort any ongoing git operations.")
|
|
188
|
+
try:
|
|
189
|
+
self.repo.git.am("--abort")
|
|
190
|
+
except GitCommandError:
|
|
191
|
+
pass
|
|
192
|
+
try:
|
|
193
|
+
self.repo.git.rebase("--abort")
|
|
194
|
+
except GitCommandError:
|
|
195
|
+
pass
|
|
196
|
+
try:
|
|
197
|
+
self.repo.git.cherry_pick("--abort")
|
|
198
|
+
except GitCommandError:
|
|
199
|
+
pass
|
|
200
|
+
try:
|
|
201
|
+
self.repo.git.merge("--abort")
|
|
202
|
+
except GitCommandError:
|
|
203
|
+
pass
|
|
204
|
+
|
|
205
|
+
def apply_patches(self, commits: list[Commit]) -> None:
|
|
206
|
+
self.git_abort()
|
|
207
|
+
self.repo.git.switch(BRANCH_NAME, detach=True)
|
|
208
|
+
self.logger.debug(f"Applying patches from {PATCH_PATH} on branch {BRANCH_NAME}")
|
|
209
|
+
general_patch_files = sorted((PATCH_PATH / "general").glob("*.patch"))
|
|
210
|
+
self.logger.debug(f"Applying general patches: {general_patch_files}")
|
|
211
|
+
review_patch_files = sorted(
|
|
212
|
+
(PATCH_PATH / self.__class__.__name__.lower()).glob("*.patch")
|
|
213
|
+
)
|
|
214
|
+
self.logger.debug(f"Applying review patches: {review_patch_files}")
|
|
215
|
+
patch_files = general_patch_files + review_patch_files
|
|
216
|
+
for patch_file in patch_files:
|
|
217
|
+
self.logger.debug(f"Applying patch: {patch_file}")
|
|
218
|
+
try:
|
|
219
|
+
self.repo.git.am(str(patch_file))
|
|
220
|
+
except Exception as e:
|
|
221
|
+
self.logger.warning(f"Failed to apply patch {patch_file}: {e}")
|
|
222
|
+
self.repo.git.am("--skip")
|
|
223
|
+
|
|
224
|
+
cherry_commits = commits or [self.commit]
|
|
225
|
+
for cherry_commit in cherry_commits:
|
|
226
|
+
self.logger.debug(f"Applying commit: {cherry_commit.hexsha}")
|
|
227
|
+
try:
|
|
228
|
+
self.repo.git.cherry_pick(cherry_commit.hexsha)
|
|
229
|
+
except Exception as e:
|
|
230
|
+
# If the commit is already applied or cherry-pick fails, log and continue
|
|
231
|
+
self.logger.warning(
|
|
232
|
+
f"Failed to cherry-pick {cherry_commit.hexsha}: {e}"
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
@abc.abstractmethod
|
|
236
|
+
def setup(self) -> None:
|
|
237
|
+
"""
|
|
238
|
+
Set up the environment for the patch review.
|
|
239
|
+
"""
|
|
240
|
+
pass
|
|
241
|
+
|
|
242
|
+
def run_cmd_with_timer(
|
|
243
|
+
self,
|
|
244
|
+
cmd: List[str],
|
|
245
|
+
desc: str,
|
|
246
|
+
cwd: str,
|
|
247
|
+
stdout: int | None = subprocess.PIPE,
|
|
248
|
+
stderr: int | None = subprocess.PIPE,
|
|
249
|
+
**kwargs: Any,
|
|
250
|
+
) -> str:
|
|
251
|
+
"""
|
|
252
|
+
Runs a make command and displays a timer while it runs,
|
|
253
|
+
but only if logger level is INFO or lower.
|
|
254
|
+
|
|
255
|
+
Parameters:
|
|
256
|
+
cmd (str): The command to run using subprocess.Popen().
|
|
257
|
+
desc (str): The title for the timer
|
|
258
|
+
stdout_path (str, optional): Path to file for stdout. Defaults to None.
|
|
259
|
+
stderr_path (str, optional): Path to file for stderr. Defaults to None.
|
|
260
|
+
**kwargs: Rest of the args for subprocess.Popen.
|
|
261
|
+
|
|
262
|
+
Returns:
|
|
263
|
+
str: Output of running the command (stdout + stderr).
|
|
264
|
+
To skip stdout/stderr, pass stdout/stderr=subprocess.DEVNULL.
|
|
265
|
+
"""
|
|
266
|
+
show_timer = self.logger.isEnabledFor(logging.INFO)
|
|
267
|
+
start = time.time()
|
|
268
|
+
|
|
269
|
+
with subprocess.Popen(
|
|
270
|
+
cmd,
|
|
271
|
+
cwd=cwd,
|
|
272
|
+
stdout=stdout,
|
|
273
|
+
stderr=stderr,
|
|
274
|
+
text=True,
|
|
275
|
+
bufsize=1,
|
|
276
|
+
universal_newlines=True,
|
|
277
|
+
**kwargs,
|
|
278
|
+
) as process:
|
|
279
|
+
output = ""
|
|
280
|
+
while True:
|
|
281
|
+
try:
|
|
282
|
+
_stdout, _stderr = process.communicate(timeout=5)
|
|
283
|
+
if _stdout:
|
|
284
|
+
self.logger.debug(_stdout)
|
|
285
|
+
output += _stdout
|
|
286
|
+
if _stderr:
|
|
287
|
+
self.logger.debug(_stderr)
|
|
288
|
+
output += _stderr
|
|
289
|
+
|
|
290
|
+
if show_timer:
|
|
291
|
+
sys.stdout.write("\r" + " " * 40 + "\r") # Clear the line
|
|
292
|
+
sys.stdout.flush()
|
|
293
|
+
elapsed = int(time.time() - start)
|
|
294
|
+
self.logger.debug(f"{desc}... {elapsed}s elapsed")
|
|
295
|
+
break
|
|
296
|
+
|
|
297
|
+
except subprocess.TimeoutExpired:
|
|
298
|
+
elapsed = int(time.time() - start)
|
|
299
|
+
if show_timer:
|
|
300
|
+
sys.stdout.write(f"\r{desc}... {elapsed}s elapsed")
|
|
301
|
+
sys.stdout.flush()
|
|
302
|
+
|
|
303
|
+
except Exception:
|
|
304
|
+
process.kill()
|
|
305
|
+
raise
|
|
306
|
+
|
|
307
|
+
return output # TODO return a tuple of (stdout, stderr) if both are needed
|
|
308
|
+
|
|
309
|
+
@abc.abstractmethod
|
|
310
|
+
def run(self) -> str:
|
|
311
|
+
"""
|
|
312
|
+
Execute the patch review.
|
|
313
|
+
|
|
314
|
+
This method must be overridden by subclasses. It should contain the logic
|
|
315
|
+
for the specific type of patch review being performed.
|
|
316
|
+
"""
|
|
317
|
+
pass
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
|
|
2
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
from .static_analysis import StaticAnalysis
|
|
6
|
+
from ...patch_review.decorators import register_static_analysis_review, register_short_review
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@register_static_analysis_review
|
|
10
|
+
@register_short_review
|
|
11
|
+
class Checkpatch(StaticAnalysis):
|
|
12
|
+
"""
|
|
13
|
+
Perform static analysis on kernel commits using the checkpatch.pl script.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
DEPENDENCIES = []
|
|
17
|
+
|
|
18
|
+
def setup(self) -> None:
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
def run(self) -> str:
|
|
22
|
+
return self.run_cmd_with_timer(
|
|
23
|
+
[
|
|
24
|
+
os.path.join("scripts", "checkpatch.pl"),
|
|
25
|
+
"--quiet",
|
|
26
|
+
"--subjective",
|
|
27
|
+
"--strict",
|
|
28
|
+
"--showfile",
|
|
29
|
+
"--show-types",
|
|
30
|
+
"--codespell",
|
|
31
|
+
"--mailback",
|
|
32
|
+
"--ignore",
|
|
33
|
+
",".join(
|
|
34
|
+
[
|
|
35
|
+
# We review patches one at a time and don't try to apply to
|
|
36
|
+
# tree. So, checkpatch will not see that earlier patch adds
|
|
37
|
+
# the DT string
|
|
38
|
+
"UNDOCUMENTED_DT_STRING",
|
|
39
|
+
"FILE_PATH_CHANGES",
|
|
40
|
+
"CONFIG_DESCRIPTION",
|
|
41
|
+
]
|
|
42
|
+
),
|
|
43
|
+
"--git",
|
|
44
|
+
self.base_commit.hexsha + "..." + self.commit.hexsha,
|
|
45
|
+
],
|
|
46
|
+
cwd=str(self.repo.working_tree_dir),
|
|
47
|
+
desc="checkpatch",
|
|
48
|
+
)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
|
|
2
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
from .static_analysis import StaticAnalysis
|
|
7
|
+
from ...patch_review.decorators import (
|
|
8
|
+
register_static_analysis_review,
|
|
9
|
+
register_short_review,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@register_static_analysis_review
|
|
14
|
+
@register_short_review
|
|
15
|
+
class Coccicheck(StaticAnalysis):
|
|
16
|
+
|
|
17
|
+
DEPENDENCIES = []
|
|
18
|
+
|
|
19
|
+
def _run_coccicheck(self, directory: str) -> str:
|
|
20
|
+
coccicheck_output = super().run_cmd_with_timer(
|
|
21
|
+
[
|
|
22
|
+
"make",
|
|
23
|
+
f"O={self.build_dir}",
|
|
24
|
+
f"-j{os.cpu_count()}",
|
|
25
|
+
"ARCH=arm64",
|
|
26
|
+
"LLVM=1",
|
|
27
|
+
"-s",
|
|
28
|
+
"coccicheck",
|
|
29
|
+
f"M={directory}",
|
|
30
|
+
"MODE=report",
|
|
31
|
+
f"DEBUG_FILE={self.symlink_path}", # if hasattr(self, 'symlink_path') else "DEBUG_FILE=/dev/null",
|
|
32
|
+
],
|
|
33
|
+
cwd=str(self.repo.working_tree_dir),
|
|
34
|
+
desc="coccicheck running",
|
|
35
|
+
)
|
|
36
|
+
return coccicheck_output
|
|
37
|
+
|
|
38
|
+
def setup(self) -> None:
|
|
39
|
+
# Create symlink /tmp/{package_name}_null -> /dev/null
|
|
40
|
+
# Necessary to trick the coccicheck script into piping stdout to /dev/null otherwise it combines stdout and stderr for some reason
|
|
41
|
+
package_name = __package__ or "coccicheck"
|
|
42
|
+
self.symlink_path = f"/tmp/{package_name}_null"
|
|
43
|
+
target = "/dev/null"
|
|
44
|
+
if os.path.islink(self.symlink_path) or os.path.exists(self.symlink_path):
|
|
45
|
+
os.remove(self.symlink_path)
|
|
46
|
+
os.symlink(target, self.symlink_path)
|
|
47
|
+
|
|
48
|
+
def run(self) -> str:
|
|
49
|
+
# TODO make sure that setup() runs in order for run() to run
|
|
50
|
+
self.logger.debug(f"Running cocci_check")
|
|
51
|
+
output = ""
|
|
52
|
+
modified_files = set(self.commit.stats.files.keys())
|
|
53
|
+
line_re = re.compile(r"^([^:]+):\d+:\d+-\d+:.*")
|
|
54
|
+
|
|
55
|
+
directories: set[str] = set()
|
|
56
|
+
for item in self.commit.stats.files:
|
|
57
|
+
dir_path = os.path.dirname(item)
|
|
58
|
+
if dir_path:
|
|
59
|
+
directories.add(dir_path)
|
|
60
|
+
self.logger.debug(f"Directories containing modified files: {directories}")
|
|
61
|
+
|
|
62
|
+
for directory in directories:
|
|
63
|
+
self.logger.debug(f"Running coccicheck on directory: '{directory}'")
|
|
64
|
+
cur_output = self._run_coccicheck(directory)
|
|
65
|
+
if not cur_output:
|
|
66
|
+
self.logger.debug(f"No coccicheck output for {directory}, skipping")
|
|
67
|
+
continue
|
|
68
|
+
|
|
69
|
+
self.logger.debug(f"Coccicheck output for {directory}:\n{cur_output}")
|
|
70
|
+
for line in cur_output.splitlines():
|
|
71
|
+
match = line_re.match(line)
|
|
72
|
+
if not match:
|
|
73
|
+
continue
|
|
74
|
+
file_path = match.group(1)
|
|
75
|
+
if file_path.startswith("./"):
|
|
76
|
+
file_path = file_path[2:]
|
|
77
|
+
full_path = os.path.join(directory, file_path)
|
|
78
|
+
if full_path in modified_files:
|
|
79
|
+
output += line + "\n"
|
|
80
|
+
|
|
81
|
+
return output
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
|
|
2
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from git.objects.commit import Commit
|
|
7
|
+
from .static_analysis import StaticAnalysis
|
|
8
|
+
from ... import SANDBOX_PATH
|
|
9
|
+
from ...patch_review.decorators import (
|
|
10
|
+
register_static_analysis_review,
|
|
11
|
+
register_long_review,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@register_static_analysis_review
|
|
16
|
+
@register_long_review
|
|
17
|
+
class DtCheck(StaticAnalysis):
|
|
18
|
+
"""
|
|
19
|
+
Performs static analysis on kernel commits to check Device Tree bindings
|
|
20
|
+
using dt_binding_check.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
DEPENDENCIES = []
|
|
24
|
+
|
|
25
|
+
def __make_refcheckdocs(self) -> str:
|
|
26
|
+
self.logger.debug("Making refcheckdocs")
|
|
27
|
+
output = super().run_cmd_with_timer(
|
|
28
|
+
[
|
|
29
|
+
"make",
|
|
30
|
+
f"-j{os.cpu_count()}",
|
|
31
|
+
"-s",
|
|
32
|
+
f"O={self.build_dir}",
|
|
33
|
+
"ARCH=arm",
|
|
34
|
+
"LLVM=1",
|
|
35
|
+
"refcheckdocs",
|
|
36
|
+
],
|
|
37
|
+
cwd=str(self.repo.working_tree_dir),
|
|
38
|
+
desc="refcheckdocs",
|
|
39
|
+
)
|
|
40
|
+
return output.strip()
|
|
41
|
+
|
|
42
|
+
def __make_dt_binding_check(self) -> str:
|
|
43
|
+
self.logger.debug("Making dt_binding_check")
|
|
44
|
+
output = super().run_cmd_with_timer(
|
|
45
|
+
cmd=[
|
|
46
|
+
"make",
|
|
47
|
+
f"-j{os.cpu_count()}",
|
|
48
|
+
"-s",
|
|
49
|
+
f"O={self.build_dir}",
|
|
50
|
+
"ARCH=arm",
|
|
51
|
+
"LLVM=1",
|
|
52
|
+
"DT_CHECKER_FLAGS=-m",
|
|
53
|
+
"dt_binding_check",
|
|
54
|
+
],
|
|
55
|
+
cwd=str(self.repo.working_tree_dir),
|
|
56
|
+
desc="dt_binding_check",
|
|
57
|
+
)
|
|
58
|
+
return output.strip()
|
|
59
|
+
|
|
60
|
+
def __get_unique_lines(self, baseline_log: str, new_log: str) -> str:
|
|
61
|
+
last_lines = set(baseline_log.splitlines())
|
|
62
|
+
current_lines = set(new_log.splitlines())
|
|
63
|
+
unique_lines = current_lines - last_lines
|
|
64
|
+
return "\n".join(unique_lines)
|
|
65
|
+
|
|
66
|
+
def __get_dt_checker_logs(self, commit: Commit) -> tuple[str, str]:
|
|
67
|
+
# TODO Extract yamllint warnings/errors
|
|
68
|
+
"""
|
|
69
|
+
Retrieves and caches dt_checker logs for a given kernel tree and SHA.
|
|
70
|
+
Logs are saved to files in the 'dt-checker-logs' folder.
|
|
71
|
+
"""
|
|
72
|
+
logs_dir = Path(SANDBOX_PATH) / "dt-checker-logs"
|
|
73
|
+
refcheckdocs_log_path = logs_dir / f"{commit.hexsha}_refcheckdocs.log"
|
|
74
|
+
dt_binding_check_log_path = logs_dir / f"{commit.hexsha}_dt_binding_check.log"
|
|
75
|
+
|
|
76
|
+
self.logger.debug(f"Running dt-checker on: {commit.hexsha}")
|
|
77
|
+
|
|
78
|
+
logs_dir.mkdir(parents=True, exist_ok=True)
|
|
79
|
+
|
|
80
|
+
if dt_binding_check_log_path.exists() and refcheckdocs_log_path.exists():
|
|
81
|
+
self.logger.debug(f"Using cached dt_binding_check logs for: {commit.hexsha}")
|
|
82
|
+
else:
|
|
83
|
+
self.apply_patches([commit])
|
|
84
|
+
refcheckdocs_logs = self.__make_refcheckdocs()
|
|
85
|
+
refcheckdocs_log_path.write_text(refcheckdocs_logs)
|
|
86
|
+
self.logger.debug(f"Saved refcheckdocs logs to {refcheckdocs_log_path}")
|
|
87
|
+
dt_binding_check_logs = self.__make_dt_binding_check()
|
|
88
|
+
dt_binding_check_log_path.write_text(dt_binding_check_logs)
|
|
89
|
+
self.logger.debug(f"Saved dt_binding_check logs to {dt_binding_check_log_path}")
|
|
90
|
+
|
|
91
|
+
return refcheckdocs_log_path.read_text(), dt_binding_check_log_path.read_text()
|
|
92
|
+
|
|
93
|
+
def setup(self) -> None:
|
|
94
|
+
self.logger.debug("Setting up dt-check")
|
|
95
|
+
self.dt_files = [
|
|
96
|
+
f
|
|
97
|
+
for f in self.commit.stats.files.keys()
|
|
98
|
+
if str(f).startswith("Documentation") and str(f).endswith(".yaml")
|
|
99
|
+
]
|
|
100
|
+
if not self.dt_files:
|
|
101
|
+
self.logger.debug("No modified dt files")
|
|
102
|
+
return
|
|
103
|
+
|
|
104
|
+
self.logger.debug(f"Modified dt files: {self.dt_files}")
|
|
105
|
+
|
|
106
|
+
def run(self) -> str:
|
|
107
|
+
output = ""
|
|
108
|
+
|
|
109
|
+
if not self.dt_files:
|
|
110
|
+
self.logger.debug("No modified dt files")
|
|
111
|
+
return output
|
|
112
|
+
|
|
113
|
+
self.logger.debug(f"Preparing kernel tree for dt checks")
|
|
114
|
+
# super().clean_tree()
|
|
115
|
+
super().make_config() # TODO change back to _make_allmodconfig
|
|
116
|
+
base_refcheck, base_binding = self.__get_dt_checker_logs(self.base_commit)
|
|
117
|
+
patch_refcheck, patch_binding = self.__get_dt_checker_logs(self.commit)
|
|
118
|
+
|
|
119
|
+
refcheckdocs_output = self.__get_unique_lines(base_refcheck, patch_refcheck)
|
|
120
|
+
dt_binding_check_output = self.__get_unique_lines(base_binding, patch_binding)
|
|
121
|
+
|
|
122
|
+
if not refcheckdocs_output and not dt_binding_check_output:
|
|
123
|
+
self.logger.info("No dt-checker errors")
|
|
124
|
+
return output
|
|
125
|
+
if len(refcheckdocs_output) > 0:
|
|
126
|
+
output += f"refcheckdocs:\n{refcheckdocs_output}\n"
|
|
127
|
+
if len(dt_binding_check_output) > 0:
|
|
128
|
+
output += f"dt_binding_check:\n{dt_binding_check_output}\n"
|
|
129
|
+
|
|
130
|
+
return output
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
|
|
2
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from .static_analysis import StaticAnalysis
|
|
7
|
+
from ... import SANDBOX_PATH
|
|
8
|
+
from ...patch_review.decorators import (
|
|
9
|
+
register_static_analysis_review,
|
|
10
|
+
register_long_review,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
@register_static_analysis_review
|
|
14
|
+
@register_long_review
|
|
15
|
+
class DtbsCheck(StaticAnalysis):
|
|
16
|
+
"""
|
|
17
|
+
Performs static analysis on kernel commits to check Device Tree bindings
|
|
18
|
+
using dtbs_check for both arm and arm64 architectures.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
DEPENDENCIES = []
|
|
22
|
+
|
|
23
|
+
def __run_dtbs_check(self, sha: str) -> str:
|
|
24
|
+
kernel_tree = str(self.repo.working_tree_dir)
|
|
25
|
+
log_dir = Path(SANDBOX_PATH) / "dtbs-logs"
|
|
26
|
+
log_dir.mkdir(parents=True, exist_ok=True)
|
|
27
|
+
logfile = log_dir / f"build-dtbs-{sha}.log"
|
|
28
|
+
cfg_opts = [
|
|
29
|
+
"CONFIG_ARM64_ERRATUM_843419=n",
|
|
30
|
+
"CONFIG_ARM64_USE_LSE_ATOMICS=n",
|
|
31
|
+
"CONFIG_BROKEN_GAS_INST=n",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
arch = "arm64" # TODO loop through both arm and arm64
|
|
35
|
+
super().make_config(arch=arch, extra_args=cfg_opts) # TODO use _make_allmodconfig
|
|
36
|
+
dtbs_check_output = super().run_cmd_with_timer(
|
|
37
|
+
cmd=[
|
|
38
|
+
"make",
|
|
39
|
+
f"-j{os.cpu_count()}",
|
|
40
|
+
"-s",
|
|
41
|
+
f"O={self.build_dir}",
|
|
42
|
+
f"ARCH={arch}",
|
|
43
|
+
"LLVM=1",
|
|
44
|
+
"dtbs_check",
|
|
45
|
+
] + cfg_opts,
|
|
46
|
+
cwd=kernel_tree,
|
|
47
|
+
desc=f"dtbs_check",
|
|
48
|
+
)
|
|
49
|
+
# logfile.write_text(dtbs_check_output) # TODO log to file and check for cache
|
|
50
|
+
return dtbs_check_output
|
|
51
|
+
|
|
52
|
+
def __get_unique_lines(self, baseline_log: str, new_log: str) -> str:
|
|
53
|
+
last_lines = set(baseline_log.splitlines())
|
|
54
|
+
current_lines = set(new_log.splitlines())
|
|
55
|
+
unique_lines = current_lines - last_lines
|
|
56
|
+
return "\n".join(unique_lines)
|
|
57
|
+
|
|
58
|
+
def setup(self) -> None:
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
def run(self) -> str:
|
|
62
|
+
self.logger.debug("Running dtbs_check analysis")
|
|
63
|
+
|
|
64
|
+
modified_files = [str(f) for f in self.commit.stats.files.keys()]
|
|
65
|
+
dt_files = [
|
|
66
|
+
f for f in modified_files if f.endswith((".yaml", ".dts", ".dtsi"))
|
|
67
|
+
]
|
|
68
|
+
if not dt_files:
|
|
69
|
+
self.logger.debug("No modified DT schema files found, skipping dtbs_check")
|
|
70
|
+
return ""
|
|
71
|
+
self.logger.debug(f"Modified DT files: {dt_files}")
|
|
72
|
+
|
|
73
|
+
self.logger.debug(f"Running dtbs_check for base commit: {self.base_commit.message}")
|
|
74
|
+
self.apply_patches([self.base_commit])
|
|
75
|
+
baseline_output = self.__run_dtbs_check(self.base_commit.hexsha)
|
|
76
|
+
|
|
77
|
+
self.logger.debug(f"Running dtbs_check for patch commit: {self.commit.message}")
|
|
78
|
+
self.apply_patches([self.commit])
|
|
79
|
+
patch_output = self.__run_dtbs_check(self.commit.hexsha)
|
|
80
|
+
|
|
81
|
+
diff_output = self.__get_unique_lines(baseline_output, patch_output)
|
|
82
|
+
if not diff_output:
|
|
83
|
+
self.logger.info("No dtbs_check errors found")
|
|
84
|
+
|
|
85
|
+
return diff_output
|