Search in sources :

Example 31 with IInterpreterManager

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

the class SyncSystemModulesManagerTest method testScheduleCheckForUpdates.

public void testScheduleCheckForUpdates() throws Exception {
    setupEnv();
    Map<IInterpreterManager, Map<String, IInterpreterInfo>> managerToNameToInfo = InterpreterManagersAPI.getInterpreterManagerToInterpreterNameToInfo();
    SyncSystemModulesManagerScheduler scheduler = new SyncSystemModulesManagerScheduler();
    final Set changes = Collections.synchronizedSet(new HashSet<>());
    try {
        Set<Entry<IInterpreterManager, Map<String, IInterpreterInfo>>> entrySet = managerToNameToInfo.entrySet();
        SyncSystemModulesManagerScheduler.IInfoTrackerListener listener = new IInfoTrackerListener() {

            @Override
            public void onChangedIInterpreterInfo(InfoTracker infoTracker, File file) {
                changes.add(file);
            }
        };
        for (Entry<IInterpreterManager, Map<String, IInterpreterInfo>> entry : entrySet) {
            Map<String, IInterpreterInfo> value = entry.getValue();
            scheduler.afterSetInfos(entry.getKey(), value.values().toArray(new IInterpreterInfo[value.size()]), listener);
        }
        final File module4File = new File(libDir, "module4.py");
        FileUtils.writeStrToFile("class Module3:pass", module4File);
        TestUtils.waitUntilCondition(new ICallback<String, Object>() {

            @Override
            public String call(Object arg) {
                if (changes.contains(module4File)) {
                    return null;
                }
                return "Changes not found.";
            }
        });
        changes.clear();
        final File myPthFile = new File(libDir, "my.pth");
        FileUtils.writeStrToFile("./setuptools-1.1.6-py2.6.egg", myPthFile);
        TestUtils.waitUntilCondition(new ICallback<String, Object>() {

            @Override
            public String call(Object arg) {
                if (changes.contains(myPthFile)) {
                    return null;
                }
                return "Changes not found.";
            }
        });
        synchronized (this) {
            // Wait a bit as we may have 2 notifications (for creation and modification of the pth).
            this.wait(250);
        }
        // Now, add an unrelated directory: no notifications are expected then.
        changes.clear();
        final File myUnrelatedDir = new File(libDir, "unrelatedDir");
        myUnrelatedDir.mkdir();
        synchronized (this) {
            this.wait(250);
        }
        // no changes expected
        assertEquals(new HashSet<>(), changes);
    } finally {
        scheduler.stop();
    }
    changes.clear();
    final File myPthFile2 = new File(libDir, "my2.pth");
    FileUtils.writeStrToFile("./setuptools-1.1.7-py2.6.egg", myPthFile2);
    synchronized (this) {
        this.wait(250);
    }
    assertEquals(new HashSet<>(), changes);
}
Also used : IInfoTrackerListener(org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManagerScheduler.IInfoTrackerListener) HashSet(java.util.HashSet) Set(java.util.Set) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IInfoTrackerListener(org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManagerScheduler.IInfoTrackerListener) InfoTracker(org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManagerScheduler.InfoTracker) ZipEntry(java.util.zip.ZipEntry) Entry(java.util.Map.Entry) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) SyncSystemModulesManagerScheduler(org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManagerScheduler)

Example 32 with IInterpreterManager

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

the class CodeCompletionTestsBase method afterRestorSystemPythonPath.

/**
 * @param info the information for the system manager that we just restored
 */
protected void afterRestorSystemPythonPath(InterpreterInfo info) {
    // has to be restored for the project, as we just restored the system pythonpath
    nature = null;
    // ok, the system manager must be there
    assertTrue(info.getModulesManager().getSize(true) > 0);
    // and it must be registered as the pydev interpreter manager
    IInterpreterManager iMan2 = getInterpreterManager();
    InterpreterInfo info2;
    try {
        info2 = (InterpreterInfo) iMan2.getDefaultInterpreterInfo(false);
    } catch (MisconfigurationException e) {
        throw new RuntimeException(e);
    }
    assertTrue(info2 == info);
    // does it have the loaded modules?
    assertTrue(info2.getModulesManager().getSize(true) > 0);
    assertTrue(info2.getModulesManager().getBuiltins().length > 0);
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 33 with IInterpreterManager

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

the class InterpreterInfoBuilder method syncInfoToPythonPath.

@Override
public BuilderResult syncInfoToPythonPath(IProgressMonitor monitor, InterpreterInfo info) {
    ISystemModulesManager modulesManager = info.getModulesManager();
    IInterpreterManager manager = modulesManager.getInterpreterManager();
    AbstractAdditionalDependencyInfo additionalInfo;
    try {
        additionalInfo = AdditionalSystemInterpreterInfo.getAdditionalSystemInfo(manager, info.getExecutableOrJar());
    } catch (MisconfigurationException e) {
        Log.log(e);
        return BuilderResult.OK;
    }
    return this.syncInfoToPythonPath(monitor, info, additionalInfo);
}
Also used : ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) MisconfigurationException(org.python.pydev.core.MisconfigurationException) AbstractAdditionalDependencyInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 34 with IInterpreterManager

