Search in sources :

Example 6 with SystemPythonNature

use of org.python.pydev.plugin.nature.SystemPythonNature 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 7 with SystemPythonNature

use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.

the class RefactorerFindReferences method findPossibleReferences.

/**
 * Find the references that may have the text we're looking for.
 *
 * @param request the request with the info for the find
 * @return an array of IFile with the files that may have the references we're
 * interested about (note that those may not actually contain the matches we're
 * interested in -- it is just a helper to refine our search).
 */
public List<Tuple<List<ModulesKey>, IPythonNature>> findPossibleReferences(RefactoringRequest request) throws OperationCanceledException {
    String initialName = request.qualifier;
    List<Tuple<List<ModulesKey>, IPythonNature>> ret = request.getPossibleReferences(initialName);
    if (ret != null) {
        return ret;
    }
    if (FORCED_RETURN != null) {
        ret = new ArrayList<Tuple<List<ModulesKey>, IPythonNature>>();
        for (Tuple<List<ModulesKey>, IPythonNature> f : FORCED_RETURN) {
            // only for testing purposes
            for (ModulesKey k : f.o1) {
                String object = FileUtils.getFileContents(k.file);
                if (object.indexOf(request.qualifier) != -1) {
                    ret.add(new Tuple<List<ModulesKey>, IPythonNature>(Arrays.asList(k), f.o2));
                }
            }
        }
        return ret;
    }
    ret = new ArrayList<Tuple<List<ModulesKey>, IPythonNature>>();
    try {
        try {
            IProject project = request.nature.getProject();
            List<Tuple<AbstractAdditionalTokensInfo, IPythonNature>> infoAndNature = null;
            if (project == null) {
                if (request.nature instanceof SystemPythonNature) {
                    SystemPythonNature systemPythonNature = (SystemPythonNature) request.nature;
                    int interpreterType = systemPythonNature.getInterpreterType();
                    List<IPythonNature> naturesRelatedTo = PythonNature.getPythonNaturesRelatedTo(interpreterType);
                    infoAndNature = new ArrayList<Tuple<AbstractAdditionalTokensInfo, IPythonNature>>();
                    for (IPythonNature iPythonNature : naturesRelatedTo) {
                        if (iPythonNature.getProject() != null && iPythonNature.getProject().isAccessible()) {
                            AbstractAdditionalTokensInfo o1 = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(iPythonNature);
                            if (o1 != null) {
                                infoAndNature.add(new Tuple<AbstractAdditionalTokensInfo, IPythonNature>(o1, iPythonNature));
                            }
                        }
                    }
                }
            } else {
                infoAndNature = AdditionalProjectInterpreterInfo.getAdditionalInfoAndNature(request.nature, false, true, true);
            }
            if (infoAndNature == null || infoAndNature.size() == 0) {
                return ret;
            }
            // long initial = System.currentTimeMillis();
            request.getMonitor().beginTask("Find possible references", infoAndNature.size());
            request.getMonitor().setTaskName("Find possible references");
            try {
                for (Tuple<AbstractAdditionalTokensInfo, IPythonNature> tuple : infoAndNature) {
                    try {
                        SubProgressMonitor sub = new SubProgressMonitor(request.getMonitor(), 1);
                        request.pushMonitor(sub);
                        if (tuple.o1 instanceof AdditionalProjectInterpreterInfo && tuple.o2 != null) {
                            AdditionalProjectInterpreterInfo info = (AdditionalProjectInterpreterInfo) tuple.o1;
                            List<ModulesKey> modulesWithToken = info.getModulesWithToken(initialName, sub);
                            if (sub.isCanceled()) {
                                break;
                            }
                            ret.add(new Tuple<List<ModulesKey>, IPythonNature>(modulesWithToken, tuple.o2));
                        }
                    } finally {
                        request.popMonitor().done();
                    }
                }
            } finally {
                request.getMonitor().done();
            }
        // System.out.println("Total: " + ((System.currentTimeMillis() - initial) / 1000.));
        } catch (MisconfigurationException e) {
            throw new RuntimeException(e);
        }
    } catch (OperationCanceledException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    request.setPossibleReferences(initialName, ret);
    return ret;
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IPythonNature(org.python.pydev.core.IPythonNature) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IProject(org.eclipse.core.resources.IProject) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) ModulesKey(org.python.pydev.core.ModulesKey) ArrayList(java.util.ArrayList) List(java.util.List) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 8 with SystemPythonNature

use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.

the class CtxParticipant method computeConsoleCompletions.

// Console completions ---------------------------------------------------------------------------------------------
/**
 * IPyDevCompletionParticipant2
 */
@Override
public Collection<ICompletionProposalHandle> computeConsoleCompletions(ActivationTokenAndQualifier tokenAndQual, Set<IPythonNature> naturesUsed, IScriptConsoleViewer viewer, int requestOffset) {
    List<ICompletionProposalHandle> completions = new ArrayList<ICompletionProposalHandle>();
    if (tokenAndQual.activationToken != null && tokenAndQual.activationToken.length() > 0) {
        // we only want
        return completions;
    }
    String qual = tokenAndQual.qualifier;
    if (qual.length() >= PyCodeCompletionPreferences.getCharsForContextInsensitiveGlobalTokensCompletion() && naturesUsed != null && naturesUsed.size() > 0) {
        // at least n characters required...
        boolean addAutoImport = AnalysisPreferences.doAutoImport(null);
        int qlen = qual.length();
        boolean useSubstringMatchInCodeCompletion = PyCodeCompletionPreferences.getUseSubstringMatchInCodeCompletion();
        IFilter nameFilter = PyCodeCompletionUtils.getNameFilter(useSubstringMatchInCodeCompletion, qual);
        for (IPythonNature nature : naturesUsed) {
            AbstractAdditionalTokensInfo additionalInfo;
            try {
                if (nature instanceof SystemPythonNature) {
                    SystemPythonNature systemPythonNature = (SystemPythonNature) nature;
                    additionalInfo = AdditionalSystemInterpreterInfo.getAdditionalSystemInfo(systemPythonNature.getRelatedInterpreterManager(), systemPythonNature.getProjectInterpreter().getExecutableOrJar());
                    fillNatureCompletionsForConsole(viewer, requestOffset, completions, qual, addAutoImport, qlen, nameFilter, nature, additionalInfo, useSubstringMatchInCodeCompletion);
                } else {
                    additionalInfo = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
                    fillNatureCompletionsForConsole(viewer, requestOffset, completions, qual, addAutoImport, qlen, nameFilter, nature, additionalInfo, useSubstringMatchInCodeCompletion);
                }
            } catch (MisconfigurationException e) {
                Log.log(e);
            }
        }
    }
    return completions;
}
Also used : IFilter(org.python.pydev.ast.codecompletion.PyCodeCompletionUtils.IFilter) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)

