Browse Source

[BUG][Plugin] Fix #6167 (#6177)

Kirs 3 years ago
parent
commit
58b694a85c

+ 2 - 1
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/AbstractCommandExecutor.java

@@ -19,7 +19,6 @@ package org.apache.dolphinscheduler.plugin.task.api;
 
 
 import static org.apache.dolphinscheduler.spi.task.TaskConstants.EXIT_CODE_FAILURE;
 import static org.apache.dolphinscheduler.spi.task.TaskConstants.EXIT_CODE_FAILURE;
 import static org.apache.dolphinscheduler.spi.task.TaskConstants.EXIT_CODE_KILL;
 import static org.apache.dolphinscheduler.spi.task.TaskConstants.EXIT_CODE_KILL;
-import static org.apache.dolphinscheduler.spi.task.TaskConstants.SH;
 
 
 import org.apache.dolphinscheduler.plugin.task.util.LoggerUtils;
 import org.apache.dolphinscheduler.plugin.task.util.LoggerUtils;
 import org.apache.dolphinscheduler.plugin.task.util.OSUtils;
 import org.apache.dolphinscheduler.plugin.task.util.OSUtils;
@@ -485,4 +484,6 @@ public abstract class AbstractCommandExecutor {
                 .build();
                 .build();
         return Executors.newSingleThreadExecutor(threadFactory);
         return Executors.newSingleThreadExecutor(threadFactory);
     }
     }
+
+    protected abstract String commandInterpreter();
 }
 }

+ 5 - 0
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/ShellCommandExecutor.java

@@ -113,4 +113,9 @@ public class ShellCommandExecutor extends AbstractCommandExecutor {
         }
         }
     }
     }
 
 
+    @Override
+    protected String commandInterpreter() {
+        return OSUtils.isWindows() ? CMD : SH;
+    }
+
 }
 }

+ 33 - 0
dolphinscheduler-task-plugin/dolphinscheduler-task-python/src/main/java/org/apache/dolphinscheduler/plugin/task/python/PythonCommandExecutor.java

@@ -33,6 +33,7 @@ import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.nio.file.Paths;
 import java.util.List;
 import java.util.List;
 import java.util.function.Consumer;
 import java.util.function.Consumer;
+import java.util.regex.Pattern;
 
 
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
@@ -52,6 +53,8 @@ public class PythonCommandExecutor extends AbstractCommandExecutor {
      */
      */
     public static final String PYTHON = "python";
     public static final String PYTHON = "python";
 
 
+    private static final Pattern PYTHON_PATH_PATTERN = Pattern.compile("/bin/python[\\d.]*$");
+
     /**
     /**
      * constructor
      * constructor
      *
      *
@@ -143,4 +146,34 @@ public class PythonCommandExecutor extends AbstractCommandExecutor {
         return null;
         return null;
     }
     }
 
 
+    /**
+     * Gets the command path to which Python can execute
+     * @return python command path
+     */
+    @Override
+    protected String commandInterpreter() {
+        String pythonHome = getPythonHome(taskRequest.getEnvFile());
+        return getPythonCommand(pythonHome);
+    }
+
+    /**
+     * get python command
+     *
+     * @param pythonHome python home
+     * @return python command
+     */
+    public static String getPythonCommand(String pythonHome) {
+        if (StringUtils.isEmpty(pythonHome)) {
+            return PYTHON;
+        }
+        File file = new File(pythonHome);
+        if (file.exists() && file.isFile()) {
+            return pythonHome;
+        }
+        if (PYTHON_PATH_PATTERN.matcher(pythonHome).find()) {
+            return pythonHome;
+        }
+        return Paths.get(pythonHome, "/bin/python").toString();
+    }
+
 }
 }