Search in sources :

Example 1 with ISystemModulesManager

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

the class AdditionalSystemInterpreterInfo method recreateAllInfo.

public static void recreateAllInfo(IInterpreterManager manager, String interpreter, IProgressMonitor monitor) {
    synchronized (additionalSystemInfoLock) {
        try (GlobalFeedbackReporter r = GlobalFeedback.start("Full system reindex...")) {
            final IInterpreterInfo interpreterInfo = manager.getInterpreterInfo(interpreter, monitor);
            int grammarVersion = interpreterInfo.getGrammarVersion();
            AbstractAdditionalTokensInfo currInfo = AdditionalSystemInterpreterInfo.getAdditionalSystemInfo(manager, interpreter);
            if (currInfo != null) {
                currInfo.clearAllInfo();
            }
            InterpreterInfo defaultInterpreterInfo = (InterpreterInfo) manager.getInterpreterInfo(interpreter, monitor);
            ISystemModulesManager m = defaultInterpreterInfo.getModulesManager();
            AbstractAdditionalTokensInfo info = restoreInfoForModuleManager(monitor, m, "(system: " + manager.getManagerRelatedName() + " - " + interpreter + ")", new AdditionalSystemInterpreterInfo(manager, interpreter), null, grammarVersion);
            if (info != null) {
                // ok, set it and save it
                additionalSystemInfo.put(new Tuple<String, String>(manager.getManagerRelatedName(), interpreter), info);
                info.save();
            }
        } catch (Throwable e) {
            Log.log(e);
        }
    }
}
Also used : ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) GlobalFeedbackReporter(org.python.pydev.shared_core.global_feedback.GlobalFeedback.GlobalFeedbackReporter)

Example 2 with ISystemModulesManager

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

the class AbstractInterpreterManager method setInfos.

/* (non-Javadoc)
     * @see org.python.pydev.core.IInterpreterManager#setInfos(org.python.pydev.core.IInterpreterInfo[], java.util.Set, org.eclipse.core.runtime.IProgressMonitor)
     */
@Override
public void setInfos(IInterpreterInfo[] infos, Set<String> interpreterNamesToRestore, IProgressMonitor monitor) {
    // Set the string to persist!
    String s = AbstractInterpreterManager.getStringToPersist(infos);
    prefs.put(getPreferenceName(), s);
    try {
        prefs.flush();
    } catch (BackingStoreException e) {
        String message = e.getMessage();
        if (message == null || message.indexOf("File name not specified") == -1) {
            Log.log(e);
        }
    }
    IInterpreterInfo[] interpreterInfos;
    try {
        synchronized (this.lock) {
            modificationStamp += 1;
            clearInterpretersFromPersistedString();
            persistedString = s;
            // After setting the preference, get the actual infos (will be recreated).
            interpreterInfos = internalRecreateCacheGetInterpreterInfos();
            this.restorePythopathForInterpreters(monitor, interpreterNamesToRestore);
            for (InterpreterInfo info : this.exeToInfo.values()) {
                try {
                    ISystemModulesManager modulesManager = info.getModulesManager();
                    Object pythonPathHelper = modulesManager.getPythonPathHelper();
                    if (!(pythonPathHelper instanceof PythonPathHelper)) {
                        continue;
                    }
                    PythonPathHelper pathHelper = (PythonPathHelper) pythonPathHelper;
                    List<String> pythonpath = pathHelper.getPythonpath();
                    if (pythonpath == null || pythonpath.size() == 0) {
                        continue;
                    }
                    modulesManager.save();
                } catch (Throwable e) {
                    Log.log(e);
                }
            }
        }
        // Now, last step is updating the natures (the call must NOT be locked in this case).
        this.restorePythopathForNatures(monitor);
        // And in jython, changing the classpath also needs to restore it.
        for (IInterpreterInfo interpreter : interpreterInfos) {
            for (ShellId id : AbstractShell.getAllShellIds()) {
                AbstractShell.stopServerShell(interpreter, id);
            }
        }
        IInterpreterManagerListener[] managerListeners = listeners.getListeners();
        for (IInterpreterManagerListener iInterpreterManagerListener : managerListeners) {
            iInterpreterManagerListener.afterSetInfos(this, interpreterInfos);
        }
    } finally {
        AbstractShell.restartAllShells();
    }
    // In the regular process we do not create the global indexing for forced builtins, thus, we schedule a process
    // now which will be able to do that when checking if things are correct in the configuration.
    SyncSystemModulesManagerScheduler syncScheduler = DefaultSyncSystemModulesManagerScheduler.get();
    if (syncScheduler != null && interpreterNamesToRestore != null && interpreterNamesToRestore.size() > 0) {
        ArrayList<IInterpreterInfo> lst = new ArrayList<>(interpreterNamesToRestore.size());
        for (IInterpreterInfo info : interpreterInfos) {
            if (interpreterNamesToRestore.contains(info.getExecutableOrJar())) {
                lst.add(info);
            }
        }
        syncScheduler.addToCheck(this, lst.toArray(new IInterpreterInfo[lst.size()]));
    }
}
Also used : BackingStoreException(org.osgi.service.prefs.BackingStoreException) ArrayList(java.util.ArrayList) PythonPathHelper(org.python.pydev.ast.codecompletion.revisited.PythonPathHelper) ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) ShellId(org.python.pydev.core.ShellId) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) IInterpreterManagerListener(org.python.pydev.core.IInterpreterManagerListener) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) SyncSystemModulesManagerScheduler(org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManagerScheduler) DefaultSyncSystemModulesManagerScheduler(org.python.pydev.ast.codecompletion.revisited.DefaultSyncSystemModulesManagerScheduler)

