Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed code for build failure in python virtual environment #341

Merged
merged 8 commits into from
May 22, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.pytorch.serve.util.ConfigManager;
import org.pytorch.serve.wlm.ModelManager;
Expand Down Expand Up @@ -40,23 +42,37 @@ public void run() {
String pythonEnv;
if ((pythonPath == null || pythonPath.isEmpty())
&& (!workingDir.getAbsolutePath().contains("site-package"))) {
pythonEnv = "PYTHONPATH=" + workingDir.getAbsolutePath();
pythonEnv = workingDir.getAbsolutePath();
} else {
pythonEnv = "PYTHONPATH=" + pythonPath;
pythonEnv = pythonPath;
if (!workingDir.getAbsolutePath().contains("site-package")) {
pythonEnv += File.pathSeparatorChar + workingDir.getAbsolutePath(); // NOPMD
}
}
// sbin added for macs for python sysctl pythonpath
HashMap<String, String> environment = new HashMap<>(System.getenv());
environment.put("PYTHONPATH", pythonEnv);

StringBuilder path = new StringBuilder();
path.append("PATH=").append(System.getenv("PATH"));
path.append(System.getenv("PATH"));
String osName = System.getProperty("os.name");
if (osName.startsWith("Mac OS X")) {
path.append(File.pathSeparatorChar).append("/sbin/");
}
String[] env = {pythonEnv, path.toString()};
final Process p = Runtime.getRuntime().exec(args, env, workingDir);

environment.put("PATH", path.toString());
ArrayList<String> envList = new ArrayList<>();
Pattern blackList = configManager.getBlacklistPattern();

for (Map.Entry<String, String> entry : environment.entrySet()) {
maaquib marked this conversation as resolved.
Show resolved Hide resolved
if (!blackList.matcher(entry.getKey()).matches()) {
envList.add(entry.getKey() + '=' + entry.getValue());
}
}

final Process p =
Runtime.getRuntime()
.exec(args, envList.toArray(new String[0]), workingDir); // NOPMD
ModelManager modelManager = ModelManager.getInstance();
Map<Integer, WorkerThread> workerMap = modelManager.getWorkers();
try (OutputStream os = p.getOutputStream()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.lang.reflect.Field;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.pytorch.serve.TestUtils;
Expand Down Expand Up @@ -38,39 +37,15 @@ private Metric createMetric(String metricName, String requestId) {
}

@SuppressWarnings("unchecked")
private void modifyEnv(String key, String val)
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
try {
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.put(key, val);
Field theCIEField =
processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
theCIEField.setAccessible(true);
Map<String, String> cienv = (Map<String, String>) theCIEField.get(null);
cienv.put(key, val);
} catch (NoSuchFieldException e) {
Class[] classes = Collections.class.getDeclaredClasses();
Map<String, String> env = System.getenv();
for (Class cl : classes) {
if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
Object obj = field.get(env);
Map<String, String> map = (Map<String, String>) obj;
map.clear();
map.put(key, val);
}
}
}
private void modifyEnv(String key, String val) throws ReflectiveOperationException {
Map<String, String> env = System.getenv();
Field field = env.getClass().getDeclaredField("m");
field.setAccessible(true);
((Map<String, String>) field.get(env)).put(key, val);
}

@Test
public void test()
throws IOException, GeneralSecurityException, IllegalAccessException,
NoSuchFieldException, ClassNotFoundException {
public void test() throws IOException, GeneralSecurityException, ReflectiveOperationException {
modifyEnv("TS_DEFAULT_RESPONSE_TIMEOUT", "130");
ConfigManager.Arguments args = new ConfigManager.Arguments();
args.setModels(new String[] {"noop_v0.1"});
Expand Down Expand Up @@ -104,9 +79,7 @@ public void test()
}

@Test
public void testNoEnvVars()
throws IllegalAccessException, NoSuchFieldException, ClassNotFoundException,
IOException {
public void testNoEnvVars() throws ReflectiveOperationException, IOException {
System.setProperty("tsConfigFile", "src/test/resources/config_test_env.properties");
modifyEnv("TS_DEFAULT_RESPONSE_TIMEOUT", "130");
ConfigManager.Arguments args = new ConfigManager.Arguments();
Expand Down