refactoring and fix testcase

This commit is contained in:
astroshim 2016-11-30 00:35:23 +09:00
parent 8c3fbd3a40
commit 294b6f967d
2 changed files with 40 additions and 41 deletions

View file

@ -33,7 +33,8 @@ import java.util.regex.Pattern;
public class PythonCondaInterpreter extends Interpreter {
Logger logger = LoggerFactory.getLogger(PythonCondaInterpreter.class);
public static final String ZEPPELIN_PYTHON = "zeppelin.python";
public static final String PYTHON_PATH = "/bin/python";
public static final String CONDA_PYTHON_PATH = "/bin/python";
public static final String DEFAULT_ZEPPELIN_PYTHON = "python";
Pattern condaEnvListPattern = Pattern.compile("([^\\s]*)[\\s*]*\\s(.*)");
Pattern listPattern = Pattern.compile("env\\s*list\\s?");
@ -69,10 +70,12 @@ public class PythonCondaInterpreter extends Interpreter {
return new InterpreterResult(InterpreterResult.Code.SUCCESS);
} else if (activateMatcher.matches()) {
String envName = activateMatcher.group(1);
restartPythonProcess(getEnvPath(envName));
changePythonEnvironment(envName);
restartPythonProcess();
return new InterpreterResult(InterpreterResult.Code.SUCCESS, "\"" + envName + "\" activated");
} else if (deactivateMatcher.matches()) {
restartPythonProcess(null);
changePythonEnvironment(null);
restartPythonProcess();
return new InterpreterResult(InterpreterResult.Code.SUCCESS, "Deactivated");
} else if (helpMatcher.matches()) {
printUsage(out);
@ -82,36 +85,28 @@ public class PythonCondaInterpreter extends Interpreter {
}
}
private String getEnvPath(String envName) {
HashMap<String, String> envList = getCondaEnvs();
String path = null;
for (String name : envList.keySet()) {
if (envName.equals(name)) {
path = envList.get(name) + PYTHON_PATH;
break;
}
}
return path;
}
private void setPythonPath (PythonInterpreter python, String pythonPath) {
if (pythonPath == null) {
String binPath = getProperty(ZEPPELIN_PYTHON);
String pythonCommand = python.getPythonCommand();
if (pythonCommand != null) {
binPath = pythonCommand;
}
pythonPath = binPath;
}
Properties props = getProperty();
props.setProperty(ZEPPELIN_PYTHON, pythonPath);
python.setProperty(props);
}
private void restartPythonProcess(String pythonPath) {
private void changePythonEnvironment(String envName) {
PythonInterpreter python = getPythonInterpreter();
String binPath = null;
if (envName == null) {
binPath = getProperty(ZEPPELIN_PYTHON);
if (binPath == null) {
binPath = DEFAULT_ZEPPELIN_PYTHON;
}
} else {
HashMap<String, String> envList = getCondaEnvs();
for (String name : envList.keySet()) {
if (envName.equals(name)) {
binPath = envList.get(name) + CONDA_PYTHON_PATH;
break;
}
}
}
python.setPythonCommand(binPath);
}
private void restartPythonProcess() {
PythonInterpreter python = getPythonInterpreter();
setPythonPath(python, pythonPath);
python.close();
python.open();
}

View file

@ -49,18 +49,21 @@ public class PythonCondaInterpreterTest implements InterpreterOutputListener {
doReturn(python).when(conda).getPythonInterpreter();
}
@Test
public void testListEnv() throws IOException, InterruptedException {
InterpreterContext context = getInterpreterContext();
private void setCondaEnvs() throws IOException, InterruptedException {
StringBuilder sb = new StringBuilder();
sb.append("#comment\n\nenv1 * /path1\nenv2\t/path2\n");
doReturn(sb).when(conda).createStringBuilder();
doReturn(0).when(conda)
.runCommand(any(StringBuilder.class), anyString(), anyString(), anyString());
.runCommand(any(StringBuilder.class), anyString(), anyString(), anyString());
}
@Test
public void testListEnv() throws IOException, InterruptedException {
setCondaEnvs();
// list available env
InterpreterContext context = getInterpreterContext();
InterpreterResult result = conda.interpret("", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
@ -72,12 +75,13 @@ public class PythonCondaInterpreterTest implements InterpreterOutputListener {
}
@Test
public void testActivateEnv() {
public void testActivateEnv() throws IOException, InterruptedException {
setCondaEnvs();
InterpreterContext context = getInterpreterContext();
conda.interpret("activate env", context);
conda.interpret("activate env1", context);
verify(python, times(1)).open();
verify(python, times(1)).close();
verify(python).setPythonCommand("conda run -n env \"python -iu\"");
verify(python).setPythonCommand("/path1/bin/python");
}
@Test
@ -86,7 +90,7 @@ public class PythonCondaInterpreterTest implements InterpreterOutputListener {
conda.interpret("deactivate", context);
verify(python, times(1)).open();
verify(python, times(1)).close();
verify(python).setPythonCommand(null);
verify(python).setPythonCommand("python");
}
private InterpreterContext getInterpreterContext() {