use of org.python.pydev.core.IInterpreterManager 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 35 with IInterpreterManager

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

the class SyncSystemModulesManager method updateStructures.

/**
 * Here we'll update the tree structure to be shown to the user with the changes (root).
 * The managerToNameToInfo structure has the information on the interpreter manager and related
 * interpreter infos for which the changes should be checked.
 */
public void updateStructures(IProgressMonitor monitor, final DataAndImageTreeNode root, ManagerInfoToUpdate managerToNameToInfo, CreateInterpreterInfoCallback callback) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    IImageCache imageCache = SharedCorePlugin.getImageCache();
    if (imageCache == null) {
        imageCache = new IImageCache() {

            @Override
            public IImageHandle getStringDecorated(String key, String stringToAddToDecoration) {
                return null;
            }

            @Override
            public IImageHandle getImageDecorated(String key, String decoration, int decorationLocation, String secondDecoration, int secondDecorationLocation) {
                return null;
            }

            @Override
            public IImageHandle getImageDecorated(String key, String decoration, int decorationLocation) {
                return null;
            }

            @Override
            public IImageHandle getImageDecorated(String key, String decoration) {
                return null;
            }

            @Override
            public IImageDescriptor getDescriptor(String projectIcon) {
                return null;
            }

            @Override
            public IImageHandle get(String key) {
                return null;
            }
        };
    }
    for (Tuple<IInterpreterManager, IInterpreterInfo> infos : managerToNameToInfo.getManagerAndInfos()) {
        IInterpreterManager manager = infos.o1;
        IInterpreterInfo internalInfo = infos.o2;
        String executable = internalInfo.getExecutableOrJar();
        IInterpreterInfo newInterpreterInfo = callback.createInterpreterInfo(manager, executable, monitor);
        if (newInterpreterInfo == null) {
            continue;
        }
        DefaultPathsForInterpreterInfo defaultPaths = new DefaultPathsForInterpreterInfo();
        OrderedSet<String> newEntries = new OrderedSet<String>(newInterpreterInfo.getPythonPath());
        newEntries.removeAll(internalInfo.getPythonPath());
        // Iterate over the new entries to suggest what should be added (we already have only what's not there).
        for (Iterator<String> it = newEntries.iterator(); it.hasNext(); ) {
            String entryInPythonpath = it.next();
            if (!defaultPaths.selectByDefault(entryInPythonpath) || !defaultPaths.exists(entryInPythonpath)) {
                // Don't suggest the addition of entries in the workspace or entries which do not exist.
                it.remove();
            }
        }
        // Iterate over existing entries to suggest what should be removed.
        OrderedSet<String> removedEntries = new OrderedSet<String>();
        List<String> pythonPath = internalInfo.getPythonPath();
        for (String string : pythonPath) {
            if (!new File(string).exists()) {
                // Only suggest a removal if it was removed from the filesystem.
                removedEntries.add(string);
            }
        }
        if (newEntries.size() > 0 || removedEntries.size() > 0) {
            DataAndImageTreeNode<IInterpreterInfo> interpreterNode = new DataAndImageTreeNode<IInterpreterInfo>(root, internalInfo, imageCache.get(UIConstants.PY_INTERPRETER_ICON));
            for (String s : newEntries) {
                new DataAndImageTreeNode(interpreterNode, new PythonpathChange(s, true), imageCache.get(UIConstants.LIB_SYSTEM));
            }
            for (String s : removedEntries) {
                new DataAndImageTreeNode(interpreterNode, new PythonpathChange(s, false), imageCache.get(UIConstants.REMOVE_LIB_SYSTEM));
            }
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) OrderedSet(org.python.pydev.shared_core.structure.OrderedSet) IImageCache(org.python.pydev.shared_core.image.IImageCache) IImageDescriptor(org.python.pydev.shared_core.image.IImageDescriptor) DefaultPathsForInterpreterInfo(org.python.pydev.ast.interpreter_managers.DefaultPathsForInterpreterInfo) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IImageHandle(org.python.pydev.shared_core.image.IImageHandle) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) File(java.io.File)

Aggregations

IInterpreterManager (org.python.pydev.core.IInterpreterManager)61 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)30 MisconfigurationException (org.python.pydev.core.MisconfigurationException)14 InterpreterInfo (org.python.pydev.ast.interpreter_managers.InterpreterInfo)13 CoreException (org.eclipse.core.runtime.CoreException)12 File (java.io.File)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)11 IPythonNature (org.python.pydev.core.IPythonNature)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)8 IProject (org.eclipse.core.resources.IProject)7 SystemPythonNature (org.python.pydev.plugin.nature.SystemPythonNature)7 Map (java.util.Map)6 Tuple (org.python.pydev.shared_core.structure.Tuple)6 AdditionalSystemInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo)5 Collection (java.util.Collection)5 HashSet (java.util.HashSet)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 PyEdit (org.python.pydev.editor.PyEdit)4 DataAndImageTreeNode (org.python.pydev.shared_core.structure.DataAndImageTreeNode)4