Pass shell environment before obtaining virtualenv environment for script execution (PY-17816)

If an IDE is executed not terminal on macOS it doesn't get shell environment and this prevents getting the correct environment by virtualenv activation
This commit is contained in:
Dmitry Trofimov
2017-04-11 19:48:32 +02:00
parent 7fad69fc05
commit bf98ba3576
2 changed files with 29 additions and 5 deletions
@@ -187,25 +187,44 @@ public class EnvironmentUtil {
LOG.info("loading shell env: " + StringUtil.join(command, " "));
return runProcessAndReadEnvs(command, envFile, "\0");
return dumpProcessEnvToFile(command, envFile, "\0");
}
finally {
FileUtil.delete(envFile);
}
}
@NotNull
protected Map<String, String> dumpProcessEnvToFile(@NotNull List<String> command, @NotNull File envFile, String lineSeparator)
throws Exception {
return runProcessAndReadEnvs(command, envFile, lineSeparator);
}
@NotNull
protected static Map<String, String> runProcessAndReadEnvs(@NotNull List<String> command, @NotNull File envFile, String lineSeparator)
throws Exception {
return runProcessAndReadEnvs(command, null, envFile, lineSeparator);
}
@NotNull
@NotNull
protected static Map<String, String> runProcessAndReadEnvs(@NotNull List<String> command,
@Nullable File workingDir,
@NotNull File envFile,
@Nullable File workingDir,
@NotNull File envFile,
String lineSeparator) throws Exception {
return runProcessAndReadEnvs(command, workingDir, null, envFile, lineSeparator);
}
@NotNull
protected static Map<String, String> runProcessAndReadEnvs(@NotNull List<String> command,
@Nullable File workingDir,
@Nullable Map<String, String> envs,
@NotNull File envFile,
String lineSeparator) throws Exception {
ProcessBuilder builder = new ProcessBuilder(command).redirectErrorStream(true);
if (envs != null) {
// we might need default environment for the process to launch correctly
builder.environment().putAll(envs);
}
if (workingDir != null) builder.directory(workingDir);
builder.environment().put(DISABLE_OMZ_AUTO_UPDATE, "true");
Process process = builder.start();
@@ -67,6 +67,11 @@ class PyVirtualEnvReader(val virtualEnvSdkPath: String) : EnvironmentUtil.ShellE
}
}
override fun dumpProcessEnvToFile(command: MutableList<String>, envFile: File, lineSeparator: String?): MutableMap<String, String> {
// pass shell environment for correct virtualenv environment setup (virtualenv expects to be executed from the terminal)
return runProcessAndReadEnvs(command, null, EnvironmentUtil.getEnvironmentMap(), envFile, lineSeparator)
}
private fun readVirtualEnvOnWindows(activate: Pair<String, String?>): MutableMap<String, String> {
val activateFile = FileUtil.createTempFile("pycharm-virualenv-activate.", ".bat", false)
val envFile = FileUtil.createTempFile("pycharm-virualenv-envs.", ".tmp", false)