Example 9 with SystemPythonNature

use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.

the class AttachToProcess method doIt.

protected void doIt() throws Exception {
    IProcessList processList = PlatformUtils.getProcessList();
    IProcessInfo[] processList2 = processList.getProcessList();
    TreeNode<Object> root = new TreeNode<Object>(null, null);
    for (IProcessInfo iProcessInfo : processList2) {
        new TreeNode<>(root, iProcessInfo);
    }
    TreeNode<Object> element = new Select1Dialog() {

        @Override
        protected String getInitialFilter() {
            return "*python*";
        }

        @Override
        protected ILabelProvider getLabelProvider() {
            return new TreeNodeLabelProvider() {

                @Override
                public Image getImage(Object element) {
                    return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.PUBLIC_ATTR_ICON));
                }

                @SuppressWarnings("unchecked")
                @Override
                public String getText(Object element) {
                    if (element == null) {
                        return "null";
                    }
                    TreeNode<Object> node = (TreeNode<Object>) element;
                    Object data = node.data;
                    if (data instanceof IProcessInfo) {
                        IProcessInfo iProcessInfo = (IProcessInfo) data;
                        return iProcessInfo.getPid() + " - " + iProcessInfo.getName();
                    }
                    return "Unexpected: " + data;
                }
            };
        }
    }.selectElement(root);
    if (element != null) {
        IProcessInfo p = (IProcessInfo) element.data;
        int pid = p.getPid();
        if (!PydevRemoteDebuggerServer.isRunning()) {
            // I.e.: the remote debugger server must be on so that we can attach to it.
            PydevRemoteDebuggerServer.startServer();
        }
        // Select interpreter
        IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        IInterpreterManager interpreterManager = InterpreterManagersAPI.getPythonInterpreterManager();
        if (interpreterManager == null) {
            MessageDialog.openError(workbenchWindow.getShell(), "No interpreter manager.", "No interpreter manager was available for attaching to a process.");
        }
        IInterpreterInfo[] interpreters = interpreterManager.getInterpreterInfos();
        if (interpreters == null || interpreters.length == 0) {
            MessageDialog.openError(workbenchWindow.getShell(), "No interpreters for creating console", "An interpreter that matches the architecture of the target process must be configured in the interpreter preferences.");
            return;
        }
        SelectionDialog listDialog = AbstractInterpreterPreferencesPage.createChooseIntepreterInfoDialog(workbenchWindow, interpreters, "Select interpreter which matches the architecture of the target process (i.e.: 32/64 bits).", false);
        int open = listDialog.open();
        if (open != ListDialog.OK || listDialog.getResult().length != 1) {
            return;
        }
        Object[] result = listDialog.getResult();
        IInterpreterInfo interpreter;
        if (result == null || result.length == 0) {
            interpreter = interpreters[0];
        } else {
            interpreter = ((IInterpreterInfo) result[0]);
        }
        SimplePythonRunner runner = new SimplePythonRunner();
        IPath relative = new Path("pysrc").append("pydevd_attach_to_process").append("attach_pydevd.py");
        String script = CorePlugin.getBundleInfo().getRelativePath(relative).getAbsolutePath();
        String[] args = new String[] { "--port", "" + DebugPluginPrefsInitializer.getRemoteDebuggerPort(), "--pid", "" + pid, "--protocol", "http" };
        IPythonNature nature = new SystemPythonNature(interpreterManager, interpreter);
        String[] s = SimplePythonRunner.preparePythonCallParameters(interpreter.getExecutableOrJar(), script, args);
        Tuple<Process, String> run = runner.run(s, (File) null, nature, new NullProgressMonitor());
        if (run.o1 != null) {
            ShowProcessOutputDialog dialog = new ShowProcessOutputDialog(UIUtils.getActiveShell(), run.o1);
            dialog.open();
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProcessList(org.python.pydev.shared_core.utils.IProcessList) IPythonNature(org.python.pydev.core.IPythonNature) Image(org.eclipse.swt.graphics.Image) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) TreeNode(org.python.pydev.shared_core.structure.TreeNode) TreeNodeLabelProvider(org.python.pydev.ui.dialogs.TreeNodeLabelProvider) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IPath(org.eclipse.core.runtime.IPath) Select1Dialog(org.python.pydev.ui.dialogs.Select1Dialog) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IProcessInfo(org.python.pydev.shared_core.utils.IProcessInfo) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) SimplePythonRunner(org.python.pydev.ast.runners.SimplePythonRunner)

