Apply new mechanism to PythonInterpreter

This commit is contained in:
Mina Lee 2016-06-21 21:51:20 -07:00
parent 41a7302d81
commit 5013890ed5
4 changed files with 32 additions and 31 deletions

View file

@ -20,7 +20,6 @@ package org.apache.zeppelin.python;
import org.apache.zeppelin.display.GUI;
import org.apache.zeppelin.interpreter.Interpreter;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterPropertyBuilder;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.interpreter.InterpreterResult.Code;
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
@ -49,28 +48,16 @@ public class PythonInterpreter extends Interpreter {
public static final String BOOTSTRAP_PY = "/bootstrap.py";
public static final String BOOTSTRAP_INPUT_PY = "/bootstrap_input.py";
public static final String ZEPPELIN_PYTHON = "zeppelin.python";
public static final String DEFAULT_ZEPPELIN_PYTHON = "python";
private Integer port;
private GatewayServer gatewayServer;
private long pythonPid;
private Boolean py4J = false;
private InterpreterContext context;
PythonProcess process = null;
static {
Interpreter.register(
"python",
"python",
PythonInterpreter.class.getName(),
new InterpreterPropertyBuilder()
.add(ZEPPELIN_PYTHON, DEFAULT_ZEPPELIN_PYTHON,
"Python directory. Default : python (assume python is in your $PATH)")
.build()
);
}
public PythonInterpreter(Properties property) {
super(property);
}
@ -223,7 +210,7 @@ public class PythonInterpreter extends Interpreter {
return context.getGui();
}
public Integer getPy4JPort() {
public Integer getPy4jPort() {
return port;
}

View file

@ -92,7 +92,7 @@ public class PythonProcess {
String output = "";
String line;
while (!(line = reader.readLine()).contains("*!?flush reader!?*")) {
logger.debug("Readed line from python shell : " + line);
logger.debug("Read line from python shell : " + line);
if (line.equals("...")) {
logger.warn("Syntax error ! ");
output += "Syntax error ! ";

View file

@ -0,0 +1,15 @@
[
{
"group": "python",
"name": "python",
"className": "org.apache.zeppelin.python.PythonInterpreter",
"properties": {
"zeppelin.python": {
"envName": null,
"propertyName": "zeppelin.python",
"defaultValue": "python",
"description": "Python directory. It is set to python by default.(assume python is in your $PATH)"
}
}
}
]

View file

@ -60,6 +60,12 @@ public class PythonInterpreterTest {
PythonProcess mockPythonProcess;
String cmdHistory;
public static Properties getPythonTestProperties() {
Properties p = new Properties();
p.setProperty(ZEPPELIN_PYTHON, DEFAULT_ZEPPELIN_PYTHON);
return p;
}
@Before
public void beforeTest() {
cmdHistory = "";
@ -79,20 +85,15 @@ public class PythonInterpreterTest {
logger.error("Can't initiate python process", e);
}
Properties properties = new Properties();
properties.put(ZEPPELIN_PYTHON, DEFAULT_ZEPPELIN_PYTHON);
pythonInterpreter = spy(new PythonInterpreter(properties));
pythonInterpreter = spy(new PythonInterpreter(getPythonTestProperties()));
when(pythonInterpreter.getPythonProcess()).thenReturn(mockPythonProcess);
try {
when(mockPythonProcess.sendAndGetResult(eq("\n\nimport py4j\n"))).thenReturn("ImportError");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
@ -111,7 +112,7 @@ public class PythonInterpreterTest {
py4j JavaGateway is not running
*/
pythonInterpreter.open();
assertNull(pythonInterpreter.getPy4JPort());
assertNull(pythonInterpreter.getPy4jPort());
assertTrue(cmdHistory.contains("def help()"));
assertTrue(cmdHistory.contains("class PyZeppelinContext():"));
@ -122,8 +123,7 @@ public class PythonInterpreterTest {
}
@Test
public void testPy4JInstalled() {
public void testPy4jInstalled() {
/*
If Py4J installed, bootstrap_input.py
@ -137,7 +137,7 @@ public class PythonInterpreterTest {
e.printStackTrace();
}
pythonInterpreter.open();
Integer py4jPort = pythonInterpreter.getPy4JPort();
Integer py4jPort = pythonInterpreter.getPy4jPort();
assertNotNull(py4jPort);
assertTrue(cmdHistory.contains("def help()"));
@ -147,8 +147,7 @@ public class PythonInterpreterTest {
assertTrue(cmdHistory.contains("GatewayClient(port=" + py4jPort + ")"));
assertTrue(cmdHistory.contains("org.apache.zeppelin.display.Input"));
assertTrue(checkSocketAdress(py4jPort));
assertTrue(checkSocketAddress(py4jPort));
}
@ -162,12 +161,12 @@ public class PythonInterpreterTest {
e.printStackTrace();
}
pythonInterpreter.open();
Integer py4jPort = pythonInterpreter.getPy4JPort();
Integer py4jPort = pythonInterpreter.getPy4jPort();
assertNotNull(py4jPort);
pythonInterpreter.close();
assertFalse(checkSocketAdress(py4jPort));
assertFalse(checkSocketAddress(py4jPort));
try {
verify(mockPythonProcess, times(1)).close();
} catch (IOException e) {
@ -189,7 +188,7 @@ public class PythonInterpreterTest {
private boolean checkSocketAdress(Integer py4jPort) {
private boolean checkSocketAddress(Integer py4jPort) {
Socket s = new Socket();
SocketAddress sa = new InetSocketAddress("localhost", py4jPort);
Boolean working = null;