[platform] native environment reader for macOS (IDEA-216133)

GitOrigin-RevId: 8a43082ba32b4955cbb5bf48247026329f65b707
This commit is contained in:
Roman Shevchenko
2021-06-17 08:39:12 +02:00
committed by intellij-monorepo-bot
parent c1e81a95ef
commit a355c402ff
6 changed files with 17 additions and 59 deletions

BIN
bin/mac/printenv Executable file

Binary file not shown.

View File

@@ -1,33 +0,0 @@
#!/usr/bin/env python
# Dumps environment variables into specified file.
# Format: zero-separated "name=value" pairs in platform encoding.
# The script can work with any version of Python from 2.3 to at least 3.9
import os
import sys
if len(sys.argv) != 2:
raise Exception('Exactly one argument expected')
PY2 = sys.version_info < (3,)
if PY2:
environ = os.environ
else:
environ = os.environb
def b(s):
if PY2:
return s
else:
return s.encode('utf-8')
fd = open(sys.argv[1], 'wb')
try:
for key, value in environ.items():
fd.writelines([key, b('='), value, b('\0')])
finally:
fd.close()

View File

@@ -226,12 +226,8 @@ public final class StartupUtil {
loadSystemLibraries(log);
});
// don't load EnvironmentUtil class in main thread
shellEnvLoadFuture = forkJoinPool.submit(() -> {
Activity subActivity = StartUpMeasurer.startActivity("environment loading");
Path envReaderFile = PathManager.findBinFile(EnvironmentUtil.READER_FILE_NAME);
return envReaderFile == null ? null : EnvironmentUtil.loadEnvironment(envReaderFile, subActivity);
});
// don't load EnvironmentUtil class in the main thread
shellEnvLoadFuture = forkJoinPool.submit(() -> EnvironmentUtil.loadEnvironment(StartUpMeasurer.startActivity("environment loading")));
Thread.currentThread().setUncaughtExceptionHandler((__, e) -> {
StartupAbortedException.processException(e);

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 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.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.util;
import com.intellij.execution.CommandLineUtil;
@@ -29,10 +29,6 @@ public class EnvReader extends EnvironmentUtil.ShellEnvReader {
super(timeoutMillis);
}
public @NotNull Map<String, String> readShellEnv(@Nullable Path file, @Nullable Map<String, String> additionalEnvironment) throws IOException {
return doReadShellEnv(file, PathManager.findBinFileWithException(EnvironmentUtil.READER_FILE_NAME), additionalEnvironment);
}
public @NotNull Map<String, String> readBatEnv(@Nullable Path batchFile, List<String> args) throws IOException {
return readBatOutputAndEnv(batchFile, args).second;
}

View File

@@ -1,9 +1,10 @@
// Copyright 2000-2021 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.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.util;
import com.intellij.diagnostic.Activity;
import com.intellij.execution.process.UnixProcessManager;
import com.intellij.execution.process.WinProcessManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.SystemInfoRt;
import com.intellij.util.containers.CollectionFactory;
@@ -30,8 +31,6 @@ import java.util.function.Consumer;
import static java.util.Collections.emptyMap;
public final class EnvironmentUtil {
public static final String READER_FILE_NAME = "printenv.py";
private static final Logger LOG = Logger.getInstance(EnvironmentUtil.class);
/**
@@ -98,7 +97,7 @@ public final class EnvironmentUtil {
}
@ApiStatus.Internal
public static @Nullable Boolean loadEnvironment(@NotNull Path reader, @NotNull Activity activity) {
public static @Nullable Boolean loadEnvironment(@NotNull Activity activity) {
if (!shouldLoadShellEnv()) {
ourEnvGetter.set(CompletableFuture.completedFuture(getSystemEnv()));
return null;
@@ -108,7 +107,7 @@ public final class EnvironmentUtil {
ourEnvGetter.set(envFuture);
Boolean result = Boolean.TRUE;
try {
Map<String, String> env = getShellEnv(reader);
Map<String, String> env = getShellEnv();
setCharsetVar(env);
envFuture.complete(Collections.unmodifiableMap(env));
}
@@ -208,8 +207,8 @@ public final class EnvironmentUtil {
public static final String DISABLE_OMZ_AUTO_UPDATE = "DISABLE_AUTO_UPDATE";
private static final String INTELLIJ_ENVIRONMENT_READER = "INTELLIJ_ENVIRONMENT_READER";
private static @NotNull Map<String, String> getShellEnv(@NotNull Path reader) throws IOException {
return new ShellEnvReader().doReadShellEnv(null, reader, null);
private static @NotNull Map<String, String> getShellEnv() throws IOException {
return new ShellEnvReader().readShellEnv(null, null);
}
public static class ShellEnvReader {
@@ -232,9 +231,10 @@ public final class EnvironmentUtil {
myTimeoutMillis = timeoutMillis;
}
public final @NotNull Map<String, String> doReadShellEnv(@Nullable Path file,
@NotNull Path reader,
@Nullable Map<String, String> additionalEnvironment) throws IOException {
public final @NotNull Map<String, String> readShellEnv(@Nullable Path file, @Nullable Map<String, String> additionalEnvironment) throws IOException {
String loader = SystemInfoRt.isMac ? "printenv" : "printenv.py";
Path reader = PathManager.findBinFileWithException(loader);
Path envFile = Files.createTempFile("intellij-shell-env.", ".tmp");
StringBuilder readerCmd = new StringBuilder();
if (file != null) {
@@ -529,8 +529,8 @@ public final class EnvironmentUtil {
}
@TestOnly
static Map<String, String> testLoader(@NotNull Path reader) throws IOException {
return getShellEnv(reader);
static Map<String, String> testLoader() throws IOException {
return getShellEnv();
}
@TestOnly

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2020 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.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.util;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import org.junit.Test;
@@ -49,7 +48,7 @@ public class EnvironmentUtilTest {
public void load() throws IOException {
assumeUnix();
Map<String, String> env = EnvironmentUtil.testLoader(PathManager.findBinFileWithException(EnvironmentUtil.READER_FILE_NAME));
Map<String, String> env = EnvironmentUtil.testLoader();
assertTrue(env.size() >= System.getenv().size() / 2);
}