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

@@ -9,12 +9,25 @@ import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
/**
* @author yole
*/
public class JavaCommandLineInspectionProjectConfigurator implements CommandLineInspectionProjectConfigurator {
@Override
public boolean isApplicable(Path projectPath) {
try {
return Files.walk(projectPath).anyMatch(f -> f.toString().endsWith(".java"));
}
catch (IOException e) {
return false;
}
}
@Override
public void configureEnvironment() {
JavaSdk javaSdk = JavaSdk.getInstance();

View File

@@ -5,12 +5,16 @@ import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
/**
* @author yole
*/
public interface CommandLineInspectionProjectConfigurator {
ExtensionPointName<CommandLineInspectionProjectConfigurator> EP_NAME = ExtensionPointName.create("com.intellij.commandLineInspectionProjectConfigurator");
boolean isApplicable(Path projectPath);
/**
* Invoked before a project is imported.
*/

View File

@@ -142,8 +142,10 @@ public class InspectionApplication {
return;
}
for (CommandLineInspectionProjectConfigurator configurator : CommandLineInspectionProjectConfigurator.EP_NAME.getIterable()) {
configurator.configureEnvironment();
for (CommandLineInspectionProjectConfigurator configurator : CommandLineInspectionProjectConfigurator.EP_NAME.getExtensionList()) {
if (configurator.isApplicable(projectPath)) {
configurator.configureEnvironment();
}
}
Project project = ProjectUtil.openOrImport(projectPath, null, false);

View File

@@ -12,6 +12,8 @@
<!-- Console folding for Jython only, thus it's located in intellij.python.plugin only -->
<stacktrace.fold substring="*sys-package-mgr*:"/>
<sdkEditorAdditionalOptionsProvider implementation="com.jetbrains.python.PythonSdkEditorAdditionalOptionsProvider"/>
<commandLineInspectionProjectConfigurator implementation="com.jetbrains.python.inspections.PythonPluginCommandLineInspectionProjectConfigurator"/>
</extensions>
<extensions defaultExtensionNs="Pythonid">

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);
});
}
}
}
}

View File

@@ -49,6 +49,12 @@ import java.nio.file.Paths
* @author vlan
*/
fun findAllPythonSdks(): List<Sdk> {
val context: UserDataHolder = UserDataHolderBase()
val existing = PythonSdkUtil.getAllSdks()
return detectCondaEnvs(null, existing, context) + detectVirtualEnvs(null, existing, context) + findBaseSdks(existing, null, context)
}
fun findBaseSdks(existingSdks: List<Sdk>, module: Module?, context: UserDataHolder): List<Sdk> {
val existing = existingSdks.filter { it.sdkType is PythonSdkUtil && it.isSystemWide }
val detected = detectSystemWideSdks(module, existingSdks, context)

View File

@@ -131,7 +131,7 @@ public class PySdkUtil {
if (SwingUtilities.isEventDispatchThread()) {
final ProgressManager progressManager = ProgressManager.getInstance();
final Application application = ApplicationManager.getApplication();
assert application.isUnitTestMode() || !application.isWriteAccessAllowed() : "Background task can't be run under write action";
assert application.isUnitTestMode() || application.isHeadlessEnvironment() || !application.isWriteAccessAllowed() : "Background task can't be run under write action";
return progressManager.runProcessWithProgressSynchronously(() -> processHandler.runProcess(timeout), "Wait...", false, null);
}
else {

View File

@@ -106,7 +106,7 @@ public class PythonSdkUpdater implements StartupActivity.Background {
@Nullable final Component ownerComponent) {
final Application application = ApplicationManager.getApplication();
assert !application.isWriteAccessAllowed() : "write actions are not allowed in non-EDT threads";
assert !application.isWriteAccessAllowed() : "sdk update should not be run in edt neither under write action";
final String key = PythonSdkType.getSdkKey(sdk);
synchronized (ourLock) {