PythonCondaInterpreter extends Interpreter { @Override public InterpreterResult interpret(String st, InterpreterContext context) throws InterpreterException { InterpreterOutput out = context.out; Matcher activateMatcher = PATTERN_COMMAND_ACTIVATE.matcher(st); Matcher createMatcher = PATTERN_COMMAND_CREATE.matcher(st); Matcher installMatcher = PATTERN_COMMAND_INSTALL.matcher(st); Matcher uninstallMatcher = PATTERN_COMMAND_UNINSTALL.matcher(st); Matcher envMatcher = PATTERN_COMMAND_ENV.matcher(st); try { if (PATTERN_COMMAND_ENV_LIST.matcher(st).matches()) { String result = runCondaEnvList(); return new InterpreterResult(Code.SUCCESS, Type.HTML, result); } else if (envMatcher.matches()) { String result = runCondaEnv(getRestArgsFromMatcher(envMatcher)); return new InterpreterResult(Code.SUCCESS, Type.HTML, result); } else if (PATTERN_COMMAND_LIST.matcher(st).matches()) { String result = runCondaList(); return new InterpreterResult(Code.SUCCESS, Type.HTML, result); } else if (createMatcher.matches()) { String result = runCondaCreate(getRestArgsFromMatcher(createMatcher)); return new InterpreterResult(Code.SUCCESS, Type.HTML, result); } else if (activateMatcher.matches()) { String envName = activateMatcher.group(1).trim(); return runCondaActivate(envName); } else if (PATTERN_COMMAND_DEACTIVATE.matcher(st).matches()) { return runCondaDeactivate(); } else if (installMatcher.matches()) { String result = runCondaInstall(getRestArgsFromMatcher(installMatcher)); return new InterpreterResult(Code.SUCCESS, Type.HTML, result); } else if (uninstallMatcher.matches()) { String result = runCondaUninstall(getRestArgsFromMatcher(uninstallMatcher)); return new InterpreterResult(Code.SUCCESS, Type.HTML, result); } else if (st == null || PATTERN_COMMAND_HELP.matcher(st).matches()) { runCondaHelp(out); return new InterpreterResult(Code.SUCCESS); } else if (PATTERN_COMMAND_INFO.matcher(st).matches()) { String result = runCondaInfo(); return new InterpreterResult(Code.SUCCESS, Type.HTML, result); } else { return new InterpreterResult(Code.ERROR, "Not supported command: " + st); } } catch (RuntimeException | IOException | InterruptedException e) { throw new InterpreterException(e); } } PythonCondaInterpreter(Properties property); @Override void open(); @Override void close(); @Override InterpreterResult interpret(String st, InterpreterContext context); String getCurrentCondaEnvName(); void setCurrentCondaEnvName(String currentCondaEnvName); static String runCondaCommandForTextOutput(String title, List<String> commands); static String wrapCondaBasicOutputStyle(String title, String content); static String wrapCondaTableOutputStyle(String title, Map<String, String> kv); static Map<String, String> parseCondaCommonStdout(String out); @Override void cancel(InterpreterContext context); @Override FormType getFormType(); @Override int getProgress(InterpreterContext context); @Override Scheduler getScheduler(); static String runCommand(List<String> commands); static String runCommand(String... command); static List<String> getRestArgsFromMatcher(Matcher m); static final String ZEPPELIN_PYTHON; static final String CONDA_PYTHON_PATH; static final String DEFAULT_ZEPPELIN_PYTHON; static final Pattern PATTERN_OUTPUT_ENV_LIST; static final Pattern PATTERN_COMMAND_ENV_LIST; static final Pattern PATTERN_COMMAND_ENV; static final Pattern PATTERN_COMMAND_LIST; static final Pattern PATTERN_COMMAND_CREATE; static final Pattern PATTERN_COMMAND_ACTIVATE; static final Pattern PATTERN_COMMAND_DEACTIVATE; static final Pattern PATTERN_COMMAND_INSTALL; static final Pattern PATTERN_COMMAND_UNINSTALL; static final Pattern PATTERN_COMMAND_HELP; static final Pattern PATTERN_COMMAND_INFO; }
@Test public void testListEnv() throws IOException, InterruptedException, InterpreterException { setMockCondaEnvList(); InterpreterContext context = getInterpreterContext(); InterpreterResult result = conda.interpret("env list", context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); assertTrue(result.toString().contains(">env1<")); assertTrue(result.toString().contains("/path1<")); assertTrue(result.toString().contains(">env2<")); assertTrue(result.toString().contains("/path2<")); }