use of org.apache.zeppelin.interpreter.InterpreterResult.Code in project zeppelin by apache.
the class ShellInterpreter method interpret.
@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
LOGGER.debug("Run shell command '" + cmd + "'");
OutputStream outStream = new ByteArrayOutputStream();
CommandLine cmdLine = CommandLine.parse(shell);
// they need to be delimited by '&&' instead
if (isWindows) {
String[] lines = StringUtils.split(cmd, "\n");
cmd = StringUtils.join(lines, " && ");
}
cmdLine.addArgument(cmd, false);
try {
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(contextInterpreter.out, contextInterpreter.out));
executor.setWatchdog(new ExecuteWatchdog(Long.valueOf(getProperty(TIMEOUT_PROPERTY))));
executors.put(contextInterpreter.getParagraphId(), executor);
int exitVal = executor.execute(cmdLine);
LOGGER.info("Paragraph " + contextInterpreter.getParagraphId() + " return with exit value: " + exitVal);
return new InterpreterResult(Code.SUCCESS, outStream.toString());
} catch (ExecuteException e) {
int exitValue = e.getExitValue();
LOGGER.error("Can not run " + cmd, e);
Code code = Code.ERROR;
String message = outStream.toString();
if (exitValue == 143) {
code = Code.INCOMPLETE;
message += "Paragraph received a SIGTERM\n";
LOGGER.info("The paragraph " + contextInterpreter.getParagraphId() + " stopped executing: " + message);
}
message += "ExitValue: " + exitValue;
return new InterpreterResult(code, message);
} catch (IOException e) {
LOGGER.error("Can not run " + cmd, e);
return new InterpreterResult(Code.ERROR, e.getMessage());
} finally {
executors.remove(contextInterpreter.getParagraphId());
}
}
use of org.apache.zeppelin.interpreter.InterpreterResult.Code in project zeppelin by apache.
the class FlinkInterpreter method interpret.
public InterpreterResult interpret(String[] lines, InterpreterContext context) {
final IMain imain = flinkIloop.intp();
String[] linesToRun = new String[lines.length + 1];
for (int i = 0; i < lines.length; i++) {
linesToRun[i] = lines[i];
}
linesToRun[lines.length] = "print(\"\")";
System.setOut(new PrintStream(out));
out.reset();
Code r = null;
String incomplete = "";
boolean inComment = false;
for (int l = 0; l < linesToRun.length; l++) {
final String s = linesToRun[l];
// check if next line starts with "." (but not ".." or "./") it is treated as an invocation
if (l + 1 < linesToRun.length) {
String nextLine = linesToRun[l + 1].trim();
boolean continuation = false;
if (nextLine.isEmpty() || // skip empty line or comment
nextLine.startsWith("//") || nextLine.startsWith("}") || nextLine.startsWith("object")) {
// include "} object" for Scala companion object
continuation = true;
} else if (!inComment && nextLine.startsWith("/*")) {
inComment = true;
continuation = true;
} else if (inComment && nextLine.lastIndexOf("*/") >= 0) {
inComment = false;
continuation = true;
} else if (nextLine.length() > 1 && nextLine.charAt(0) == '.' && // ".."
nextLine.charAt(1) != '.' && nextLine.charAt(1) != '/') {
// "./"
continuation = true;
} else if (inComment) {
continuation = true;
}
if (continuation) {
incomplete += s + "\n";
continue;
}
}
final String currentCommand = incomplete;
scala.tools.nsc.interpreter.Results.Result res = null;
try {
res = Console.withOut(System.out, new AbstractFunction0<Results.Result>() {
@Override
public Results.Result apply() {
return imain.interpret(currentCommand + s);
}
});
} catch (Exception e) {
logger.info("Interpreter exception", e);
return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e));
}
r = getResultCode(res);
if (r == Code.ERROR) {
return new InterpreterResult(r, out.toString());
} else if (r == Code.INCOMPLETE) {
incomplete += s + "\n";
} else {
incomplete = "";
}
}
if (r == Code.INCOMPLETE) {
return new InterpreterResult(r, "Incomplete expression");
} else {
return new InterpreterResult(r, out.toString());
}
}
use of org.apache.zeppelin.interpreter.InterpreterResult.Code in project zeppelin by apache.
the class ScaldingInterpreter method interpretInput.
public InterpreterResult interpretInput(String[] lines) {
// add print("") to make sure not finishing with comment
// see https://github.com/NFLabs/zeppelin/issues/151
String[] linesToRun = new String[lines.length + 1];
for (int i = 0; i < lines.length; i++) {
linesToRun[i] = lines[i];
}
linesToRun[lines.length] = "print(\"\")";
out.reset();
// Moving two lines below from open() to this function.
// If they are in open output is incomplete.
PrintStream printStream = new PrintStream(out, true);
Console.setOut(printStream);
Code r = null;
String incomplete = "";
boolean inComment = false;
for (int l = 0; l < linesToRun.length; l++) {
String s = linesToRun[l];
// check if next line starts with "." (but not ".." or "./") it is treated as an invocation
if (l + 1 < linesToRun.length) {
String nextLine = linesToRun[l + 1].trim();
boolean continuation = false;
if (nextLine.isEmpty() || // skip empty line or comment
nextLine.startsWith("//") || nextLine.startsWith("}") || nextLine.startsWith("object")) {
// include "} object" for Scala companion object
continuation = true;
} else if (!inComment && nextLine.startsWith("/*")) {
inComment = true;
continuation = true;
} else if (inComment && nextLine.lastIndexOf("*/") >= 0) {
inComment = false;
continuation = true;
} else if (nextLine.length() > 1 && nextLine.charAt(0) == '.' && // ".."
nextLine.charAt(1) != '.' && nextLine.charAt(1) != '/') {
// "./"
continuation = true;
} else if (inComment) {
continuation = true;
}
if (continuation) {
incomplete += s + "\n";
continue;
}
}
scala.tools.nsc.interpreter.Results.Result res = null;
try {
res = interpreter.intp().interpret(incomplete + s);
} catch (Exception e) {
LOGGER.error("Interpreter exception: ", e);
return new InterpreterResult(Code.ERROR, e.getMessage());
}
r = getResultCode(res);
if (r == Code.ERROR) {
Console.flush();
return new InterpreterResult(r, out.toString());
} else if (r == Code.INCOMPLETE) {
incomplete += s + "\n";
} else {
incomplete = "";
}
}
if (r == Code.INCOMPLETE) {
return new InterpreterResult(r, "Incomplete expression");
} else {
Console.flush();
return new InterpreterResult(r, out.toString());
}
}
use of org.apache.zeppelin.interpreter.InterpreterResult.Code in project zeppelin by apache.
the class NotebookServiceTest method testParagraphOperations.
@Test
public void testParagraphOperations() throws IOException {
// create note
String note1Id = notebookService.createNote("note1", "python", false, context, callback);
notebook.processNote(note1Id, note1 -> {
assertEquals("note1", note1.getName());
assertEquals(0, note1.getParagraphCount());
verify(callback).onSuccess(note1, context);
return null;
});
// add paragraph
reset(callback);
Paragraph p = notebookService.insertParagraph(note1Id, 0, new HashMap<>(), context, callback);
assertNotNull(p);
verify(callback).onSuccess(p, context);
notebook.processNote(note1Id, note1 -> {
assertEquals(1, note1.getParagraphCount());
return null;
});
// update paragraph
reset(callback);
notebookService.updateParagraph(note1Id, p.getId(), "my_title", "my_text", new HashMap<>(), new HashMap<>(), context, callback);
assertEquals("my_title", p.getTitle());
assertEquals("my_text", p.getText());
// move paragraph
reset(callback);
notebookService.moveParagraph(note1Id, p.getId(), 0, context, callback);
notebook.processNote(note1Id, note1 -> {
assertEquals(p, note1.getParagraph(0));
verify(callback).onSuccess(p, context);
return null;
});
// run paragraph asynchronously
reset(callback);
p.getConfig().put("colWidth", "6.0");
p.getConfig().put("title", true);
boolean runStatus = notebook.processNote(note1Id, note1 -> {
return notebookService.runParagraph(note1, p.getId(), "my_title", "1+1", new HashMap<>(), new HashMap<>(), null, false, false, context, callback);
});
assertTrue(runStatus);
verify(callback).onSuccess(p, context);
assertEquals(2, p.getConfig().size());
// run paragraph synchronously via correct code
reset(callback);
runStatus = notebook.processNote(note1Id, note1 -> {
return notebookService.runParagraph(note1, p.getId(), "my_title", "1+1", new HashMap<>(), new HashMap<>(), null, false, true, context, callback);
});
assertTrue(runStatus);
verify(callback).onSuccess(p, context);
assertEquals(2, p.getConfig().size());
// run all paragraphs, with null paragraph list provided
reset(callback);
assertTrue(notebookService.runAllParagraphs(note1Id, null, context, callback));
reset(callback);
runStatus = notebook.processNote(note1Id, note1 -> {
return notebookService.runParagraph(note1, p.getId(), "my_title", "invalid_code", new HashMap<>(), new HashMap<>(), null, false, true, context, callback);
});
assertTrue(runStatus);
// TODO(zjffdu) Enable it after ZEPPELIN-3699
// assertNotNull(p.getResult());
verify(callback).onSuccess(p, context);
// clean output
reset(callback);
notebookService.clearParagraphOutput(note1Id, p.getId(), context, callback);
assertNull(p.getReturn());
verify(callback).onSuccess(p, context);
}
use of org.apache.zeppelin.interpreter.InterpreterResult.Code in project zeppelin by apache.
the class SparkInterpreter method interpretInput.
public InterpreterResult interpretInput(String[] lines, InterpreterContext context) {
SparkEnv.set(env);
String[] linesToRun = new String[lines.length];
for (int i = 0; i < lines.length; i++) {
linesToRun[i] = lines[i];
}
Console.setOut(context.out);
out.setInterpreterOutput(context.out);
context.out.clear();
Code r = null;
String incomplete = "";
boolean inComment = false;
for (int l = 0; l < linesToRun.length; l++) {
String s = linesToRun[l];
// check if next line starts with "." (but not ".." or "./") it is treated as an invocation
if (l + 1 < linesToRun.length) {
String nextLine = linesToRun[l + 1].trim();
boolean continuation = false;
if (nextLine.isEmpty() || // skip empty line or comment
nextLine.startsWith("//") || nextLine.startsWith("}") || nextLine.startsWith("object")) {
// include "} object" for Scala companion object
continuation = true;
} else if (!inComment && nextLine.startsWith("/*")) {
inComment = true;
continuation = true;
} else if (inComment && nextLine.lastIndexOf("*/") >= 0) {
inComment = false;
continuation = true;
} else if (nextLine.length() > 1 && nextLine.charAt(0) == '.' && // ".."
nextLine.charAt(1) != '.' && nextLine.charAt(1) != '/') {
// "./"
continuation = true;
} else if (inComment) {
continuation = true;
}
if (continuation) {
incomplete += s + "\n";
continue;
}
}
scala.tools.nsc.interpreter.Results.Result res = null;
try {
res = interpret(incomplete + s);
} catch (Exception e) {
sc.clearJobGroup();
out.setInterpreterOutput(null);
logger.info("Interpreter exception", e);
return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e));
}
r = getResultCode(res);
if (r == Code.ERROR) {
sc.clearJobGroup();
out.setInterpreterOutput(null);
return new InterpreterResult(r, "");
} else if (r == Code.INCOMPLETE) {
incomplete += s + "\n";
} else {
incomplete = "";
}
}
// make sure code does not finish with comment
if (r == Code.INCOMPLETE) {
scala.tools.nsc.interpreter.Results.Result res = null;
res = interpret(incomplete + "\nprint(\"\")");
r = getResultCode(res);
}
if (r == Code.INCOMPLETE) {
sc.clearJobGroup();
out.setInterpreterOutput(null);
return new InterpreterResult(r, "Incomplete expression");
} else {
sc.clearJobGroup();
putLatestVarInResourcePool(context);
out.setInterpreterOutput(null);
return new InterpreterResult(Code.SUCCESS);
}
}
Aggregations