-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathPythonScanner.java
More file actions
466 lines (410 loc) · 19.4 KB
/
PythonScanner.java
File metadata and controls
466 lines (410 loc) · 19.4 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* SonarQube Python Plugin
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* 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 com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.RecognitionException;
import java.io.File;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.SonarProduct;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.issue.NoSonarFilter;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.plugins.python.api.PythonCheck;
import org.sonar.plugins.python.api.PythonFile;
import org.sonar.plugins.python.api.PythonFileConsumer;
import org.sonar.plugins.python.api.PythonInputFileContext;
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
import org.sonar.plugins.python.api.PythonVisitorContext;
import org.sonar.plugins.python.api.internal.EndOfAnalysis;
import org.sonar.plugins.python.api.tree.FileInput;
import org.sonar.plugins.python.cpd.PythonCpdAnalyzer;
import org.sonar.plugins.python.indexer.PythonIndexer;
import org.sonar.plugins.python.nosonar.NoSonarLineInfoCollector;
import org.sonar.plugins.python.telemetry.collectors.TestFileTelemetry;
import org.sonar.plugins.python.telemetry.collectors.TestFileTelemetryCollector;
import org.sonar.plugins.python.telemetry.collectors.TypeInferenceTelemetry;
import org.sonar.plugins.python.telemetry.collectors.TypeInferenceTelemetryCollector;
import org.sonar.plugins.python.warnings.AnalysisWarningsWrapper;
import org.sonar.python.IPythonLocation;
import org.sonar.python.SubscriptionVisitor;
import org.sonar.python.parser.PythonParser;
import org.sonar.python.tree.IPythonTreeMaker;
import org.sonar.python.tree.PythonTreeMaker;
public class PythonScanner extends Scanner {
private static final Logger LOG = LoggerFactory.getLogger(PythonScanner.class);
private static final Pattern DATABRICKS_MAGIC_COMMAND_PATTERN = Pattern.compile("^\\h*#\\h*(MAGIC|COMMAND).*");
private static final String ARCHITECTURE_CALLBACK_LOCK_KEY = "architectureCallbackLock";
private final Supplier<PythonParser> parserSupplier;
private final PythonChecks checks;
private final PythonCpdAnalyzer cpdAnalyzer;
private final PythonIndexer indexer;
private final Map<PythonInputFile, Set<Class<? extends PythonCheck>>> checksExecutedWithoutParsingByFiles;
private final AtomicInteger recognitionErrorCount;
private final AtomicBoolean foundDatabricks;
private final PythonFileConsumer architectureCallback;
private final Map<String, Lock> repositoryLocks;
private final NewSymbolsCollector newSymbolsCollector;
private final PythonHighlighter pythonHighlighter;
private final IssuesRepository issuesRepository;
private final MeasuresRepository measuresRepository;
private final NoSonarLineInfoCollector noSonarLineInfoCollector;
private final TypeInferenceTelemetryCollector typeInferenceTelemetryCollector;
private final TestFileTelemetryCollector testFileTelemetryCollector;
private final boolean testSourcesConfigured;
private final AnalysisWarningsWrapper analysisWarnings;
private final AtomicBoolean heuristicWarningEmitted = new AtomicBoolean(false);
private volatile int filesScannedWithCache = 0;
static final String UNSET_SONAR_TESTS_WARNING = "SonarPython detected files that look like test code " +
"but 'sonar.tests' is not configured. Rules targeting production code were not executed on these files. " +
"Configure 'sonar.tests' in your project properties for a more accurate analysis.";
public PythonScanner(
SensorContext context, PythonChecks checks, FileLinesContextFactory fileLinesContextFactory, NoSonarFilter noSonarFilter,
Supplier<PythonParser> parserSupplier, PythonIndexer indexer, PythonFileConsumer architectureCallback,
NoSonarLineInfoCollector noSonarLineInfoCollector, AnalysisWarningsWrapper analysisWarnings) {
super(context);
this.checks = checks;
this.parserSupplier = parserSupplier;
this.indexer = indexer;
this.noSonarLineInfoCollector = noSonarLineInfoCollector;
this.analysisWarnings = analysisWarnings;
this.testSourcesConfigured = TestFileClassifier.isTestSourceConfigured(context.config());
this.indexer.buildOnce(context);
this.architectureCallback = architectureCallback;
this.checksExecutedWithoutParsingByFiles = new ConcurrentHashMap<>();
this.recognitionErrorCount = new AtomicInteger(0);
this.foundDatabricks = new AtomicBoolean(false);
this.repositoryLocks = new ConcurrentHashMap<>();
var lock = new ReentrantLock();
this.cpdAnalyzer = new PythonCpdAnalyzer(context, lock);
this.newSymbolsCollector = new NewSymbolsCollector(lock);
this.pythonHighlighter = new PythonHighlighter(lock);
this.issuesRepository = new IssuesRepository(context, checks, indexer, isInSonarLint(context), lock);
this.measuresRepository = new MeasuresRepository(context, noSonarFilter, fileLinesContextFactory, isInSonarLint(context), noSonarLineInfoCollector, lock);
this.typeInferenceTelemetryCollector = new TypeInferenceTelemetryCollector();
this.testFileTelemetryCollector = new TestFileTelemetryCollector();
}
@Override
protected String name() {
return "rules execution";
}
@Override
protected void logStart(int numThreads) {
LOG.debug("Scanning files in {} threads", numThreads);
}
@Override
protected void scanFile(PythonInputFile inputFile) throws IOException {
var pythonFile = SonarQubePythonFile.create(inputFile);
InputFile.Type fileType = inputFile.wrappedFile().type();
PythonVisitorContext visitorContext = createVisitorContext(inputFile, pythonFile);
InputFile.Type effectiveTypeForRules = resolveEffectiveTypeForRules(
fileType, projectRelativePath(inputFile), visitorContext.rootTree());
if (!testSourcesConfigured && fileType == InputFile.Type.MAIN) {
indexer.writeEffectiveFileType(inputFile.wrappedFile().key(), effectiveTypeForRules);
}
executeChecks(visitorContext, checks.sonarPythonChecks(), effectiveTypeForRules, inputFile);
executeOtherChecks(inputFile, visitorContext, effectiveTypeForRules);
runLockedByRepository(ARCHITECTURE_CALLBACK_LOCK_KEY, () -> architectureCallback.scanFile(visitorContext));
noSonarLineInfoCollector.collect(pythonFile.key(), visitorContext.rootTree());
if (visitorContext.rootTree() != null) {
if (fileType == InputFile.Type.MAIN) {
pushTokens(inputFile, visitorContext);
measuresRepository.save(inputFile, visitorContext);
} else if (fileType == InputFile.Type.TEST) {
measuresRepository.saveNclocForTestFile(inputFile, visitorContext);
}
}
var issues = visitorContext.getIssues();
issuesRepository.save(inputFile, issues);
if (visitorContext.rootTree() != null && !isInSonarLint(context)) {
newSymbolsCollector.collect(context.newSymbolTable().onFile(inputFile.wrappedFile()), visitorContext.rootTree());
pythonHighlighter.highlight(context, visitorContext, inputFile);
typeInferenceTelemetryCollector.collect(visitorContext.rootTree());
testFileTelemetryCollector.collect(visitorContext.rootTree(), fileType, inputFile.wrappedFile().uri().getPath());
}
searchForDataBricks(visitorContext);
}
private PythonVisitorContext createVisitorContext(PythonInputFile inputFile, PythonFile pythonFile) throws IOException {
PythonVisitorContext visitorContext;
try {
AstNode astNode = parserSupplier.get().parse(inputFile.contents());
PythonTreeMaker treeMaker = getTreeMaker(inputFile);
FileInput parse = treeMaker.fileInput(astNode);
visitorContext = new PythonVisitorContext.Builder(parse, pythonFile)
.projectConfiguration(indexer.projectConfig())
.workingDirectory(getWorkingDirectory(context))
.packageName(indexer.packageName(inputFile))
.projectLevelSymbolTable(indexer.projectLevelSymbolTable())
.typeTable(indexer.projectLevelTypeTable())
.cacheContext(indexer.cacheContext())
.sonarProduct(context.runtime().getProduct())
.build();
} catch (RecognitionException e) {
visitorContext = new PythonVisitorContext(pythonFile, e, context.runtime().getProduct());
var line = (inputFile.kind() == PythonInputFile.Kind.IPYTHON) ?
((GeneratedIPythonFile) inputFile).locationMap().get(e.getLine()).line() : e.getLine();
var newMessage = e.getMessage().replace("line " + e.getLine(), "line " + line);
LOG.error("Unable to parse file: {}", inputFile);
LOG.error(newMessage);
recognitionErrorCount.incrementAndGet();
context.newAnalysisError()
.onFile(inputFile.wrappedFile())
.at(inputFile.wrappedFile().newPointer(line, 0))
.message(newMessage)
.save();
}
return visitorContext;
}
private void pushTokens(PythonInputFile inputFile, PythonVisitorContext visitorContext) {
if (!isInSonarLint(context) && inputFile.kind() == PythonInputFile.Kind.PYTHON) {
cpdAnalyzer.pushCpdTokens(inputFile.wrappedFile(), visitorContext);
}
}
private void executeChecks(PythonVisitorContext visitorContext, Collection<PythonCheck> checks, InputFile.Type fileType,
PythonInputFile inputFile) {
Collection<PythonSubscriptionCheck> subscriptionChecks = new ArrayList<>();
for (PythonCheck check : checks) {
if (isCheckNotApplicable(check, fileType)
|| checksExecutedWithoutParsingByFiles.getOrDefault(inputFile, Collections.emptySet()).contains(check.getClass())) {
continue;
}
if (check instanceof PythonSubscriptionCheck pythonSubscriptionCheck) {
subscriptionChecks.add(pythonSubscriptionCheck);
} else {
check.scanFile(visitorContext);
}
}
SubscriptionVisitor.analyze(subscriptionChecks, visitorContext);
}
private void executeOtherChecks(PythonInputFile inputFile, PythonVisitorContext visitorContext, InputFile.Type fileType) {
checks.noSonarPythonChecks()
.forEach((repositoryKey, repositoryChecks) -> runLockedByRepository(repositoryKey, () -> executeChecks(visitorContext, repositoryChecks, fileType, inputFile)));
}
private void searchForDataBricks(PythonVisitorContext visitorContext) {
var hasDatabricks = visitorContext.pythonFile().content().lines().anyMatch(
line -> DATABRICKS_MAGIC_COMMAND_PATTERN.matcher(line).matches());
foundDatabricks.compareAndSet(false, hasDatabricks);
}
private static PythonTreeMaker getTreeMaker(PythonInputFile inputFile) {
return Python.KEY.equals(inputFile.wrappedFile().language()) ? new PythonTreeMaker() :
new IPythonTreeMaker(getOffsetLocations(inputFile));
}
private static Map<Integer, IPythonLocation> getOffsetLocations(PythonInputFile inputFile) {
if (inputFile.kind() == PythonInputFile.Kind.IPYTHON) {
return ((GeneratedIPythonFile) inputFile).locationMap();
}
return Map.of();
}
@Override
public boolean scanFileWithoutParsing(PythonInputFile inputFile) {
InputFile.Type fileType = inputFile.wrappedFile().type();
InputFile.Type effectiveTypeForRules = resolveEffectiveTypeForRulesFromCache(
fileType, projectRelativePath(inputFile), inputFile.wrappedFile().key());
if (effectiveTypeForRules == null) {
return false;
}
boolean result = true;
PythonFile pythonFile = SonarQubePythonFile.create(inputFile.wrappedFile());
PythonInputFileContext inputFileContext = new PythonInputFileContext(
pythonFile,
context.fileSystem().workDir(),
indexer.cacheContext(),
context.runtime().getProduct(),
indexer.projectLevelSymbolTable()
);
result = scanFileWithoutParsingSonarPython(inputFile, effectiveTypeForRules, inputFileContext, result);
var atomicResult = new AtomicBoolean(result);
var otherChecks = checks.noSonarPythonChecks();
otherChecks.forEach((repositoryKey, repositoryChecks) -> runLockedByRepository(repositoryKey, () -> {
for (var check : repositoryChecks) {
var scanResult = scanFileWithoutParsingNotSonarPython(inputFile, check, effectiveTypeForRules, atomicResult.get(), inputFileContext);
atomicResult.set(scanResult);
}
}));
result = atomicResult.get();
result &= architectureCallback.scanWithoutParsing(inputFileContext);
if (!result) {
// If scan without parsing is not successful, measures will be pushed during regular scan.
// We must avoid pushing measures twice due to the risk of duplicate cache key error.
return false;
}
boolean success = restoreAndPushMeasuresIfApplicable(inputFile);
if (success && !testSourcesConfigured && fileType == InputFile.Type.MAIN) {
indexer.writeEffectiveFileType(inputFile.wrappedFile().key(), effectiveTypeForRules);
}
return success;
}
private boolean scanFileWithoutParsingNotSonarPython(PythonInputFile inputFile, PythonCheck check, InputFile.Type fileType,
boolean result,
PythonInputFileContext inputFileContext) {
if (isCheckNotApplicable(check, fileType)) {
return result;
}
if (!indexer.canBeFullyScannedWithoutParsing(inputFile)) {
result = false;
return result;
}
if (check.scanWithoutParsing(inputFileContext)) {
var executedChecks = checksExecutedWithoutParsingByFiles.getOrDefault(inputFile, new HashSet<>());
executedChecks.add(check.getClass());
checksExecutedWithoutParsingByFiles.putIfAbsent(inputFile, executedChecks);
} else {
result = false;
}
return result;
}
private boolean scanFileWithoutParsingSonarPython(PythonInputFile inputFile, InputFile.Type fileType,
PythonInputFileContext inputFileContext, boolean result) {
var ourChecks = checks.sonarPythonChecks();
for (var check : ourChecks) {
if (isCheckNotApplicable(check, fileType)) {
continue;
}
if (check.scanWithoutParsing(inputFileContext)) {
var executedChecks = checksExecutedWithoutParsingByFiles.getOrDefault(inputFile, new HashSet<>());
executedChecks.add(check.getClass());
checksExecutedWithoutParsingByFiles.putIfAbsent(inputFile, executedChecks);
} else {
result = false;
}
}
return result;
}
@Override
public void endOfAnalysis() {
indexer.postAnalysis(context);
checks.sonarPythonEndOfAnalyses().forEach(c -> c.endOfAnalysis(indexer.cacheContext()));
checks.noSonarPythonEndOfAnalyses().forEach(this::endOfAnalysisForRepository);
}
private void endOfAnalysisForRepository(String repositoryKey, List<EndOfAnalysis> endOfAnalyses) {
runLockedByRepository(repositoryKey, () -> endOfAnalyses.forEach(c -> c.endOfAnalysis(indexer.cacheContext())));
}
private String projectRelativePath(PythonInputFile inputFile) {
return context.fileSystem().baseDir().toURI()
.relativize(inputFile.wrappedFile().uri())
.toString();
}
private boolean isBypassed(InputFile.Type platformType) {
return testSourcesConfigured || platformType == InputFile.Type.TEST;
}
private InputFile.Type resolveEffectiveTypeForRules(InputFile.Type platformType, String filePath, @Nullable FileInput tree) {
if (isBypassed(platformType)) {
return platformType;
}
boolean isTest = TestFileClassifier.looksLikeTestFile(filePath, tree);
if (isTest) {
maybeEmitHeuristicWarning();
return InputFile.Type.TEST;
}
return InputFile.Type.MAIN;
}
@Nullable
private InputFile.Type resolveEffectiveTypeForRulesFromCache(InputFile.Type platformType, String filePath, String fileKey) {
if (isBypassed(platformType)) {
return platformType;
}
InputFile.Type cached = indexer.readEffectiveFileType(fileKey);
if (cached == null) {
// Cache entry is missing while the file is unchanged — the cache is in an inconsistent state
// This should never happen in normal operation. Fall back to a full parse rather than guessing the type.
LOG.debug("No cached effective file type for '{}': triggering full parse", filePath);
}
return cached;
}
private void maybeEmitHeuristicWarning() {
if (heuristicWarningEmitted.compareAndSet(false, true)) {
LOG.warn(UNSET_SONAR_TESTS_WARNING);
analysisWarnings.addUnique(UNSET_SONAR_TESTS_WARNING);
}
}
boolean isCheckNotApplicable(PythonCheck pythonCheck, InputFile.Type fileType) {
return fileType != InputFile.Type.MAIN && pythonCheck.scope() != PythonCheck.CheckScope.ALL;
}
// visible for testing
static File getWorkingDirectory(SensorContext context) {
return isInSonarLint(context) ? null : context.fileSystem().workDir();
}
private static boolean isInSonarLint(SensorContext context) {
return context.runtime().getProduct().equals(SonarProduct.SONARLINT);
}
@Override
protected void processException(Exception e, PythonInputFile file) {
LOG.warn("Unable to analyze file: " + file, e);
}
@Override
public boolean canBeScannedWithoutParsing(PythonInputFile inputFile) {
return this.indexer.canBePartiallyScannedWithoutParsing(inputFile);
}
@Override
protected void reportStatistics(int numSkippedFiles, int numTotalFiles) {
filesScannedWithCache = numSkippedFiles;
LOG.info("""
The Python analyzer was able to leverage cached data from previous analyses for {} out of {} files. These files were not \
parsed.""",
numSkippedFiles, numTotalFiles);
}
private boolean restoreAndPushMeasuresIfApplicable(PythonInputFile inputFile) {
if (inputFile.wrappedFile().type() == InputFile.Type.TEST) {
return true;
}
return cpdAnalyzer.pushCachedCpdTokens(inputFile.wrappedFile(), indexer.cacheContext());
}
public int getRecognitionErrorCount() {
return recognitionErrorCount.get();
}
public boolean getFoundDatabricks() {
return foundDatabricks.get();
}
public TypeInferenceTelemetry getTypeInferenceTelemetry() {
return typeInferenceTelemetryCollector.getTelemetry();
}
public TestFileTelemetry getTestFileTelemetry() {
return testFileTelemetryCollector.getTelemetry();
}
public boolean wasTestFileHeuristicTriggered() {
return heuristicWarningEmitted.get();
}
public int getFilesScannedWithCache() {
return filesScannedWithCache;
}
private void runLockedByRepository(String repositoryKey, Runnable runnable) {
var repositoryLock = repositoryLocks.computeIfAbsent(repositoryKey, k -> new ReentrantLock());
try {
repositoryLock.lock();
runnable.run();
} finally {
repositoryLock.unlock();
}
}
}