Example 3 with ISystemModulesManager

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

the class AbstractInterpreterManager method getBuiltinMod.

@Override
public IModule getBuiltinMod(String projectInterpreterName, IModuleRequestState moduleRequest) {
    // Cache with the internal name.
    projectInterpreterName = getInternalName(projectInterpreterName);
    if (projectInterpreterName == null) {
        return null;
    }
    String cacheName = projectInterpreterName + "_" + moduleRequest.getAcceptTypeshed();
    IModule mod = builtinMod.get(cacheName);
    if (mod != null) {
        return mod;
    }
    try {
        InterpreterInfo interpreterInfo = this.getInterpreterInfo(projectInterpreterName, null);
        ISystemModulesManager modulesManager = interpreterInfo.getModulesManager();
        mod = modulesManager.getBuiltinModule("__builtin__", false, moduleRequest);
        if (mod == null) {
            // Python 3.0 has builtins and not __builtin__
            mod = modulesManager.getBuiltinModule("builtins", false, moduleRequest);
        }
        if (mod != null) {
            builtinMod.put(cacheName, mod);
        }
    } catch (MisconfigurationException e) {
        Log.log(e);
    }
    return builtinMod.get(cacheName);
}
Also used : IModule(org.python.pydev.core.IModule) ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo)

Example 4 with ISystemModulesManager

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

the class PyOrganizeImports method organizeImports.

