Search in sources :

Example 1 with UnableToFindExecutableException

use of org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException in project Pydev by fabioz.

the class CondaPackageManager method manage.

public void manage(String[] initialCommands, boolean autoRun, File workingDir) {
    final File condaExecutable;
    try {
        condaExecutable = PyDevCondaPreferences.findCondaExecutable(interpreterInfo);
    } catch (UnableToFindExecutableException e) {
        Log.log(e);
        PyDialogHelpers.openException("Unable to find conda", e);
        return;
    }
    ProcessWindow processWindow = new ProcessWindow(UIUtils.getActiveShell()) {

        @Override
        protected void configureShell(Shell shell) {
            super.configureShell(shell);
            shell.setText("Manage conda");
        }

        @Override
        protected String[] getAvailableCommands() {
            List<String> lst = new ArrayList<>(Arrays.asList(initialCommands));
            final String prefixDir = new File(interpreterInfo.getExecutableOrJar()).getParent();
            String prefixInfo = " -p " + prefixDir;
            for (int i = 0; i < lst.size(); i++) {
                String existing = lst.get(i);
                if (!existing.contains("-p")) {
                    existing = existing.trim();
                    if (existing.startsWith("install") || existing.startsWith("uninstall") || existing.startsWith("upgrade") || existing.startsWith("update") || existing.startsWith("clean") || existing.startsWith("list") || existing.startsWith("package") || existing.startsWith("remove") || existing.startsWith("search")) {
                        existing += prefixInfo;
                    }
                    lst.set(i, existing);
                }
            }
            return ArrayUtils.concatArrays(lst.toArray(new String[0]), new String[] { "install -p " + prefixDir + " <package>", "uninstall -p " + prefixDir + " <package>" });
        }

        @Override
        protected String getSeeURL() {
            return "https://conda.io/docs/commands.html";
        }

        @Override
        public Tuple<Process, String> createProcess(String[] arguments) {
            clearOutput();
            AbstractShell.restartAllShells();
            String[] cmdLine = ArrayUtils.concatArrays(new String[] { condaExecutable.toString() }, arguments);
            return new SimpleRunner().run(cmdLine, workingDir, null, null);
        }
    };
    if (workingDir == null) {
        workingDir = condaExecutable.getParentFile();
    }
    processWindow.setParameters(null, null, condaExecutable, workingDir);
    processWindow.setAutoRun(autoRun);
    processWindow.open();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) AbstractShell(org.python.pydev.ast.codecompletion.shell.AbstractShell) SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) UnableToFindExecutableException(org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException) ArrayList(java.util.ArrayList) File(java.io.File) ProcessWindow(org.python.pydev.process_window.ProcessWindow)

Example 2 with UnableToFindExecutableException

use of org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException in project Pydev by fabioz.

the class PipPackageManager method list.

/**
 * To be called from any thread
 */
@Override
public List<String[]> list() {
    List<String[]> listed = new ArrayList<String[]>();
    File pipExecutable;
    Tuple<String, String> output;
    try {
        pipExecutable = interpreterInfo.searchExecutableForInterpreter("pip", false);
        // use system encoding
        String encoding = null;
        output = new SimpleRunner().runAndGetOutput(new String[] { pipExecutable.toString(), "list", "--format=columns" }, null, null, null, encoding);
    } catch (UnableToFindExecutableException e) {
        IPythonNature nature = new SystemPythonNature(interpreterInfo.getModulesManager().getInterpreterManager(), interpreterInfo);
        String[] parameters = SimplePythonRunner.preparePythonCallParameters(interpreterInfo.getExecutableOrJar(), "-m", new String[] { getPipModuleName(interpreterInfo), "list", "--format=columns" });
        output = new SimplePythonRunner().runAndGetOutput(parameters, null, nature, null, "utf-8");
    }
    List<String> splitInLines = StringUtils.splitInLines(output.o1, false);
    for (String line : splitInLines) {
        line = line.trim();
        List<String> split = StringUtils.split(line, ' ');
        if (split.size() == 2) {
            String p0 = split.get(0).trim();
            String p1 = split.get(1).trim();
            if (p0.toLowerCase().equals("package") && p1.toLowerCase().equals("version")) {
                continue;
            }
            if (p0.toLowerCase().startsWith("--") && p1.toLowerCase().startsWith("--")) {
                continue;
            }
            listed.add(new String[] { p0.trim(), p1.trim(), "<pip>" });
        }
    }
    if (output.o2.toLowerCase().contains("no module named pip")) {
        listed.add(new String[] { "pip not installed (or not found) in interpreter", "", "" });
    } else {
        for (String s : StringUtils.iterLines(output.o2)) {
            listed.add(new String[] { s, "", "" });
        }
    }
    return listed;
}
Also used : SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) UnableToFindExecutableException(org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) SimplePythonRunner(org.python.pydev.ast.runners.SimplePythonRunner) File(java.io.File)

Example 3 with UnableToFindExecutableException

use of org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException in project Pydev by fabioz.

the class CondaPackageManager method list.

