You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zeppelin.apache.org by mo...@apache.org on 2016/12/01 02:02:37 UTC

zeppelin git commit: [ZEPPELIN-1724] conda run command removed in 4.1.0

Repository: zeppelin
Updated Branches:
  refs/heads/master 850fd81a5 -> 01c45d7ae


[ZEPPELIN-1724] conda run command removed in 4.1.0

### What is this PR for?
Because `conda run` command removed since version `4.0.9`, PythonCondaInterpreter not working after the `conda-4.0.9`.
This PR fixes this issue.

I tested conda-4.2.12 and conda-4.0.9 .

### What type of PR is it?
Bug Fix | Improvement

### What is the Jira issue?
https://issues.apache.org/jira/browse/ZEPPELIN-1724

### How should this be tested?
Please refer to https://github.com/apache/zeppelin/pull/1645

### Questions:
* Does the licenses files need update? no
* Is there breaking changes for older versions? no
* Does this needs documentation? no

Author: astroshim <hs...@nflabs.com>

Closes #1699 from astroshim/ZEPPELIN-1724 and squashes the following commits:

294b6f9 [astroshim] refactoring and fix testcase
8c3fbd3 [astroshim] fix conda version


Project: http://git-wip-us.apache.org/repos/asf/zeppelin/repo
Commit: http://git-wip-us.apache.org/repos/asf/zeppelin/commit/01c45d7a
Tree: http://git-wip-us.apache.org/repos/asf/zeppelin/tree/01c45d7a
Diff: http://git-wip-us.apache.org/repos/asf/zeppelin/diff/01c45d7a

Branch: refs/heads/master
Commit: 01c45d7ae601963068ebfc225a5947fc69eac67a
Parents: 850fd81
Author: astroshim <hs...@nflabs.com>
Authored: Wed Nov 30 00:35:23 2016 +0900
Committer: Lee moon soo <mo...@apache.org>
Committed: Wed Nov 30 18:02:33 2016 -0800

----------------------------------------------------------------------
 .../zeppelin/python/PythonCondaInterpreter.java | 75 ++++++++++++++------
 .../python/PythonCondaInterpreterTest.java      | 22 +++---
 2 files changed, 68 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zeppelin/blob/01c45d7a/python/src/main/java/org/apache/zeppelin/python/PythonCondaInterpreter.java
----------------------------------------------------------------------
diff --git a/python/src/main/java/org/apache/zeppelin/python/PythonCondaInterpreter.java b/python/src/main/java/org/apache/zeppelin/python/PythonCondaInterpreter.java
index 455687d..304e1f0 100644
--- a/python/src/main/java/org/apache/zeppelin/python/PythonCondaInterpreter.java
+++ b/python/src/main/java/org/apache/zeppelin/python/PythonCondaInterpreter.java
@@ -22,6 +22,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.*;
+import java.util.HashMap;
 import java.util.Properties;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -31,6 +32,9 @@ 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 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?");