@SuppressWarnings("unchecked")
private void organizeImports(PyEdit edit, final IDocument doc, IFile f, PySelection ps) throws MisconfigurationException, PythonNatureWithoutProjectException {
    DocumentRewriteSession session = null;
    String endLineDelim = ps.getEndLineDelim();
    List<IOrganizeImports> participants = null;
    if (f == null && !automatic) {
        // organizing single file ...
        // let's see if someone wants to make a better implementation in another plugin...
        participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_ORGANIZE_IMPORTS);
        for (IOrganizeImports organizeImports : participants) {
            if (!organizeImports.beforePerformArrangeImports(ps, edit, f)) {
                return;
            }
        }
    }
    String fileContents = doc.get();
    if (fileContents.contains("isort:skip_file") || fileContents.length() == 0) {
        return;
    }
    IAdaptable projectAdaptable = edit != null ? edit : f;
    String indentStr = edit != null ? edit.getIndentPrefs().getIndentationString() : DefaultIndentPrefs.get(f).getIndentationString();
    session = TextSelectionUtils.startWrite(doc);
    try {
        // Important: the remove and later update have to be done in the same session (since the remove
        // will just remove some names and he actual perform will remove the remaining if needed).
        // i.e.: from a import b <-- b will be removed by the OrganizeImportsFixesUnused and the
        // from a will be removed in the performArrangeImports later on.
        boolean removeUnusedImports = false;
        if (!automatic) {
            // Only go through the removal of unused imports if it's manually activated (not on automatic mode).
            removeUnusedImports = ImportsPreferencesPage.getDeleteUnusedImports(projectAdaptable);
            if (removeUnusedImports) {
                new OrganizeImportsFixesUnused().beforePerformArrangeImports(ps, edit, f);
            }
        }
        Set<String> knownThirdParty = new HashSet<String>();
        // isort itself already has a reasonable stdLib, so, don't do our own.
        String importEngine = ImportsPreferencesPage.getImportEngine(projectAdaptable);
        if (edit != null) {
            IPythonNature pythonNature = edit.getPythonNature();
            if (pythonNature != null) {
                IInterpreterInfo projectInterpreter = pythonNature.getProjectInterpreter();
                ISystemModulesManager modulesManager = projectInterpreter.getModulesManager();
                ModulesKey[] onlyDirectModules = modulesManager.getOnlyDirectModules();
                Set<String> stdLib = new HashSet<>();
                for (ModulesKey modulesKey : onlyDirectModules) {
                    if (modulesKey.file == null) {
                        int i = modulesKey.name.indexOf('.');
                        String name;
                        if (i < 0) {
                            name = modulesKey.name;
                        } else {
                            name = modulesKey.name.substring(0, i);
                        }
                        // Add all names to std lib
                        stdLib.add(name);
                    }
                }
                for (ModulesKey modulesKey : onlyDirectModules) {
                    int i = modulesKey.name.indexOf('.');
                    String name;
                    if (i < 0) {
                        name = modulesKey.name;
                    } else {
                        name = modulesKey.name.substring(0, i);
                    }
                    // Consider all in site-packages to be third party.
                    if (modulesKey.file != null && modulesKey.file.toString().contains("site-packages")) {
                        stdLib.remove(name);
                        knownThirdParty.add(name);
                    }
                }
            }
        }
        switch(importEngine) {
            case ImportsPreferencesPage.IMPORT_ENGINE_ISORT:
                if (fileContents.length() > 0) {
                    File targetFile = edit != null ? edit.getEditorFile() : null;
                    if (targetFile == null) {
                        if (f != null) {
                            IPath location = f.getLocation();
                            if (location != null) {
                                targetFile = location.toFile();
                            }
                        }
                    }
                    String isortResult = JythonModules.makeISort(fileContents, targetFile, knownThirdParty);
                    if (isortResult != null) {
                        try {
                            DocUtils.updateDocRangeWithContents(doc, fileContents, isortResult.toString());
                        } catch (Exception e) {
                            Log.log(StringUtils.format("Error trying to apply isort result. Curr doc:\n>>>%s\n<<<.\nNew doc:\\n>>>%s\\n<<<.", fileContents, isortResult.toString()), e);
                        }
                    }
                }
                break;
            case ImportsPreferencesPage.IMPORT_ENGINE_REGULAR_SORT:
                performArrangeImports(doc, removeUnusedImports, endLineDelim, indentStr, automatic, edit);
                break;
            default:
                // case ImportsPreferencesPage.IMPORT_ENGINE_PEP_8:
                if (f == null) {
                    f = edit.getIFile();
                }
                IProject p = f != null ? f.getProject() : null;
                pep8PerformArrangeImports(doc, removeUnusedImports, endLineDelim, p, indentStr, automatic, edit);
                break;
        }
        if (participants != null) {
            for (IOrganizeImports organizeImports : participants) {
                organizeImports.afterPerformArrangeImports(ps, edit);
            }
        }
    } finally {
        TextSelectionUtils.endWrite(doc, session);
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IPath(org.eclipse.core.runtime.IPath) IPythonNature(org.python.pydev.core.IPythonNature) SyntaxErrorException(org.python.pydev.core.docutils.SyntaxErrorException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IProject(org.eclipse.core.resources.IProject) ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) ModulesKey(org.python.pydev.core.ModulesKey) IFile(org.eclipse.core.resources.IFile) File(java.io.File) HashSet(java.util.HashSet)

Example 5 with ISystemModulesManager

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

the class ProjectModulesManager method getCompletePythonPath.

/**
 * @see org.python.pydev.core.IProjectModulesManager#getCompletePythonPath()
 */
@Override
public List<String> getCompletePythonPath(IInterpreterInfo interpreter, IInterpreterManager manager) {
    List<String> l = new ArrayList<String>();
    IModulesManager[] managersInvolved = getManagersInvolved(true);
    for (IModulesManager m : managersInvolved) {
        if (m instanceof ISystemModulesManager) {
            ISystemModulesManager systemModulesManager = (ISystemModulesManager) m;
            l.addAll(systemModulesManager.getCompletePythonPath(interpreter, manager));
        } else {
            PythonPathHelper h = (PythonPathHelper) m.getPythonPathHelper();
            if (h != null) {
                List<String> pythonpath = h.getPythonpath();
                // Note: this was previously only l.addAll(pythonpath), and was changed to the code below as a place
                // to check for consistencies in the pythonpath stored in the pythonpath helper and the pythonpath
                // available in the PythonPathNature (in general, when requesting it the PythonPathHelper should be
                // used, as it's a cache for the resolved values of the PythonPathNature).
                boolean forceCheck = false;
                ProjectModulesManager m2 = null;
                String onlyProjectPythonPathStr = null;
                if (m instanceof ProjectModulesManager) {
                    long currentTimeMillis = System.currentTimeMillis();
                    m2 = (ProjectModulesManager) m;
                    // it should be fast to get it too if it's consistent).
                    if (pythonpath.size() == 0 || currentTimeMillis - m2.checkedPythonpathConsistency > 20 * 1000) {
                        try {
                            IPythonNature n = m.getNature();
                            if (n != null) {
                                IPythonPathNature pythonPathNature = n.getPythonPathNature();
                                if (pythonPathNature != null) {
                                    onlyProjectPythonPathStr = pythonPathNature.getOnlyProjectPythonPathStr(true);
                                    m2.checkedPythonpathConsistency = currentTimeMillis;
                                    forceCheck = true;
                                }
                            }
                        } catch (Exception e) {
                            Log.log(e);
                        }
                    }
                }
                if (forceCheck) {
                    // Check if it's actually correct and auto-fix if it's not.
                    List<String> parsed = PythonPathHelper.parsePythonPathFromStr(onlyProjectPythonPathStr, null);
                    if (m2.nature != null && !new HashSet<String>(parsed).equals(new HashSet<String>(pythonpath))) {
                        // Make it right at this moment (so any other place that calls it before the restore
                        // takes place has the proper version).
                        h.setPythonPath(parsed);
                        // Force a rebuild as the PythonPathHelper paths are not up to date.
                        m2.nature.rebuildPath();
                    }
                    // add the proper paths
                    l.addAll(parsed);
                } else {
                    l.addAll(pythonpath);
                }
            }
        }
    }
    return l;
}
Also used : ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) IPythonPathNature(org.python.pydev.core.IPythonPathNature) IModulesManager(org.python.pydev.core.IModulesManager) IProjectModulesManager(org.python.pydev.core.IProjectModulesManager) CoreException(org.eclipse.core.runtime.CoreException) ISystemModulesManager(org.python.pydev.core.ISystemModulesManager)

Aggregations

ISystemModulesManager (org.python.pydev.core.ISystemModulesManager)16 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)6 MisconfigurationException (org.python.pydev.core.MisconfigurationException)6 ArrayList (java.util.ArrayList)5 File (java.io.File)4 CoreException (org.eclipse.core.runtime.CoreException)3 InterpreterInfo (org.python.pydev.ast.interpreter_managers.InterpreterInfo)3 IModule (org.python.pydev.core.IModule)3 IModulesManager (org.python.pydev.core.IModulesManager)3 IPythonNature (org.python.pydev.core.IPythonNature)3 PythonNatureWithoutProjectException (org.python.pydev.core.PythonNatureWithoutProjectException)3 AbstractAdditionalDependencyInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo)2 AdditionalSystemInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo)2 IOException (java.io.IOException)2 IProject (org.eclipse.core.resources.IProject)2 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)2 PythonPathHelper (org.python.pydev.ast.codecompletion.revisited.PythonPathHelper)2 PythonInterpreterManager (org.python.pydev.ast.interpreter_managers.PythonInterpreterManager)2 IInfo (org.python.pydev.core.IInfo)2 IProjectModulesManager (org.python.pydev.core.IProjectModulesManager)2