CPP-10844 Visual Studio is not found when path to VS or user home contains non ascii characters: EnvironmentUtil API cleanup

This commit is contained in:
Anton Makeev
2017-11-15 22:53:10 +01:00
parent b78d458da6
commit d681db4179
4 changed files with 22 additions and 84 deletions

View File

@@ -162,8 +162,11 @@ public class EnvironmentUtil {
public static class ShellEnvReader {
public Map<String, String> readShellEnv() throws Exception {
return readShellEnv(null);
}
protected Map<String, String> readShellEnv(@Nullable Map<String, String> additionalEnvironment) throws Exception {
File reader = PathManager.findBinFileWithException("printenv.py");
File envFile = FileUtil.createTempFile("intellij-shell-env.", ".tmp", false);
@@ -181,7 +184,7 @@ public class EnvironmentUtil {
LOG.info("loading shell env: " + StringUtil.join(command, " "));
return dumpProcessEnvToFile(command, envFile, "\0");
return runProcessAndReadOutputAndEnvs(command, null, additionalEnvironment, envFile).second;
}
finally {
FileUtil.delete(envFile);
@@ -208,10 +211,7 @@ public class EnvironmentUtil {
cl.add(envFile.getPath());
cl.addAll(Arrays.asList("||", "exit", "/B", "%ERRORLEVEL%"));
return runProcessAndReadOutputAndEnvs(cl,
batchFile.getParentFile(),
null,
envFile, "\0");
return runProcessAndReadOutputAndEnvs(cl, batchFile.getParentFile(), null, envFile);
}
finally {
FileUtil.delete(envFile);
@@ -225,45 +225,15 @@ public class EnvironmentUtil {
ReadEnv.class.getCanonicalName());
}
@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, null, envFile, lineSeparator);
}
@NotNull
protected static Map<String, String> runProcessAndReadEnvs(@NotNull List<String> command,
@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 {
return runProcessAndReadOutputAndEnvs(command, workingDir, envs, envFile, lineSeparator).second;
}
@NotNull
protected static Pair<String, Map<String, String>> runProcessAndReadOutputAndEnvs(@NotNull List<String> command,
@Nullable File workingDir,
@Nullable Map<String, String> envs,
@NotNull File envFile,
String lineSeparator) throws Exception {
@Nullable Map<String, String> scriptEnvironment,
@NotNull File envFile) throws Exception {
ProcessBuilder builder = new ProcessBuilder(command).redirectErrorStream(true);
if (envs != null) {
if (scriptEnvironment != null) {
// we might need default environment for the process to launch correctly
builder.environment().putAll(envs);
builder.environment().putAll(scriptEnvironment);
}
if (workingDir != null) builder.directory(workingDir);
builder.environment().put(DISABLE_OMZ_AUTO_UPDATE, "true");
@@ -277,7 +247,7 @@ public class EnvironmentUtil {
if (rv != 0 || lines.isEmpty()) {
throw new Exception("rv:" + rv + " text:" + lines.length() + " out:" + StringUtil.trimEnd(gobbler.getText(), '\n'));
}
return Pair.create(gobbler.getText(), parseEnv(lines, lineSeparator));
return Pair.create(gobbler.getText(), parseEnv(lines));
}
@NotNull
@@ -297,18 +267,18 @@ public class EnvironmentUtil {
}
@Nullable
protected String getShell() throws Exception {
protected String getShell() {
return System.getenv("SHELL");
}
}
@NotNull
private static Map<String, String> parseEnv(String text, String lineSeparator) throws Exception {
private static Map<String, String> parseEnv(String text) throws Exception {
Set<String> toIgnore = new HashSet<String>(Arrays.asList("_", "PWD", "SHLVL", DISABLE_OMZ_AUTO_UPDATE, INTELLIJ_ENVIRONMENT_READER));
Map<String, String> env = System.getenv();
Map<String, String> newEnv = new HashMap<String, String>();
String[] lines = text.split(lineSeparator);
String[] lines = text.split("\0");
for (String line : lines) {
int pos = line.indexOf('=');
if (pos <= 0) {
@@ -437,7 +407,7 @@ public class EnvironmentUtil {
@TestOnly
static Map<String, String> testParser(@NotNull String lines) {
try {
return parseEnv(lines, "\0");
return parseEnv(lines);
}
catch (Exception e) {
throw new RuntimeException(e);

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 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.sdk
import com.intellij.openapi.components.PersistentStateComponent
@@ -69,7 +55,7 @@ class PyVirtualEnvTerminalCustomizer : LocalTerminalCustomizer() {
reader.activate?.let {
// we add only envs that are setup by the activate script, because adding other variables from the different shell
// can break the actual shell
envs.putAll(reader.readShellEnv().mapKeys { k -> k.key.toUpperCase() }.filterKeys { k ->
envs.putAll(reader.readPythonEnv().mapKeys { k -> k.key.toUpperCase() }.filterKeys { k ->
k in PyVirtualEnvReader.virtualEnvVars
})
}

View File

@@ -37,10 +37,11 @@ class PyVirtualEnvReader(val virtualEnvSdkPath: String) : EnvironmentUtil.ShellE
}
}
override fun readShellEnv(): MutableMap<String, String> {
fun readPythonEnv(): MutableMap<String, String> {
try {
if (SystemInfo.isUnix) {
return super.readShellEnv()
// pass shell environment for correct virtualenv environment setup (virtualenv expects to be executed from the terminal)
return super.readShellEnv(EnvironmentUtil.getEnvironmentMap())
}
else {
if (activate != null) {
@@ -57,11 +58,6 @@ class PyVirtualEnvReader(val virtualEnvSdkPath: String) : EnvironmentUtil.ShellE
return mutableMapOf()
}
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)
}
override fun getShellProcessCommand(): MutableList<String> {
val shellPath = shell

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 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.sdk;
import com.google.common.collect.ImmutableMap;
@@ -949,7 +935,7 @@ public final class PythonSdkType extends SdkType {
PyVirtualEnvReader reader = new PyVirtualEnvReader(sdkHome);
if (reader.getActivate() != null) {
try {
env.putAll(reader.readShellEnv().entrySet().stream()
env.putAll(reader.readPythonEnv().entrySet().stream()
.filter((entry) -> PyVirtualEnvReader.Companion.getVirtualEnvVars().contains(entry.getKey())
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}