-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathPythonExtensions.java
More file actions
308 lines (282 loc) · 12.1 KB
/
PythonExtensions.java
File metadata and controls
308 lines (282 loc) · 12.1 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* SonarQube Python Plugin
* Copyright (C) 2011-2025 SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.plugins.python;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.Plugin;
import org.sonar.api.PropertyType;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarRuntime;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.resources.Qualifiers;
import org.sonar.plugins.python.api.PythonCustomRuleRepositoryWrapper;
import org.sonar.plugins.python.api.SonarLintCache;
import org.sonar.plugins.python.api.SonarLintCacheWrapper;
import org.sonar.plugins.python.architecture.ArchitectureCallbackWrapper;
import org.sonar.plugins.python.bandit.BanditRulesDefinition;
import org.sonar.plugins.python.bandit.BanditSensor;
import org.sonar.plugins.python.coverage.PythonCoverageSensor;
import org.sonar.plugins.python.editions.OpenSourceRepositoryInfoProvider;
import org.sonar.plugins.python.editions.RepositoryInfoProviderWrapper;
import org.sonar.plugins.python.flake8.Flake8RulesDefinition;
import org.sonar.plugins.python.flake8.Flake8Sensor;
import org.sonar.plugins.python.indexer.PythonIndexerWrapper;
import org.sonar.plugins.python.indexer.SonarLintPythonIndexer;
import org.sonar.plugins.python.mypy.MypyRulesDefinition;
import org.sonar.plugins.python.mypy.MypySensor;
import org.sonar.plugins.python.nosonar.NoSonarIssueFilter;
import org.sonar.plugins.python.nosonar.NoSonarLineInfoCollector;
import org.sonar.python.project.config.ProjectConfigurationBuilder;
import org.sonar.plugins.python.pylint.PylintRulesDefinition;
import org.sonar.plugins.python.pylint.PylintSensor;
import org.sonar.plugins.python.ruff.RuffRulesDefinition;
import org.sonar.plugins.python.ruff.RuffSensor;
import org.sonar.plugins.python.warnings.AnalysisWarningsWrapper;
import org.sonar.plugins.python.xunit.PythonXUnitSensor;
import static org.sonar.plugins.python.api.PythonVersionUtils.PYTHON_VERSION_KEY;
public class PythonExtensions {
private static final Logger LOG = LoggerFactory.getLogger(PythonExtensions.class);
static final String PYTHON_CATEGORY = "Python";
// Subcategories
static final String GENERAL = "General";
private static final String TEST_AND_COVERAGE = "Tests and Coverage";
private static final String EXTERNAL_ANALYZERS_CATEGORY = "External Analyzers";
private static final String DEPRECATED_PREFIX = "DEPRECATED : Use " + PythonCoverageSensor.REPORT_PATHS_KEY + " instead. ";
private PythonExtensions() {
// Utility class
}
public static void addCommonExtensions(Plugin.Context context) {
context.addExtensions(
ProjectConfigurationBuilder.class,
NoSonarLineInfoCollector.class,
NoSonarIssueFilter.class,
buildPythonSuffix(),
buildIpynbPythonSuffix(),
buildPythonVersion(),
Python.class,
PythonProfile.class,
PythonAgentQualityProfile.class,
PythonSensor.class,
PythonRuleRepository.class,
AnalysisWarningsWrapper.class,
ArchitectureCallbackWrapper.class,
PythonCustomRuleRepositoryWrapper.class,
PythonIndexerWrapper.class,
RepositoryInfoProviderWrapper.class,
SonarLintCacheWrapper.class,
IPynb.class,
IPynbProfile.class,
IPynbSensor.class,
IPynbRuleRepository.class,
OpenSourceRepositoryInfoProvider.class
);
SonarRuntime sonarRuntime = context.getRuntime();
if (sonarRuntime.getProduct() != SonarProduct.SONARLINT) {
addCoberturaExtensions(context);
addXUnitExtensions(context);
addPylintExtensions(context);
addBanditExtensions(context);
addFlake8Extensions(context);
addMypyExtensions(context);
addRuffExtensions(context);
context.addExtension(DependencyTelemetrySensor.class);
}
if (sonarRuntime.getProduct() == SonarProduct.SONARLINT) {
SonarLintPluginAPIManager sonarLintPluginAPIManager = new SonarLintPluginAPIManager();
sonarLintPluginAPIManager.addSonarlintPythonIndexer(context, new SonarLintPluginAPIVersion());
}
}
private static void addCoberturaExtensions(Plugin.Context context) {
context.addExtensions(
PropertyDefinition.builder(PythonCoverageSensor.REPORT_PATHS_KEY)
.index(20)
.name("Path to coverage report(s)")
.description("""
List of paths pointing to coverage reports. Ant patterns are accepted for relative path. \
The reports have to conform to the Cobertura XML format.""")
.category(PYTHON_CATEGORY)
.subCategory(TEST_AND_COVERAGE)
.onQualifiers(Qualifiers.PROJECT)
.defaultValue(PythonCoverageSensor.DEFAULT_REPORT_PATH)
.multiValues(true)
.build(),
// deprecated
PropertyDefinition.builder(PythonCoverageSensor.REPORT_PATH_KEY)
.index(21)
.name("Path to coverage report")
.description(DEPRECATED_PREFIX +
"Path to a coverage report. Ant patterns are accepted for relative path. The report has to conform to the Cobertura XML format.")
.category(PYTHON_CATEGORY)
.subCategory(TEST_AND_COVERAGE)
.onQualifiers(Qualifiers.PROJECT)
.defaultValue("")
.build(),
PythonCoverageSensor.class);
}
private static void addXUnitExtensions(Plugin.Context context) {
context.addExtensions(
PropertyDefinition.builder(PythonXUnitSensor.SKIP_DETAILS)
.index(23)
.name("Skip the details when importing the Xunit reports")
.description("""
When enabled the test execution statistics is provided only on project level. Use this mode when paths in report \
are not found. Disabled by default.""")
.category(PYTHON_CATEGORY)
.subCategory(TEST_AND_COVERAGE)
.onQualifiers(Qualifiers.PROJECT)
.defaultValue("false")
.type(PropertyType.BOOLEAN)
.build(),
PropertyDefinition.builder(PythonXUnitSensor.REPORT_PATH_KEY)
.index(24)
.name("Path to xunit report(s)")
.description("""
Path to the report of test execution, relative to project's root. Ant patterns are accepted. The reports have to \
conform to the junitreport XML format.""")
.category(PYTHON_CATEGORY)
.subCategory(TEST_AND_COVERAGE)
.onQualifiers(Qualifiers.PROJECT)
.defaultValue(PythonXUnitSensor.DEFAULT_REPORT_PATH)
.build(),
PythonXUnitSensor.class);
}
private static void addBanditExtensions(Plugin.Context context) {
context.addExtensions(BanditSensor.class,
PropertyDefinition.builder(BanditSensor.REPORT_PATH_KEY)
.name("Bandit Report Files")
.description("Paths (absolute or relative) to json files with Bandit issues.")
.category(EXTERNAL_ANALYZERS_CATEGORY)
.subCategory(PYTHON_CATEGORY)
.onQualifiers(Qualifiers.PROJECT)
.multiValues(true)
.build(),
BanditRulesDefinition.class);
}
private static void addPylintExtensions(Plugin.Context context) {
context.addExtensions(PylintSensor.class,
PropertyDefinition.builder(PylintSensor.REPORT_PATH_KEY)
.name("Pylint Report Files")
.description("Paths (absolute or relative) to report files with Pylint issues.")
.category(EXTERNAL_ANALYZERS_CATEGORY)
.subCategory(PYTHON_CATEGORY)
.onQualifiers(Qualifiers.PROJECT)
.multiValues(true)
.build(),
PylintRulesDefinition.class);
}
private static void addFlake8Extensions(Plugin.Context context) {
context.addExtensions(Flake8Sensor.class,
PropertyDefinition.builder(Flake8Sensor.REPORT_PATH_KEY)
.name("Flake8 Report Files")
.description("Paths (absolute or relative) to report files with Flake8 issues.")
.category(EXTERNAL_ANALYZERS_CATEGORY)
.subCategory(PYTHON_CATEGORY)
.onQualifiers(Qualifiers.PROJECT)
.multiValues(true)
.build(),
Flake8RulesDefinition.class);
}
private static void addMypyExtensions(Plugin.Context context) {
context.addExtensions(MypySensor.class,
PropertyDefinition.builder(MypySensor.REPORT_PATH_KEY)
.name("Mypy Report Files")
.description("Paths (absolute or relative) to report files with Mypy issues.")
.category(EXTERNAL_ANALYZERS_CATEGORY)
.subCategory(PYTHON_CATEGORY)
.onQualifiers(Qualifiers.PROJECT)
.multiValues(true)
.build(),
MypyRulesDefinition.class);
}
private static void addRuffExtensions(Plugin.Context context) {
context.addExtensions(RuffSensor.class,
PropertyDefinition.builder(RuffSensor.REPORT_PATH_KEY)
.name("Ruff Report Files")
.description("Paths (absolute or relative) to report files with Ruff issues.")
.category(EXTERNAL_ANALYZERS_CATEGORY)
.subCategory(PYTHON_CATEGORY)
.onQualifiers(Qualifiers.PROJECT)
.multiValues(true)
.build(),
RuffRulesDefinition.class);
}
private static PropertyDefinition buildPythonVersion() {
return PropertyDefinition.builder(PYTHON_VERSION_KEY)
.index(12)
.name("Python versions")
.description("Comma-separated list of Python versions this project is compatible with.")
.multiValues(true)
.category(PYTHON_CATEGORY)
.subCategory(GENERAL)
.onQualifiers(Qualifiers.PROJECT)
.build();
}
private static PropertyDefinition buildIpynbPythonSuffix() {
return PropertyDefinition.builder(PythonExtensionKeys.IPYNB_FILE_SUFFIXES_KEY)
.index(11)
.name("IPython File Suffixes")
.description("List of suffixes of IPython Notebooks files to analyze.")
.multiValues(true)
.category(PYTHON_CATEGORY)
.subCategory(GENERAL)
.onQualifiers(Qualifiers.PROJECT)
.defaultValue("ipynb")
.build();
}
private static PropertyDefinition buildPythonSuffix() {
return PropertyDefinition.builder(PythonExtensionKeys.PYTHON_FILE_SUFFIXES_KEY)
.index(10)
.name("File Suffixes")
.description("List of suffixes of Python files to analyze.")
.multiValues(true)
.category(PYTHON_CATEGORY)
.subCategory(GENERAL)
.onQualifiers(Qualifiers.PROJECT)
.defaultValue("py")
.build();
}
static class SonarLintPluginAPIManager {
public void addSonarlintPythonIndexer(Plugin.Context context, SonarLintPluginAPIVersion sonarLintPluginAPIVersion) {
if (sonarLintPluginAPIVersion.isDependencyAvailable()) {
// Only SonarLintPythonIndexer has the ModuleFileListener dependency.
// However, SonarLintCache can only be used with SonarLintPythonIndexer present at the moment.
// Hence, we also add it here, even if it technically does not share the dependency.
//
// Furthermore, with recent versions of SonarLint, the ModuleFileListener dependency should always be available.
//
// Attention:
// The constructors of PythonSensor currently expect both, SonarLintCache and SonarLintPythonIndexer, to always be available at the
// same time for injection.
// Thus, some care is required when making changes to the addExtension calls here.
context.addExtension(SonarLintCache.class);
context.addExtension(SonarLintPythonIndexer.class);
} else {
LOG.debug("Error while trying to inject SonarLintPythonIndexer");
}
}
}
static class SonarLintPluginAPIVersion {
boolean isDependencyAvailable() {
try {
Class.forName("org.sonarsource.sonarlint.plugin.api.module.file.ModuleFileListener");
} catch (ClassNotFoundException e) {
return false;
}
return true;
}
}
}