Increase usage of cached sdk env

This commit is contained in:
Semyon Proshev
2019-03-20 19:53:51 +03:00
parent b3436a8799
commit 8a9fdccfb0
7 changed files with 20 additions and 16 deletions
@@ -51,7 +51,7 @@ class PyVirtualEnvTerminalCustomizer : LocalTerminalCustomizer() {
}
else {
//for other shells we read envs from activate script by the default shell and pass them to the process
envs.putAll(PythonSdkType.activateVirtualEnv(path))
envs.putAll(PythonSdkType.activateVirtualEnv(sdk))
}
}
}
@@ -175,7 +175,7 @@ public class GenerateBinaryStubsFix implements LocalQuickFix {
GeneralCommandLine cmd = PythonHelper.EXTRA_SYSPATH.newCommandLine(homePath, Lists.newArrayList(myQualifiedName));
final ProcessOutput runResult = PySdkUtil.getProcessOutput(cmd,
new File(homePath).getParent(),
PythonSdkType.activateVirtualEnv(homePath), 5000
PythonSdkType.activateVirtualEnv(mySdk), 5000
);
if (runResult.getExitCode() == 0 && !runResult.isTimeout()) {
final String extraPath = runResult.getStdout();
@@ -369,14 +369,7 @@ public abstract class PythonCommandLineState extends CommandLineState {
Sdk sdk = PythonSdkType.findSdkByPath(sdkHome);
if (sdk != null &&
(Registry.is("python.activate.virtualenv.on.run") && PythonSdkType.isVirtualEnv(sdkHome) || PythonSdkType.isConda(sdk))) {
Map<String, String> environment = sdk.getUserData(PythonSdkType.ENVIRONMENT_KEY);
if (environment == null) {
environment = PythonSdkType.activateVirtualEnv(sdkHome);
sdk.putUserData(PythonSdkType.ENVIRONMENT_KEY, environment);
}
Map<String, String> environment = PythonSdkType.activateVirtualEnv(sdk);
env.putAll(environment);
for (Map.Entry<String, String> e : myConfig.getEnvs().entrySet()) {
@@ -101,7 +101,7 @@ public final class PythonSdkType extends SdkType {
private static final Key<WeakReference<Component>> SDK_CREATOR_COMPONENT_KEY = Key.create("#com.jetbrains.python.sdk.creatorComponent");
private static final Predicate<Sdk> REMOTE_SDK_PREDICATE = PythonSdkType::isRemote;
public static final Key<Map<String, String>> ENVIRONMENT_KEY = Key.create("ENVIRONMENT_KEY");
private static final Key<Map<String, String>> ENVIRONMENT_KEY = Key.create("ENVIRONMENT_KEY");
public static PythonSdkType getInstance() {
return SdkType.findInstance(PythonSdkType.class);
@@ -972,6 +972,18 @@ public final class PythonSdkType extends SdkType {
return !isRemote(sdk);
}
@NotNull
public static Map<String, String> activateVirtualEnv(@NotNull Sdk sdk) {
final Map<String, String> cached = sdk.getUserData(ENVIRONMENT_KEY);
if (cached != null) return cached;
final String sdkHome = sdk.getHomePath();
if (sdkHome == null) return Collections.emptyMap();
final Map<String, String> environment = activateVirtualEnv(sdkHome);
sdk.putUserData(ENVIRONMENT_KEY, environment);
return environment;
}
@NotNull
public static Map<String, String> activateVirtualEnv(@NotNull String sdkHome) {
@@ -122,8 +122,7 @@ public class PythonSdkUpdater implements StartupActivity {
String sdkHome = sdk.getHomePath();
if (sdkHome != null && (PythonSdkType.isVirtualEnv(sdkHome) || PythonSdkType.isConda(sdk))) {
final Future<?> updateSdkFeature = application.executeOnPooledThread(() -> {
sdk.putUserData(PythonSdkType.ENVIRONMENT_KEY,
PythonSdkType.activateVirtualEnv(sdkHome)); // pre-cache virtualenv activated environment
PythonSdkType.activateVirtualEnv(sdk); // pre-cache virtualenv activated environment
});
if (ApplicationManager.getApplication().isUnitTestMode()) {
// Running SDK update in background is inappropriate for tests: test may complete before update and updater thread will leak
@@ -21,7 +21,7 @@ fun runConda(condaExecutable: String, arguments: List<String>): ProcessOutput {
@Throws(PyExecutionException::class)
fun runConda(sdk: Sdk, arguments: List<String>): ProcessOutput {
return findCondaExecutable(sdk).let { run(it, arguments, sdk.getUserData(PythonSdkType.ENVIRONMENT_KEY) ?: readCondaEnv(it)) }
return run(findCondaExecutable(sdk), arguments, PythonSdkType.activateVirtualEnv(sdk))
}
@Throws(PyExecutionException::class)
@@ -212,7 +212,7 @@ public class PySkeletonGenerator {
"-d", mySkeletonsPath, // output dir
"-b", // for builtins
},
PythonSdkType.activateVirtualEnv(binaryPath), MINUTE * 5
PythonSdkType.activateVirtualEnv(sdk), MINUTE * 5
);
runResult.checkSuccess(LOG);
LOG.info("Rebuilding builtin skeletons took " + (System.currentTimeMillis() - startTime) + " ms");
@@ -233,7 +233,7 @@ public class PySkeletonGenerator {
final ProcessOutput process = getProcessOutput(parentDir,
ArrayUtil.toStringArray(cmd),
PythonSdkType.activateVirtualEnv(homePath),
PythonSdkType.activateVirtualEnv(sdk),
MINUTE * 4); // see PY-3898
LOG.info("Retrieving binary module list took " + (System.currentTimeMillis() - startTime) + " ms");