Class PythonUtils

java.lang.Object
de.oktoflow.platform.tools.lib.PythonUtils

public class PythonUtils extends Object
Some generic Python process helper functions. For now taken over to keep this plugin on Java 8. Considers from environment IIP_PYTHON as Python binary to use as well as IIP_PYTHONARGS as additional Python interpreter arguments. Tries to find Python (python3, python) in system path.
Author:
Holger Eichelberger, SSE
  • Field Details

    • DEFAULT_PYTHON_EXECUTABLE

      public static final File DEFAULT_PYTHON_EXECUTABLE
    • pythonExecutable

      private static File pythonExecutable
  • Constructor Details

    • PythonUtils

      public PythonUtils()
  • Method Details

    • isOsWindows

      public static boolean isOsWindows()
      Returns whether we are running on Windows.
      Returns:
      true for Windows, false else
    • getOsPath

      public static String getOsPath()
      Returns the operating system "PATH" variable.
      Returns:
      the path variable, empty if not defined/found
    • getSystemProperty

      public static String getSystemProperty(String property, String dflt)
      Gets a System property, defaulting to dflt if the property is not defined/cannot be read.
      Parameters:
      property - the system property name
      Returns:
      the system property value or deflt if it is not defined/cannot be read
    • setPythonExecutable

      public static void setPythonExecutable(File exec)
      Defines the (global) Python executable.
      Parameters:
      exec - the executable, may be null for dynamically determined
    • getPythonExecutable

      public static File getPythonExecutable()
      Returns the Python executable. We consider the following precedences:
      1. Local python3 in Linux Jenkins installation (for testing, although this is production code; with installation path)
      2. Local python3 in default Linux installation ("/usr/bin", with installation path)
      3. The global python executable setPythonExecutable(File) (with installation path)
      4. The command "python"
      Returns:
      the executable, returns at least "python"; the result may be an absolute path but must not be. Calling File.getAbsolutePath() on the result may be misleading - if required, use it as string.
    • getPythonExecutable

      public static File getPythonExecutable(String pythonBinary)
      Returns the Python executable. We consider the following precedences:
      1. Local python3 in Linux Jenkins installation (for testing, although this is production code; with installation path)
      2. Local python3 in default Linux installation ("/usr/bin", with installation path)
      3. The global python executable setPythonExecutable(File) (with installation path)
      4. The command "python"
      Parameters:
      pythonBinary - explicitly given path to python binary
      Returns:
      the executable, returns at least "python"; the result may be an absolute path but must not be. Calling File.getAbsolutePath() on the result may be misleading - if required, use it as string.
    • getPythonArgs

      public static String getPythonArgs(String pythonArgs)
      Returns the python interpreter arguments either from the string passed in or from IIP_PYTHONARGS.
      Parameters:
      pythonArgs - the python interpreter arguments, may be null or empty
      Returns:
      the python interpreter arguments, may be null or empty
    • insertArgs

      public static final String[] insertArgs(String[] cmd, int pos, String args)
      Inserts arguments into a (command line) array.
      Parameters:
      cmd - the original (command line) array
      pos - the position where to insert, call is ignored if the position is outside the array
      args - the arguments to insert, call is ignored if null or empty, args are split by spaces
      Returns:
      the (extended) arguments