@Override
public List<String[]> list() {
    List<String[]> listed = new ArrayList<String[]>();
    File condaExecutable;
    try {
        condaExecutable = PyDevCondaPreferences.findCondaExecutable(interpreterInfo);
    } catch (UnableToFindExecutableException e) {
        return errorToList(listed, e);
    }
    // use system encoding
    String encoding = null;
    Tuple<String, String> output = new SimpleRunner().runAndGetOutput(new String[] { condaExecutable.toString(), "list", "-p", prefix.toString(), "--json" }, null, null, null, encoding);
    try {
        JsonValue readFrom = JsonValue.readFrom(output.o1);
        JsonArray asArray = readFrom.asArray();
        for (JsonValue value : asArray) {
            JsonObject asObject = value.asObject();
            JsonValue name = asObject.get("name");
            JsonValue version = asObject.get("version");
            JsonValue channel = asObject.get("channel");
            JsonValue buildString = asObject.get("build_string");
            listed.add(new String[] { name.asString(), version.asString(), StringUtils.join("", buildString.asString(), " (", channel.asString(), ")") });
        }
    } catch (Exception e) {
        // Older version of Conda had a different json format and "list --json" wouldn't show pip info, so,
        // fallback to an implementation which just did a conda list without --json and parse its output
        output = new SimpleRunner().runAndGetOutput(new String[] { condaExecutable.toString(), "list", "-p", prefix.toString() }, null, null, null, encoding);
        List<String> splitInLines = StringUtils.splitInLines(output.o1, false);
        for (String line : splitInLines) {
            line = line.trim();
            if (line.startsWith("#")) {
                continue;
            }
            List<String> split = StringUtils.split(line, ' ');
            if (split.size() >= 3) {
                listed.add(new String[] { split.get(0), split.get(1), StringUtils.join(" - ", split.subList(2, split.size() - 1)) });
            } else if (split.size() == 2) {
                listed.add(new String[] { split.get(0), split.get(1), "" });
            }
        }
    }
    return listed;
}
Also used : JsonArray(org.python.pydev.json.eclipsesource.JsonArray) SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) UnableToFindExecutableException(org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException) ArrayList(java.util.ArrayList) JsonValue(org.python.pydev.json.eclipsesource.JsonValue) JsonObject(org.python.pydev.json.eclipsesource.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) UnableToFindExecutableException(org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException)

Example 4 with UnableToFindExecutableException

use of org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException in project Pydev by fabioz.

the class PipPackageManager method manage.

public void manage(final String[] initialCommand, final boolean autoRun) {
    File pipExecutable;
    String[] availableCommands = new String[] { "install <package>", "uninstall <package>" };
    try {
        pipExecutable = interpreterInfo.searchExecutableForInterpreter("pip", false);
    } catch (UnableToFindExecutableException e) {
        availableCommands = new String[] { "-m " + getPipModuleName(interpreterInfo) + " install <package>", "-m " + getPipModuleName(interpreterInfo) + " uninstall <package>" };
        pipExecutable = new File(interpreterInfo.getExecutableOrJar());
    }
    final String[] availableCommandsFinal = ArrayUtils.concatArrays(initialCommand, availableCommands);
    final File pipExecutableFinal = pipExecutable;
    ProcessWindow processWindow = new ProcessWindow(UIUtils.getActiveShell()) {

        @Override
        protected void configureShell(Shell shell) {
            super.configureShell(shell);
            shell.setText("Manage pip");
        }

        @Override
        protected String[] getAvailableCommands() {
            return availableCommandsFinal;
        }

        @Override
        protected String getSeeURL() {
            return "https://pip.pypa.io/en/stable";
        }

        @Override
        public Tuple<Process, String> createProcess(String[] arguments) {
            clearOutput();
            AbstractShell.restartAllShells();
            String[] cmdLine = ArrayUtils.concatArrays(new String[] { pipExecutableFinal.toString() }, arguments);
            return new SimpleRunner().run(cmdLine, workingDir, pythonPathNature.getNature(), null);
        }
    };
    IPythonPathNature pythonPathNature = null;
    try {
        pythonPathNature = interpreterInfo.getModulesManager().getNature().getPythonPathNature();
    } catch (Exception e) {
        Log.log(e);
    }
    processWindow.setParameters(null, pythonPathNature, pipExecutableFinal, pipExecutableFinal.getParentFile());
    processWindow.setAutoRun(autoRun);
    processWindow.open();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) AbstractShell(org.python.pydev.ast.codecompletion.shell.AbstractShell) SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) UnableToFindExecutableException(org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException) IPythonPathNature(org.python.pydev.core.IPythonPathNature) File(java.io.File) ProcessWindow(org.python.pydev.process_window.ProcessWindow) UnableToFindExecutableException(org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException)

Aggregations

File (java.io.File)4 SimpleRunner (org.python.pydev.ast.runners.SimpleRunner)4 UnableToFindExecutableException (org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException)4 ArrayList (java.util.ArrayList)3 Shell (org.eclipse.swt.widgets.Shell)2 AbstractShell (org.python.pydev.ast.codecompletion.shell.AbstractShell)2 ProcessWindow (org.python.pydev.process_window.ProcessWindow)2 List (java.util.List)1 SimplePythonRunner (org.python.pydev.ast.runners.SimplePythonRunner)1 IPythonNature (org.python.pydev.core.IPythonNature)1 IPythonPathNature (org.python.pydev.core.IPythonPathNature)1 JsonArray (org.python.pydev.json.eclipsesource.JsonArray)1 JsonObject (org.python.pydev.json.eclipsesource.JsonObject)1 JsonValue (org.python.pydev.json.eclipsesource.JsonValue)1 SystemPythonNature (org.python.pydev.plugin.nature.SystemPythonNature)1