Reduce PythonSdkType.activateVirtualEnv(String) usages (EA-209936)

Use `PythonSdkType.activateVirtualEnv(Sdk)` instead with caching.

GitOrigin-RevId: 175afc2b52a4f50f4e63c3f33a2f89aab85ffd39
This commit is contained in:
Semyon Proshev
2019-09-03 14:59:03 +00:00
committed by intellij-monorepo-bot
parent 4a9e8ed499
commit 9dcbfb78fd
7 changed files with 22 additions and 22 deletions
@@ -165,7 +165,7 @@ public class SphinxBaseCommand {
PythonCommandLineState.initPythonPath(cmd, true, pathList, sdkHomePath);
PythonSdkType.patchCommandLineForVirtualenv(cmd, sdkHomePath, true);
PythonSdkType.patchCommandLineForVirtualenv(cmd, sdk);
BuildoutFacet facet = BuildoutFacet.getInstance(module);
if (facet != null) {
facet.patchCommandLineForBuildout(cmd);
@@ -362,7 +362,7 @@ public abstract class AbstractPythonRunConfiguration<T extends AbstractPythonRun
Sdk sdk = getSdk();
if (sdk != null && interpreterPath != null) {
patchCommandLineFirst(commandLine, interpreterPath);
patchCommandLineForVirtualenv(commandLine, interpreterPath);
patchCommandLineForVirtualenv(commandLine, sdk);
patchCommandLineForBuildout(commandLine, interpreterPath);
patchCommandLineLast(commandLine, interpreterPath);
}
@@ -391,7 +391,7 @@ public abstract class AbstractPythonRunConfiguration<T extends AbstractPythonRun
}
/**
* Gets called after {@link #patchCommandLineForVirtualenv(GeneralCommandLine, String)}
* Gets called after {@link #patchCommandLineForVirtualenv(GeneralCommandLine, Sdk)}
* Does nothing here, real implementations should use alter running script name or use engulfer.
*
* @param commandLine
@@ -404,10 +404,10 @@ public abstract class AbstractPythonRunConfiguration<T extends AbstractPythonRun
* Alters PATH so that a virtualenv is activated, if present.
*
* @param commandLine
* @param sdkHome
* @param sdk
*/
protected void patchCommandLineForVirtualenv(GeneralCommandLine commandLine, String sdkHome) {
PythonSdkType.patchCommandLineForVirtualenv(commandLine, sdkHome, isPassParentEnvs());
protected void patchCommandLineForVirtualenv(@NotNull GeneralCommandLine commandLine, @NotNull Sdk sdk) {
PythonSdkType.patchCommandLineForVirtualenv(commandLine, sdk);
}
protected void setUnbufferedEnv() {
@@ -15,7 +15,7 @@ class PyRunAnythingCommandCustomizer : RunAnythingCommandCustomizer() {
dataContext: DataContext,
commandLine: GeneralCommandLine): GeneralCommandLine {
dataContext.virtualFile?.findPythonSdk(dataContext.project)?.let { sdk ->
PythonSdkType.patchCommandLineForVirtualenv(commandLine, sdk.homePath, true)
PythonSdkType.patchCommandLineForVirtualenv(commandLine, sdk)
commandLine.findExecutableInPath()?.let {
commandLine.exePath = it
}
@@ -342,12 +342,11 @@ public final class PythonSdkType extends SdkType {
/**
* Alters PATH so that a virtualenv is activated, if present.
*
* @param commandLine what to patch
* @param sdkHome home of SDK we're using
* @param passParentEnvironment iff true, include system paths in PATH
* @param commandLine what to patch
* @param sdk SDK we're using
*/
public static void patchCommandLineForVirtualenv(GeneralCommandLine commandLine, String sdkHome, boolean passParentEnvironment) {
final Map<String, String> virtualEnv = activateVirtualEnv(sdkHome);
public static void patchCommandLineForVirtualenv(@NotNull GeneralCommandLine commandLine, @NotNull Sdk sdk) {
final Map<String, String> virtualEnv = activateVirtualEnv(sdk);
if (!virtualEnv.isEmpty()) {
final Map<String, String> environment = commandLine.getEnvironment();
@@ -361,7 +360,7 @@ public final class PythonSdkType extends SdkType {
}
}
else {
environment.put(key,value);
environment.put(key, value);
}
}
}
@@ -544,11 +543,11 @@ public final class PythonSdkType extends SdkType {
}
@NotNull
public static List<String> getSysPath(String bin_path) throws InvalidSdkException {
String working_dir = new File(bin_path).getParent();
public static List<String> getSysPath(@NotNull Sdk sdk) throws InvalidSdkException {
String working_dir = new File(sdk.getHomePath()).getParent();
Application application = ApplicationManager.getApplication();
if (application != null && (!application.isUnitTestMode() || ApplicationInfoImpl.isInStressTest())) {
return getSysPathsFromScript(bin_path);
return getSysPathsFromScript(sdk);
}
else { // mock sdk
List<String> ret = new ArrayList<>(1);
@@ -558,12 +557,13 @@ public final class PythonSdkType extends SdkType {
}
@NotNull
public static List<String> getSysPathsFromScript(@NotNull String binaryPath) throws InvalidSdkException {
public static List<String> getSysPathsFromScript(@NotNull Sdk sdk) throws InvalidSdkException {
// to handle the situation when PYTHONPATH contains ., we need to run the syspath script in the
// directory of the script itself - otherwise the dir in which we run the script (e.g. /usr/bin) will be added to SDK path
final String binaryPath = sdk.getHomePath();
GeneralCommandLine cmd = PythonHelper.SYSPATH.newCommandLine(binaryPath, Lists.newArrayList());
final ProcessOutput runResult = PySdkUtil.getProcessOutput(cmd, new File(binaryPath).getParent(),
activateVirtualEnv(binaryPath), MINUTE);
activateVirtualEnv(sdk), MINUTE);
if (!runResult.checkSuccess(LOG)) {
throw new InvalidSdkException(String.format("Failed to determine Python's sys.path value:\nSTDOUT: %s\nSTDERR: %s",
runResult.getStdout(),
@@ -450,7 +450,7 @@ public class PythonSdkUpdater implements StartupActivity {
throw new IllegalArgumentException("Cannot evaluate sys.path for remote Python interpreter " + sdk);
}
final long startTime = System.currentTimeMillis();
final List<String> sysPath = PythonSdkType.getSysPath(sdk.getHomePath());
final List<String> sysPath = PythonSdkType.getSysPath(sdk);
LOG.info("Updating sys.path took " + (System.currentTimeMillis() - startTime) + " ms");
return sysPath;
}
@@ -85,7 +85,7 @@ abstract class PyTypeShedTestCase(protected val path: String, protected val sdkP
})
val sdk = sdkVar ?: return null
val modificator = sdk.sdkModificator
val paths = PythonSdkType.getSysPathsFromScript(sdkPath)
val paths = PythonSdkType.getSysPathsFromScript(sdk)
PythonSdkUpdater.filterRootPaths(sdk, paths, project).forEach {
modificator.addRoot(it, OrderRootType.CLASSES)
}
@@ -41,7 +41,7 @@ import java.io.IOException;
/**
* Engine to create SDK for tests.
* See {@link #createTempSdk(com.intellij.openapi.vfs.VirtualFile, SdkCreationType, com.intellij.openapi.module.Module)}
* See {@link #createTempSdk(VirtualFile, SdkCreationType, Module)}
*
* @author Ilya.Kazakevich
*/
@@ -112,7 +112,7 @@ public final class PySdkTools {
modificator.setSdkAdditionalData(new PythonSdkAdditionalData(PythonSdkFlavor.getFlavor(sdk)));
for (final String path : PythonSdkType.getSysPathsFromScript(sdk.getHomePath())) {
for (final String path : PythonSdkType.getSysPathsFromScript(sdk)) {
addTestSdkRoot(modificator, path);
}
if (!addSkeletons) {