@@ -62,15 +66,15 @@ public class PythonCondaInterpreter extends Interpreter {
     Matcher helpMatcher = helpPattern.matcher(st);
 
     if (st == null || st.isEmpty() || listMatcher.matches()) {
-      listEnv(out);
+      listEnv(out, getCondaEnvs());
       return new InterpreterResult(InterpreterResult.Code.SUCCESS);
     } else if (activateMatcher.matches()) {
       String envName = activateMatcher.group(1);
-      setPythonCommand("conda run -n " + envName + " \"python -iu\"");
+      changePythonEnvironment(envName);
       restartPythonProcess();
       return new InterpreterResult(InterpreterResult.Code.SUCCESS, "\"" + envName + "\" activated");
     } else if (deactivateMatcher.matches()) {
-      setPythonCommand(null);
+      changePythonEnvironment(null);
       restartPythonProcess();
       return new InterpreterResult(InterpreterResult.Code.SUCCESS, "Deactivated");
     } else if (helpMatcher.matches()) {
@@ -81,9 +85,24 @@ public class PythonCondaInterpreter extends Interpreter {
     }
   }
 
-  public void setPythonCommand(String cmd) {
+  private void changePythonEnvironment(String envName) {
     PythonInterpreter python = getPythonInterpreter();
-    python.setPythonCommand(cmd);
+    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() {
@@ -111,15 +130,14 @@ public class PythonCondaInterpreter extends Interpreter {
     return python;
   }
 
-  private void listEnv(InterpreterOutput out) {
+  private HashMap getCondaEnvs() {
+    HashMap envList = null;
+
     StringBuilder sb = createStringBuilder();
     try {
       int exit = runCommand(sb, "conda", "env", "list");
       if (exit == 0) {
-        out.setType(InterpreterResult.Type.HTML);
-        out.write("<h4>Conda environments</h4>\n");
-        // start table
-        out.write("<div style=\"display:table\">\n");
+        envList = new HashMap();
         String[] lines = sb.toString().split("\n");
         for (String s : lines) {
           if (s == null || s.isEmpty() || s.startsWith("#")) {
@@ -130,23 +148,40 @@ public class PythonCondaInterpreter extends Interpreter {
           if (!match.matches()) {
             continue;
           }
-          out.write(String.format("<div style=\"display:table-row\">" +
-              "<div style=\"display:table-cell;width:150px\">%s</div>" +
-              "<div style=\"display:table-cell;\">%s</div>" +
-              "</div>\n",
-              match.group(1), match.group(2)));
+          envList.put(match.group(1), match.group(2));
         }
-        // end table
-        out.write("</div><br />\n");
-        out.write("<small><code>%python.conda help</code> for the usage</small>\n");
-      } else {
-        out.write("Failed to run 'conda' " + exit + "\n");
       }
     } catch (IOException | InterruptedException e) {
       throw new InterpreterException(e);
     }
+    return envList;
+  }
+
+  private void listEnv(InterpreterOutput out, HashMap<String, String> envList) {
+    try {
+      out.setType(InterpreterResult.Type.HTML);
+      out.write("<h4>Conda environments</h4>\n");
+      // start table
+      out.write("<div style=\"display:table\">\n");
+
+      for (String name : envList.keySet()) {
+        String path = envList.get(name);
+
+        out.write(String.format("<div style=\"display:table-row\">" +
+            "<div style=\"display:table-cell;width:150px\">%s</div>" +
+            "<div style=\"display:table-cell;\">%s</div>" +
+            "</div>\n",
+          name, path));
+      }
+      // end table
+      out.write("</div><br />\n");
+      out.write("<small><code>%python.conda help</code> for the usage</small>\n");
+    } catch  (IOException e) {
+      throw new InterpreterException(e);
+    }
   }
 
+
   private void printUsage(InterpreterOutput out) {
     try {
       out.setType(InterpreterResult.Type.HTML);

http://git-wip-us.apache.org/repos/asf/zeppelin/blob/01c45d7a/python/src/test/java/org/apache/zeppelin/python/PythonCondaInterpreterTest.java
----------------------------------------------------------------------
diff --git a/python/src/test/java/org/apache/zeppelin/python/PythonCondaInterpreterTest.java b/python/src/test/java/org/apache/zeppelin/python/PythonCondaInterpreterTest.java
index 434f46f..b654d2e 100644
--- a/python/src/test/java/org/apache/zeppelin/python/PythonCondaInterpreterTest.java
+++ b/python/src/test/java/org/apache/zeppelin/python/PythonCondaInterpreterTest.java
@@ -49,18 +49,21 @@ public class PythonCondaInterpreterTest {
     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());
 
@@ -73,12 +76,13 @@ public class PythonCondaInterpreterTest {
   }
 
   @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
@@ -87,7 +91,7 @@ public class PythonCondaInterpreterTest {
     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() {