jarbin-c-coding-style 2.0.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.
- jarbin_c_coding_style-2.0.0.dist-info/METADATA +150 -0
- jarbin_c_coding_style-2.0.0.dist-info/RECORD +186 -0
- jarbin_c_coding_style-2.0.0.dist-info/WHEEL +4 -0
- jarbin_c_coding_style-2.0.0.dist-info/entry_points.txt +2 -0
- jarbin_c_coding_style-2.0.0.dist-info/licenses/LICENSE +674 -0
- sources/__init__.py +0 -0
- sources/__main__.py +6 -0
- sources/analysis/__init__.py +0 -0
- sources/analysis/directives.py +258 -0
- sources/analysis/lexer.py +288 -0
- sources/analysis/parser.py +1754 -0
- sources/analysis/project.py +286 -0
- sources/analysis/source.py +326 -0
- sources/analysis/syntax.py +238 -0
- sources/analysis/tokens.py +116 -0
- sources/analysis/unit.py +700 -0
- sources/application.py +118 -0
- sources/build_info.json +6 -0
- sources/cli/__init__.py +0 -0
- sources/cli/arguments.py +507 -0
- sources/cli/completion.py +610 -0
- sources/cli/context.py +258 -0
- sources/cli/converters.py +204 -0
- sources/cli/help.py +162 -0
- sources/cli/parser.py +659 -0
- sources/configs/__init__.py +0 -0
- sources/configs/editor.py +207 -0
- sources/configs/ini.py +327 -0
- sources/configs/project.py +735 -0
- sources/configs/schema/__init__.py +0 -0
- sources/configs/schema/project.py +68 -0
- sources/constants.py +801 -0
- sources/diagnostics/__init__.py +0 -0
- sources/diagnostics/fingerprint.py +82 -0
- sources/diagnostics/model.py +146 -0
- sources/engine/__init__.py +0 -0
- sources/engine/api.py +355 -0
- sources/engine/examples.py +157 -0
- sources/engine/outcome.py +114 -0
- sources/engine/plan.py +440 -0
- sources/engine/runner.py +345 -0
- sources/engine/scanner.py +179 -0
- sources/enums.py +838 -0
- sources/errors/__init__.py +0 -0
- sources/errors/exceptions.py +727 -0
- sources/fixers/__init__.py +0 -0
- sources/fixers/base.py +209 -0
- sources/fixers/conflicts.py +129 -0
- sources/fixers/diff.py +73 -0
- sources/fixers/engine.py +434 -0
- sources/fixers/journal.py +290 -0
- sources/fixers/kinds.py +30 -0
- sources/fixers/layout.py +411 -0
- sources/fixers/model.py +151 -0
- sources/fixers/registry.py +54 -0
- sources/fixers/text.py +353 -0
- sources/fixers/verify.py +123 -0
- sources/fixers/writer.py +88 -0
- sources/integrations/__init__.py +0 -0
- sources/integrations/ci/__init__.py +0 -0
- sources/integrations/ci/github.py +183 -0
- sources/integrations/editors/__init__.py +0 -0
- sources/integrations/git/__init__.py +0 -0
- sources/integrations/git/repository.py +176 -0
- sources/integrations/update/__init__.py +0 -0
- sources/integrations/update/installers.py +529 -0
- sources/integrations/update/releases.py +397 -0
- sources/integrations/update/state.py +143 -0
- sources/outputs/__init__.py +0 -0
- sources/outputs/github/__init__.py +0 -0
- sources/outputs/github/renderer.py +135 -0
- sources/outputs/html/__init__.py +0 -0
- sources/outputs/html/renderer.py +694 -0
- sources/outputs/json/__init__.py +0 -0
- sources/outputs/json/renderer.py +154 -0
- sources/outputs/json/report.schema.json +121 -0
- sources/outputs/junit/__init__.py +0 -0
- sources/outputs/junit/renderer.py +151 -0
- sources/outputs/registry.py +135 -0
- sources/outputs/report.py +224 -0
- sources/outputs/sarif/__init__.py +0 -0
- sources/outputs/sarif/renderer.py +182 -0
- sources/outputs/terminal/__init__.py +0 -0
- sources/outputs/terminal/renderer.py +184 -0
- sources/outputs/terminal/style.py +159 -0
- sources/rules/__init__.py +0 -0
- sources/rules/arguments.py +362 -0
- sources/rules/catalog.py +227 -0
- sources/rules/categories/advanced/constant_pointers.ini +46 -0
- sources/rules/categories/advanced/line_break_at_end_of_file.ini +44 -0
- sources/rules/categories/advanced/static.ini +65 -0
- sources/rules/categories/advanced/typing.ini +47 -0
- sources/rules/categories/control_structures/conditional_branching.ini +89 -0
- sources/rules/categories/control_structures/goto.ini +45 -0
- sources/rules/categories/control_structures/ternary_operators.ini +57 -0
- sources/rules/categories/extensions/language_extensions.ini +71 -0
- sources/rules/categories/files_organization/contents_of_the_repository.ini +85 -0
- sources/rules/categories/files_organization/file_coherence.ini +166 -0
- sources/rules/categories/files_organization/file_extension.ini +68 -0
- sources/rules/categories/files_organization/fixtures/program +0 -0
- sources/rules/categories/files_organization/naming_files_and_folders.ini +74 -0
- sources/rules/categories/functions/coherence_of_functions.ini +45 -0
- sources/rules/categories/functions/comments_inside_a_function.ini +45 -0
- sources/rules/categories/functions/functions_without_parameters.ini +39 -0
- sources/rules/categories/functions/naming_functions.ini +79 -0
- sources/rules/categories/functions/number_of_columns.ini +61 -0
- sources/rules/categories/functions/number_of_lines.ini +70 -0
- sources/rules/categories/functions/number_of_parameters.ini +64 -0
- sources/rules/categories/functions/structures_as_parameters.ini +44 -0
- sources/rules/categories/global_scope/constant_values.ini +49 -0
- sources/rules/categories/global_scope/file_header.ini +92 -0
- sources/rules/categories/global_scope/global_variables.ini +53 -0
- sources/rules/categories/global_scope/include.ini +39 -0
- sources/rules/categories/global_scope/indentation_of_preprocessor.ini +62 -0
- sources/rules/categories/global_scope/inline_assembly.ini +43 -0
- sources/rules/categories/global_scope/leading_trailing_lines.ini +58 -0
- sources/rules/categories/global_scope/line_endings.ini +53 -0
- sources/rules/categories/global_scope/separation_of_functions.ini +61 -0
- sources/rules/categories/global_scope/trailing_spaces.ini +45 -0
- sources/rules/categories/header_files/content.ini +104 -0
- sources/rules/categories/header_files/include_guard.ini +47 -0
- sources/rules/categories/header_files/macros.ini +57 -0
- sources/rules/categories/layout_inside_a_function_scope/blank_lines.ini +71 -0
- sources/rules/categories/layout_inside_a_function_scope/code_line_content.ini +112 -0
- sources/rules/categories/layout_inside_a_function_scope/curly_brackets.ini +94 -0
- sources/rules/categories/layout_inside_a_function_scope/indentation.ini +96 -0
- sources/rules/categories/layout_inside_a_function_scope/spaces.ini +113 -0
- sources/rules/categories/layout_inside_a_function_scope/variable_declarations.ini +65 -0
- sources/rules/categories/variables_and_types/naming_identifiers.ini +74 -0
- sources/rules/categories/variables_and_types/pointers.ini +67 -0
- sources/rules/categories/variables_and_types/structures.ini +47 -0
- sources/rules/data/verbs.txt +182 -0
- sources/rules/definition.py +430 -0
- sources/rules/loader.py +925 -0
- sources/rules/paths.py +6 -0
- sources/rules/primitives/__init__.py +0 -0
- sources/rules/primitives/base.py +297 -0
- sources/rules/primitives/braces.py +211 -0
- sources/rules/primitives/directives.py +203 -0
- sources/rules/primitives/indentation.py +376 -0
- sources/rules/primitives/layout.py +199 -0
- sources/rules/primitives/naming.py +277 -0
- sources/rules/primitives/project.py +62 -0
- sources/rules/primitives/registry.py +49 -0
- sources/rules/primitives/repository.py +129 -0
- sources/rules/primitives/review.py +87 -0
- sources/rules/primitives/semantic.py +319 -0
- sources/rules/primitives/spacing.py +210 -0
- sources/rules/primitives/structure.py +586 -0
- sources/rules/primitives/syntax.py +200 -0
- sources/rules/primitives/text.py +408 -0
- sources/rules/primitives/tokens.py +84 -0
- sources/rules/template.py +138 -0
- sources/selftest/__init__.py +0 -0
- sources/selftest/cases.py +292 -0
- sources/services.py +90 -0
- sources/subcommands/__init__.py +0 -0
- sources/subcommands/base.py +45 -0
- sources/subcommands/check/__init__.py +0 -0
- sources/subcommands/check/command.py +45 -0
- sources/subcommands/check/fixing.py +224 -0
- sources/subcommands/check/handler.py +757 -0
- sources/subcommands/info/__init__.py +0 -0
- sources/subcommands/info/command.py +45 -0
- sources/subcommands/info/handler.py +527 -0
- sources/subcommands/registry.py +37 -0
- sources/subcommands/rule/__init__.py +0 -0
- sources/subcommands/rule/command.py +45 -0
- sources/subcommands/rule/handler.py +1067 -0
- sources/subcommands/rule/render.py +273 -0
- sources/subcommands/runner.py +124 -0
- sources/subcommands/test/__init__.py +0 -0
- sources/subcommands/test/command.py +45 -0
- sources/subcommands/test/handler.py +166 -0
- sources/subcommands/test/render.py +219 -0
- sources/subcommands/test/runner.py +430 -0
- sources/subcommands/update/__init__.py +0 -0
- sources/subcommands/update/command.py +45 -0
- sources/subcommands/update/handler.py +408 -0
- sources/utils/__init__.py +0 -0
- sources/utils/build_info.py +188 -0
- sources/utils/git.py +73 -0
- sources/utils/globs.py +98 -0
- sources/utils/logging.py +154 -0
- sources/utils/schema.py +77 -0
- sources/utils/versions.py +87 -0
sources/engine/plan.py
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
from collections.abc import Iterable, Mapping
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from types import MappingProxyType
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from sources.configs.project import PathOverride, ProjectConfig, RuleSettings
|
|
7
|
+
from sources.constants import PROGRAM_VERSION
|
|
8
|
+
from sources.enums import Detection, FileKind, Layer, RuleStatus, SeverityLevel
|
|
9
|
+
from sources.errors.exceptions import ProjectConfigError, ValueConversionError
|
|
10
|
+
from sources.rules.arguments import ArgumentSpec
|
|
11
|
+
from sources.rules.catalog import RuleCatalog, SelectedRule
|
|
12
|
+
from sources.rules.definition import CheckDefinition, RuleDefinition
|
|
13
|
+
from sources.rules.loader import bind_parameters
|
|
14
|
+
from sources.rules.primitives.base import Primitive
|
|
15
|
+
from sources.rules.primitives.registry import PRIMITIVES
|
|
16
|
+
from sources.utils.globs import match_any_glob, match_glob
|
|
17
|
+
from sources.utils.versions import check_constraint
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True, slots=True)
|
|
21
|
+
class BoundCheck:
|
|
22
|
+
"""
|
|
23
|
+
A check ready to run: primitive, bound parameters and effective settings.
|
|
24
|
+
(dataclass)
|
|
25
|
+
|
|
26
|
+
Attributes
|
|
27
|
+
----------
|
|
28
|
+
rule : RuleDefinition
|
|
29
|
+
Its rule.
|
|
30
|
+
check : CheckDefinition
|
|
31
|
+
Its definition.
|
|
32
|
+
primitive : Primitive
|
|
33
|
+
The primitive running it.
|
|
34
|
+
parameters : Mapping[str, Any]
|
|
35
|
+
Bound parameter values.
|
|
36
|
+
severity : SeverityLevel
|
|
37
|
+
Effective severity.
|
|
38
|
+
detection : Detection
|
|
39
|
+
Effective detection level.
|
|
40
|
+
file_kinds : frozenset[FileKind]
|
|
41
|
+
Kinds of the files it analyses.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
rule: RuleDefinition
|
|
45
|
+
check: CheckDefinition
|
|
46
|
+
primitive: Primitive
|
|
47
|
+
parameters: Mapping[str, Any]
|
|
48
|
+
severity: SeverityLevel
|
|
49
|
+
detection: Detection
|
|
50
|
+
file_kinds: frozenset[FileKind]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True, slots=True)
|
|
54
|
+
class BoundRule:
|
|
55
|
+
"""
|
|
56
|
+
An active rule with its effective arguments and checks.
|
|
57
|
+
(dataclass)
|
|
58
|
+
|
|
59
|
+
Attributes
|
|
60
|
+
----------
|
|
61
|
+
rule : RuleDefinition
|
|
62
|
+
The rule.
|
|
63
|
+
arguments : Mapping[str, Any]
|
|
64
|
+
Effective argument values.
|
|
65
|
+
checks : tuple[BoundCheck, ...]
|
|
66
|
+
Active checks.
|
|
67
|
+
include : tuple[str, ...]
|
|
68
|
+
Only check files matching these globs (empty: every file).
|
|
69
|
+
exclude : tuple[str, ...]
|
|
70
|
+
Never check files matching these globs.
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
rule: RuleDefinition
|
|
74
|
+
arguments: Mapping[str, Any]
|
|
75
|
+
checks: tuple[BoundCheck, ...]
|
|
76
|
+
include: tuple[str, ...] = ()
|
|
77
|
+
exclude: tuple[str, ...] = ()
|
|
78
|
+
|
|
79
|
+
def accepts(self, path: str) -> bool:
|
|
80
|
+
"""
|
|
81
|
+
Whether the rule checks a file, according to its include/exclude globs.
|
|
82
|
+
|
|
83
|
+
Parameters
|
|
84
|
+
----------
|
|
85
|
+
path : str
|
|
86
|
+
Relative POSIX path.
|
|
87
|
+
|
|
88
|
+
Returns
|
|
89
|
+
----------
|
|
90
|
+
bool
|
|
91
|
+
True when the file is checked.
|
|
92
|
+
"""
|
|
93
|
+
if self.include and not match_any_glob(path, self.include):
|
|
94
|
+
return False
|
|
95
|
+
return not match_any_glob(path, self.exclude)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@dataclass(frozen=True, slots=True)
|
|
99
|
+
class ResolvedOverride:
|
|
100
|
+
"""
|
|
101
|
+
A path-scoped override with its selectors resolved.
|
|
102
|
+
(dataclass)
|
|
103
|
+
|
|
104
|
+
Attributes
|
|
105
|
+
----------
|
|
106
|
+
glob : str
|
|
107
|
+
Files it applies to.
|
|
108
|
+
enable : frozenset[SelectedRule]
|
|
109
|
+
Rules/checks enabled for those files.
|
|
110
|
+
disable : frozenset[SelectedRule]
|
|
111
|
+
Rules/checks disabled for those files.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
glob: str
|
|
115
|
+
enable: frozenset[SelectedRule]
|
|
116
|
+
disable: frozenset[SelectedRule]
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@dataclass(frozen=True, slots=True)
|
|
120
|
+
class ActivePlan:
|
|
121
|
+
"""
|
|
122
|
+
What a run executes: the bound rules, path overrides and run settings.
|
|
123
|
+
(dataclass)
|
|
124
|
+
|
|
125
|
+
Attributes
|
|
126
|
+
----------
|
|
127
|
+
rules : tuple[BoundRule, ...]
|
|
128
|
+
Rules that run on every file they accept.
|
|
129
|
+
dormant : tuple[BoundRule, ...]
|
|
130
|
+
Rules that are off by default but enabled by an override for some paths.
|
|
131
|
+
overrides : tuple[ResolvedOverride, ...]
|
|
132
|
+
Path-scoped overrides, in config order.
|
|
133
|
+
layer : Layer
|
|
134
|
+
Highest analysis layer needed.
|
|
135
|
+
warnings : tuple[str, ...]
|
|
136
|
+
Non-fatal remarks (skipped rules...).
|
|
137
|
+
"""
|
|
138
|
+
|
|
139
|
+
rules: tuple[BoundRule, ...]
|
|
140
|
+
dormant: tuple[BoundRule, ...] = ()
|
|
141
|
+
overrides: tuple[ResolvedOverride, ...] = ()
|
|
142
|
+
layer: Layer = Layer.REPOSITORY
|
|
143
|
+
warnings: tuple[str, ...] = field(default=())
|
|
144
|
+
|
|
145
|
+
def check_enabled_for(self, bound: BoundCheck, path: str, active_by_default: bool) -> bool:
|
|
146
|
+
"""
|
|
147
|
+
Whether a check runs on a file once path overrides are applied (later overrides win).
|
|
148
|
+
|
|
149
|
+
Parameters
|
|
150
|
+
----------
|
|
151
|
+
bound : BoundCheck
|
|
152
|
+
The check.
|
|
153
|
+
path : str
|
|
154
|
+
Relative POSIX path.
|
|
155
|
+
active_by_default : bool
|
|
156
|
+
Whether the check is active without overrides.
|
|
157
|
+
|
|
158
|
+
Returns
|
|
159
|
+
----------
|
|
160
|
+
bool
|
|
161
|
+
True when the check runs on the file.
|
|
162
|
+
"""
|
|
163
|
+
state: bool = active_by_default
|
|
164
|
+
whole: SelectedRule = SelectedRule(bound.rule.id)
|
|
165
|
+
single: SelectedRule = SelectedRule(bound.rule.id, bound.check.id)
|
|
166
|
+
for override in self.overrides:
|
|
167
|
+
if not match_glob(path, override.glob):
|
|
168
|
+
continue
|
|
169
|
+
if whole in override.enable or single in override.enable:
|
|
170
|
+
state = True
|
|
171
|
+
if whole in override.disable or single in override.disable:
|
|
172
|
+
state = False
|
|
173
|
+
return state
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _resolve_all(catalog: RuleCatalog, selectors: Iterable[str], source: str | None) -> set[SelectedRule]:
|
|
177
|
+
"""
|
|
178
|
+
Resolve several selectors.
|
|
179
|
+
|
|
180
|
+
Parameters
|
|
181
|
+
----------
|
|
182
|
+
catalog : RuleCatalog
|
|
183
|
+
Known rules.
|
|
184
|
+
selectors : Iterable[str]
|
|
185
|
+
The selectors.
|
|
186
|
+
source : str | None
|
|
187
|
+
File the selectors come from.
|
|
188
|
+
|
|
189
|
+
Returns
|
|
190
|
+
----------
|
|
191
|
+
set[SelectedRule]
|
|
192
|
+
Everything selected.
|
|
193
|
+
|
|
194
|
+
Raises
|
|
195
|
+
----------
|
|
196
|
+
UnknownSelectorError
|
|
197
|
+
A selector matches nothing.
|
|
198
|
+
"""
|
|
199
|
+
selected: set[SelectedRule] = set()
|
|
200
|
+
for selector in selectors:
|
|
201
|
+
selected.update(catalog.resolve(selector, source=source))
|
|
202
|
+
return selected
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _effective_arguments(rule: RuleDefinition, settings: RuleSettings | None, overrides: Mapping[str, str], config: ProjectConfig) -> dict[str, Any]:
|
|
206
|
+
"""
|
|
207
|
+
Effective argument values: rule defaults, then the project config, then command-line overrides.
|
|
208
|
+
|
|
209
|
+
Parameters
|
|
210
|
+
----------
|
|
211
|
+
rule : RuleDefinition
|
|
212
|
+
The rule.
|
|
213
|
+
settings : RuleSettings | None
|
|
214
|
+
Its `[rule:<id>]` section, if any.
|
|
215
|
+
overrides : Mapping[str, str]
|
|
216
|
+
Raw command-line overrides of its arguments.
|
|
217
|
+
config : ProjectConfig
|
|
218
|
+
Project config (for messages and relative paths).
|
|
219
|
+
|
|
220
|
+
Returns
|
|
221
|
+
----------
|
|
222
|
+
dict[str, Any]
|
|
223
|
+
Values by argument name.
|
|
224
|
+
|
|
225
|
+
Raises
|
|
226
|
+
----------
|
|
227
|
+
ProjectConfigError
|
|
228
|
+
An argument is unknown to the rule or has an invalid value.
|
|
229
|
+
"""
|
|
230
|
+
values: dict[str, Any] = {name: spec.default for name, spec in rule.arguments.items()}
|
|
231
|
+
layers: list[tuple[Mapping[str, str], str]] = [
|
|
232
|
+
(settings.arguments if settings is not None else {}, f"[rule:{rule.id}]"),
|
|
233
|
+
(overrides, "--override"),
|
|
234
|
+
]
|
|
235
|
+
for raw_values, origin in layers:
|
|
236
|
+
for name, raw in raw_values.items():
|
|
237
|
+
spec: ArgumentSpec | None = rule.arguments.get(name)
|
|
238
|
+
if spec is None:
|
|
239
|
+
known: str = ", ".join(rule.arguments) or "none"
|
|
240
|
+
raise ProjectConfigError(f"Unknown argument '{name}' for rule '{rule.id}' in {origin}.", f"Arguments of this rule: {known}.", path=config.source)
|
|
241
|
+
try:
|
|
242
|
+
values[name] = spec.coerce(raw, config.directory)
|
|
243
|
+
except ValueConversionError as error:
|
|
244
|
+
raise ProjectConfigError(f"Invalid value '{error.raw}' for '{name}' of rule '{rule.id}' in {origin}: {error.reason}.", path=config.source) from None
|
|
245
|
+
return values
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _bind_rule(rule: RuleDefinition, settings: RuleSettings | None, overrides: Mapping[str, str], config: ProjectConfig, check_filter: set[str] | None, disabled_checks: set[str]) -> BoundRule:
|
|
249
|
+
"""
|
|
250
|
+
Bind a rule: effective arguments, then every active check with its parameters and settings.
|
|
251
|
+
|
|
252
|
+
Parameters
|
|
253
|
+
----------
|
|
254
|
+
rule : RuleDefinition
|
|
255
|
+
The rule.
|
|
256
|
+
settings : RuleSettings | None
|
|
257
|
+
Its `[rule:<id>]` section, if any.
|
|
258
|
+
overrides : Mapping[str, str]
|
|
259
|
+
Raw command-line overrides of its arguments.
|
|
260
|
+
config : ProjectConfig
|
|
261
|
+
Project config.
|
|
262
|
+
check_filter : set[str] | None
|
|
263
|
+
Only these checks run (None: every check).
|
|
264
|
+
disabled_checks : set[str]
|
|
265
|
+
Checks disabled by selectors.
|
|
266
|
+
|
|
267
|
+
Returns
|
|
268
|
+
----------
|
|
269
|
+
BoundRule
|
|
270
|
+
The bound rule.
|
|
271
|
+
|
|
272
|
+
Raises
|
|
273
|
+
----------
|
|
274
|
+
ProjectConfigError
|
|
275
|
+
A setting is invalid for this rule.
|
|
276
|
+
"""
|
|
277
|
+
arguments: dict[str, Any] = _effective_arguments(rule, settings, overrides, config)
|
|
278
|
+
check_ids: set[str] = {check.id for check in rule.checks}
|
|
279
|
+
if settings is not None:
|
|
280
|
+
for check_id in (*settings.check_enabled, *settings.check_severity):
|
|
281
|
+
if check_id not in check_ids:
|
|
282
|
+
raise ProjectConfigError(f"Rule '{rule.id}' has no check '{check_id}'.", "Checks: " + ", ".join(sorted(check_ids)) + ".", path=config.source)
|
|
283
|
+
checks: list[BoundCheck] = []
|
|
284
|
+
for check in rule.checks:
|
|
285
|
+
enabled: bool = check.id not in disabled_checks and (check_filter is None or check.id in check_filter)
|
|
286
|
+
if settings is not None and check.id in settings.check_enabled:
|
|
287
|
+
enabled = settings.check_enabled[check.id]
|
|
288
|
+
if not enabled:
|
|
289
|
+
continue
|
|
290
|
+
primitive: Primitive = PRIMITIVES[check.kind]
|
|
291
|
+
try:
|
|
292
|
+
parameters: dict[str, Any] = bind_parameters(primitive, check, arguments)
|
|
293
|
+
except ValueError as error:
|
|
294
|
+
raise ProjectConfigError(f"Invalid arguments for check '{check.id}' of rule '{rule.id}': {error}.", path=config.source) from None
|
|
295
|
+
severity: SeverityLevel = check.severity or rule.severity
|
|
296
|
+
if settings is not None and settings.severity is not None:
|
|
297
|
+
severity = settings.severity
|
|
298
|
+
if settings is not None and check.id in settings.check_severity:
|
|
299
|
+
severity = settings.check_severity[check.id]
|
|
300
|
+
file_kinds: frozenset[FileKind] = check.applies_to or rule.applies_to
|
|
301
|
+
if check.selection.file_kinds:
|
|
302
|
+
file_kinds &= check.selection.file_kinds
|
|
303
|
+
checks.append(BoundCheck(
|
|
304
|
+
rule=rule,
|
|
305
|
+
check=check,
|
|
306
|
+
primitive=primitive,
|
|
307
|
+
parameters=MappingProxyType(parameters),
|
|
308
|
+
severity=severity,
|
|
309
|
+
detection=check.detection or rule.detection,
|
|
310
|
+
file_kinds=file_kinds,
|
|
311
|
+
))
|
|
312
|
+
return BoundRule(
|
|
313
|
+
rule=rule,
|
|
314
|
+
arguments=MappingProxyType(arguments),
|
|
315
|
+
checks=tuple(checks),
|
|
316
|
+
include=settings.include if settings is not None else (),
|
|
317
|
+
exclude=settings.exclude if settings is not None else (),
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def _rule_settings(catalog: RuleCatalog, config: ProjectConfig) -> dict[str, RuleSettings]:
|
|
322
|
+
"""
|
|
323
|
+
`[rule:<id>]` sections keyed by resolved rule ID (aliases accepted).
|
|
324
|
+
|
|
325
|
+
Parameters
|
|
326
|
+
----------
|
|
327
|
+
catalog : RuleCatalog
|
|
328
|
+
Known rules.
|
|
329
|
+
config : ProjectConfig
|
|
330
|
+
Project config.
|
|
331
|
+
|
|
332
|
+
Returns
|
|
333
|
+
----------
|
|
334
|
+
dict[str, RuleSettings]
|
|
335
|
+
Settings by rule ID.
|
|
336
|
+
|
|
337
|
+
Raises
|
|
338
|
+
----------
|
|
339
|
+
UnknownSelectorError
|
|
340
|
+
A section names an unknown rule.
|
|
341
|
+
ProjectConfigError
|
|
342
|
+
Two sections name the same rule.
|
|
343
|
+
"""
|
|
344
|
+
settings: dict[str, RuleSettings] = {}
|
|
345
|
+
for written, value in config.rules.items():
|
|
346
|
+
rule_id: str = catalog.get(written, source=config.source).id
|
|
347
|
+
if rule_id in settings:
|
|
348
|
+
raise ProjectConfigError(f"Rule '{rule_id}' is configured twice.", path=config.source)
|
|
349
|
+
settings[rule_id] = value
|
|
350
|
+
return settings
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def build_plan(
|
|
354
|
+
catalog: RuleCatalog,
|
|
355
|
+
config: ProjectConfig,
|
|
356
|
+
*,
|
|
357
|
+
only: Iterable[str] = (),
|
|
358
|
+
argument_overrides: Mapping[str, Mapping[str, str]] | None = None,
|
|
359
|
+
) -> ActivePlan:
|
|
360
|
+
"""
|
|
361
|
+
Select the active rules and bind them (plan 01, phase 6). Without `only`, the active set is every
|
|
362
|
+
non-preview rule, plus `[rules] enable`, minus `[rules] disable`, then `[rule:<id>] enabled`. With
|
|
363
|
+
`only`, exactly the selected rules/checks run.
|
|
364
|
+
|
|
365
|
+
Parameters
|
|
366
|
+
----------
|
|
367
|
+
catalog : RuleCatalog
|
|
368
|
+
Known rules.
|
|
369
|
+
config : ProjectConfig
|
|
370
|
+
Project config.
|
|
371
|
+
only : Iterable[str]
|
|
372
|
+
Selectors that replace the active set (command line `--only`).
|
|
373
|
+
argument_overrides : Mapping[str, Mapping[str, str]] | None
|
|
374
|
+
Raw argument values by rule ID or alias (command line `--override`).
|
|
375
|
+
|
|
376
|
+
Returns
|
|
377
|
+
----------
|
|
378
|
+
ActivePlan
|
|
379
|
+
The plan.
|
|
380
|
+
|
|
381
|
+
Raises
|
|
382
|
+
----------
|
|
383
|
+
UnknownSelectorError
|
|
384
|
+
A selector matches nothing.
|
|
385
|
+
ProjectConfigError
|
|
386
|
+
A setting is invalid, or two conflicting rules are active.
|
|
387
|
+
"""
|
|
388
|
+
settings: dict[str, RuleSettings] = _rule_settings(catalog, config)
|
|
389
|
+
overrides: dict[str, Mapping[str, str]] = {catalog.get(name).id: values for name, values in (argument_overrides or {}).items()}
|
|
390
|
+
only_selected: set[SelectedRule] = _resolve_all(catalog, only, None)
|
|
391
|
+
enabled: set[SelectedRule] = _resolve_all(catalog, config.enable, config.source)
|
|
392
|
+
disabled: set[SelectedRule] = _resolve_all(catalog, config.disable, config.source)
|
|
393
|
+
resolved_overrides: tuple[ResolvedOverride, ...] = tuple(
|
|
394
|
+
ResolvedOverride(item.glob, frozenset(_resolve_all(catalog, item.enable, config.source)), frozenset(_resolve_all(catalog, item.disable, config.source)))
|
|
395
|
+
for item in config.overrides
|
|
396
|
+
)
|
|
397
|
+
warnings: list[str] = []
|
|
398
|
+
active: list[BoundRule] = []
|
|
399
|
+
dormant: list[BoundRule] = []
|
|
400
|
+
override_enabled: set[str] = {selected.rule_id for item in resolved_overrides for selected in item.enable}
|
|
401
|
+
for rule in catalog.rules.values():
|
|
402
|
+
check_filter: set[str] | None = None
|
|
403
|
+
disabled_checks: set[str] = {selected.check_id for selected in disabled if selected.rule_id == rule.id and selected.check_id}
|
|
404
|
+
if only_selected:
|
|
405
|
+
chosen: list[SelectedRule] = [selected for selected in only_selected if selected.rule_id == rule.id]
|
|
406
|
+
is_active: bool = bool(chosen)
|
|
407
|
+
if chosen and all(selected.check_id for selected in chosen):
|
|
408
|
+
check_filter = {selected.check_id for selected in chosen if selected.check_id}
|
|
409
|
+
else:
|
|
410
|
+
is_active = rule.status is not RuleStatus.PREVIEW or SelectedRule(rule.id) in enabled
|
|
411
|
+
if SelectedRule(rule.id) in disabled:
|
|
412
|
+
is_active = False
|
|
413
|
+
if rule.id in settings and settings[rule.id].enabled is not None:
|
|
414
|
+
is_active = bool(settings[rule.id].enabled)
|
|
415
|
+
if not is_active and rule.id not in override_enabled:
|
|
416
|
+
continue
|
|
417
|
+
if rule.requires_jccs:
|
|
418
|
+
try:
|
|
419
|
+
compatible: bool = check_constraint(rule.requires_jccs, PROGRAM_VERSION)
|
|
420
|
+
except ValueError as error:
|
|
421
|
+
raise ProjectConfigError(f"Invalid requires_jccs of rule '{rule.id}': {error}.", path=rule.source) from None
|
|
422
|
+
if not compatible:
|
|
423
|
+
warnings.append(f"Rule '{rule.id}' needs JCCS {rule.requires_jccs}; skipped.")
|
|
424
|
+
continue
|
|
425
|
+
bound: BoundRule = _bind_rule(rule, settings.get(rule.id), overrides.get(rule.id, {}), config, check_filter, disabled_checks)
|
|
426
|
+
(active if is_active else dormant).append(bound)
|
|
427
|
+
active_ids: set[str] = {bound.rule.id for bound in active}
|
|
428
|
+
for bound in list(active):
|
|
429
|
+
for superseded in bound.rule.supersedes:
|
|
430
|
+
if superseded in active_ids:
|
|
431
|
+
warnings.append(f"Rule '{superseded}' is superseded by '{bound.rule.id}'; disabled.")
|
|
432
|
+
active_ids.discard(superseded)
|
|
433
|
+
active = [bound for bound in active if bound.rule.id in active_ids]
|
|
434
|
+
for bound in active:
|
|
435
|
+
for conflict in bound.rule.conflicts_with:
|
|
436
|
+
if conflict in active_ids:
|
|
437
|
+
raise ProjectConfigError(f"Rules '{bound.rule.id}' and '{conflict}' cannot be active together.", path=config.source)
|
|
438
|
+
every: list[BoundRule] = active + dormant
|
|
439
|
+
layer: Layer = max((check.primitive.layer for bound in every for check in bound.checks), default=Layer.REPOSITORY)
|
|
440
|
+
return ActivePlan(tuple(active), tuple(dormant), resolved_overrides, layer, tuple(warnings))
|