Setup Python SDK and facet to run inspections on Python projects

GitOrigin-RevId: f1b3be8207ae5ed8674ff81ca02fcb26d7b4afe4
This commit is contained in:
Dmitry Trofimov
2019-09-19 18:24:50 +02:00
committed by intellij-monorepo-bot
parent 7bec7f77c9
commit bf6cbda222
8 changed files with 110 additions and 4 deletions

View File

@@ -0,0 +1,79 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.inspections;
import com.intellij.codeInspection.CommandLineInspectionProjectConfigurator;
import com.intellij.facet.FacetManager;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.util.ui.UIUtil;
import com.jetbrains.python.facet.PythonFacetType;
import com.jetbrains.python.sdk.*;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public class PythonPluginCommandLineInspectionProjectConfigurator implements CommandLineInspectionProjectConfigurator {
@Override
public boolean isApplicable(Path projectPath) {
System.out.println("Python is here");
try {
return Files.walk(projectPath).anyMatch(f -> {
return f.toString().endsWith(".py");
});
}
catch (IOException e) {
return false;
}
}
@Override
public void configureEnvironment() {
System.out.println("Python environment configuration...");
List<Sdk> sdks = PythonSdkUtil.getAllSdks();
System.out.println("Python interpreters detected:");
for (Sdk sdk : sdks) {
System.out.println(sdk.getHomePath());
}
if (sdks.isEmpty()) {
final List<Sdk> detectedSdks = PySdkExtKt.findAllPythonSdks();
if (detectedSdks.size() > 0) {
for (Sdk sdk : detectedSdks) {
System.out.println(sdk.getHomePath());
}
final Sdk sdk = detectedSdks.get(0);
ApplicationManager.getApplication().runWriteAction(() -> {
System.out.println("Settings up interpreter " + sdk.getName());
ProjectJdkTable.getInstance().addJdk(sdk);
});
PythonSdkUpdater.update(sdk, null, null, null);
} else {
System.out.println("ERROR: Can't find Python interpreter");
}
}
}
@Override
public void configureProject(@NotNull Project project) {
List<Sdk> sdks = PythonSdkUtil.getAllSdks();
if (!sdks.isEmpty()) {
for (Module m : ModuleManager.getInstance(project).getModules()) {
PythonFacetType facetType = PythonFacetType.getInstance();
ApplicationManager.getApplication().runWriteAction(() -> {
FacetManager.getInstance(m).addFacet(facetType, facetType.getPresentableName(), null);
});
}
}
}
}