Example 10 with SystemPythonNature

use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.

the class SystemModulesManager method getNature.

@Override
public IPythonNature getNature() {
    if (nature == null) {
        IInterpreterManager manager = getInterpreterManager();
        nature = new SystemPythonNature(manager, this.info);
    }
    return nature;
}
Also used : SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Aggregations

SystemPythonNature (org.python.pydev.plugin.nature.SystemPythonNature)17 IPythonNature (org.python.pydev.core.IPythonNature)8 MisconfigurationException (org.python.pydev.core.MisconfigurationException)8 IInterpreterManager (org.python.pydev.core.IInterpreterManager)7 ArrayList (java.util.ArrayList)6 File (java.io.File)5 CoreException (org.eclipse.core.runtime.CoreException)5 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 IProject (org.eclipse.core.resources.IProject)3 SimplePythonRunner (org.python.pydev.ast.runners.SimplePythonRunner)3 SimpleRunner (org.python.pydev.ast.runners.SimpleRunner)3 Tuple (org.python.pydev.shared_core.structure.Tuple)3 AbstractAdditionalTokensInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)2 AdditionalProjectInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo)2 IOException (java.io.IOException)2 ZipFile (java.util.zip.ZipFile)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 Path (org.eclipse.core.runtime.Path)2 SelectionDialog (org.eclipse.ui.dialogs.SelectionDialog)2