Search in sources :

Example 1 with PythonPathHelper

use of org.python.pydev.ast.codecompletion.revisited.PythonPathHelper 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 2 with PythonPathHelper

use of org.python.pydev.ast.codecompletion.revisited.PythonPathHelper in project Pydev by fabioz.

the class NatureGroup method calculateChildren.

@Override
protected void calculateChildren() throws MisconfigurationException {
    ICodeCompletionASTManager astManager = nature.getAstManager();
    if (astManager == null) {
        addLeaf("AST manager == null (should happen only in the plugin initialization) -- skipping other checks.");
        return;
    }
    IModulesManager projectModulesManager = astManager.getModulesManager();
    if (projectModulesManager == null) {
        addLeaf("Modules manager == null (should happen only in the plugin initialization) -- skipping other checks.");
        return;
    }
    PythonPathHelper pythonPathHelper = (PythonPathHelper) projectModulesManager.getPythonPathHelper();
    if (pythonPathHelper == null) {
        addLeaf("PythonPathHelper == null (should happen only in the plugin initialization) -- skipping other checks.");
        return;
    }
    List<String> pythonpath = pythonPathHelper.getPythonpath();
    for (String s : pythonpath) {
        addLeaf("PYTHONPATH: " + s);
    }
    HashSet<ModulesKey> expectedModuleNames = new HashSet<ModulesKey>();
    for (String string : pythonpath) {
        File file = new File(string);
        if (file.isDirectory()) {
            // TODO: Handle zip file modules!
            Collection<PyFileInfo> modulesBelow = PythonPathHelper.getModulesBelow(file, new NullProgressMonitor(), pythonpath).getFoundPyFileInfos();
            for (PyFileInfo fileInfo : modulesBelow) {
                File moduleFile = fileInfo.getFile();
                String modName = pythonPathHelper.resolveModule(FileUtils.getFileAbsolutePath(moduleFile), true, nature.getProject());
                if (modName != null) {
                    expectedModuleNames.add(new ModulesKey(modName, moduleFile));
                } else {
                    if (PythonPathHelper.isValidModuleLastPart(StringUtils.stripExtension((moduleFile.getName())))) {
                        addLeaf(StringUtils.format("Unable to resolve module: %s (gotten null module name)", moduleFile));
                    }
                }
            }
        } else {
            if (!file.exists()) {
                addLeaf(StringUtils.format("File %s is referenced in the pythonpath but does not exist.", file));
            } else {
                addLeaf(org.python.pydev.shared_core.string.StringUtils.format("File %s not handled (TODO: Fix zip files support in the viewer).", file));
            }
        }
    }
    IntegrityInfo info = new IntegrityInfo();
    info.nature = nature;
    ModulesKey[] onlyDirectModules = projectModulesManager.getOnlyDirectModules();
    TreeSet<ModulesKey> inModulesManager = new TreeSet<ModulesKey>(Arrays.asList(onlyDirectModules));
    Set<String> allAdditionalInfoModuleNames = new TreeSet<String>();
    List<Tuple<AbstractAdditionalTokensInfo, IPythonNature>> additionalInfoAndNature = AdditionalProjectInterpreterInfo.getAdditionalInfoAndNature(nature, false, false, false);
    AbstractAdditionalTokensInfo additionalProjectInfo;
    if (additionalInfoAndNature.size() == 0) {
        addChild(new LeafElement(this, "No additional infos found (1 expected) -- skipping other checks."));
        return;
    } else {
        if (additionalInfoAndNature.size() > 1) {
            addChild(new LeafElement(this, StringUtils.format("%s additional infos found (only 1 expected) -- continuing checks but analysis may be wrong.", additionalInfoAndNature.size())));
        }
        additionalProjectInfo = additionalInfoAndNature.get(0).o1;
        allAdditionalInfoModuleNames.addAll(additionalProjectInfo.getAllModulesWithTokens());
    }
    for (ModulesKey key : inModulesManager) {
        if (!expectedModuleNames.contains(key)) {
            info.modulesNotInDisk.add(key);
            addChild(new LeafElement(this, StringUtils.format("%s exists in memory but not in the disk.", key)));
        }
    }
    ModulesKey tempKey = new ModulesKey(null, null);
    for (String s : allAdditionalInfoModuleNames) {
        tempKey.name = s;
        if (!expectedModuleNames.contains(tempKey)) {
            info.additionalModulesNotInDisk.add(s);
            addChild(new LeafElement(this, StringUtils.format("%s exists in the additional info but not in the disk.", s)));
        }
    }
    for (ModulesKey key : expectedModuleNames) {
        boolean isInModulesManager = inModulesManager.contains(key);
        if (!isInModulesManager) {
            info.modulesNotInMemory.add(key);
            addChild(new LeafElement(this, StringUtils.format("%s exists in the disk but not in memory.", key)));
        }
        if (!allAdditionalInfoModuleNames.contains(key.name)) {
            try {
                AbstractModule mod = AbstractModule.createModule(key.name, key.file, info.nature, true);
                if (!(mod instanceof SourceModule)) {
                    continue;
                }
                SourceModule module = (SourceModule) mod;
                if (module == null || module.getAst() == null) {
                    addChild(new LeafElement(this, StringUtils.format("Warning: cannot parse: %s - %s (so, it's ok not having additional info on it)", key.name, key.file)));
                } else {
                    try {
                        Iterator<ASTEntry> innerEntriesForAST = AbstractAdditionalDependencyInfo.getInnerEntriesForAST(module.getAst()).o2;
                        if (innerEntriesForAST.hasNext()) {
                            info.moduleNotInAdditionalInfo.add(module);
                            addChild(new LeafElement(this, StringUtils.format("The additional info index of the module: %s is not updated.", key.name)));
                        }
                    } catch (Exception e) {
                        addChild(new LeafElement(this, StringUtils.format("Unexpected error happened on: %s - %s: %s", key.name, key.file, e.getMessage())));
                    }
                }
            } catch (IOException e) {
                // OK, it cannot be parsed, so, we cannot generate its info
                addChild(new LeafElement(this, StringUtils.format("Warning: cannot parse: %s - %s (so, it's ok not having additional info on it)", key.name, key.file)));
            }
        }
    }
    // modules manager
    if (info.modulesNotInDisk.size() > 0) {
        for (ModulesKey m : info.modulesNotInDisk) {
            addChild(new LeafElement(this, StringUtils.format("FIX: Removing from modules manager: %s", m)));
        }
        projectModulesManager.removeModules(info.modulesNotInDisk);
    }
    for (ModulesKey key : info.modulesNotInMemory) {
        addChild(new LeafElement(this, "FIX: Adding to modules manager: " + key));
        projectModulesManager.addModule(key);
    }
    // additional info
    for (String s : info.additionalModulesNotInDisk) {
        addChild(new LeafElement(this, StringUtils.format("FIX: Removing from additional info: %s", s)));
        additionalProjectInfo.removeInfoFromModule(s, true);
    }
    for (SourceModule mod : info.moduleNotInAdditionalInfo) {
        addChild(new LeafElement(this, StringUtils.format("FIX: Adding to additional info: %s", mod.getName())));
        additionalProjectInfo.addAstInfo(mod.getAst(), mod.getModulesKey(), true);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TreeSet(java.util.TreeSet) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) PyFileInfo(org.python.pydev.ast.listing_utils.PyFileListing.PyFileInfo) HashSet(java.util.HashSet) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) IModulesManager(org.python.pydev.core.IModulesManager) IOException(java.io.IOException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) PythonPathHelper(org.python.pydev.ast.codecompletion.revisited.PythonPathHelper) AbstractModule(org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) ModulesKey(org.python.pydev.core.ModulesKey) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 3 with PythonPathHelper

use of org.python.pydev.ast.codecompletion.revisited.PythonPathHelper in project Pydev by fabioz.

the class ProjectImportedHasAstManagerTestWorkbench method testEditWithNoNature.

public void testEditWithNoNature() throws Exception {
    NullProgressMonitor monitor = new NullProgressMonitor();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IPath workspaceLoc = root.getLocation();
    IProject project = root.getProject("pydev_nature_pre_configured");
    if (project.exists()) {
        project.delete(true, monitor);
    }
    // let's create its structure
    IPath projectLoc = workspaceLoc.append("pydev_nature_pre_configured");
    projectLoc.toFile().mkdir();
    writeProjectFile(projectLoc.append(".project"));
    writePydevProjectFile(projectLoc.append(".pydevproject"));
    File srcLocFile = projectLoc.append("src").toFile();
    srcLocFile.mkdir();
    assertTrue(!project.exists());
    project.create(monitor);
    project.open(monitor);
    project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
    IJobManager jobManager = Job.getJobManager();
    jobManager.resume();
    final PythonNature nature = (PythonNature) PythonNature.addNature(project, null, null, null, null, null, null);
    assertTrue(nature != null);
    // Let's give it some time to run the jobs that restore the nature
    goToIdleLoopUntilCondition(new ICallback<Boolean, Object>() {

        @Override
        public Boolean call(Object arg) {
            if (nature != null) {
                if (nature.getAstManager() != null) {
                    return true;
                }
            }
            return false;
        }
    });
    assertTrue(nature.getAstManager() != null);
    PythonPathHelper pythonPathHelper = (PythonPathHelper) nature.getAstManager().getModulesManager().getPythonPathHelper();
    List<String> lst = new ArrayList<String>();
    lst.add(FileUtils.getFileAbsolutePath(srcLocFile));
    assertEquals(lst, pythonPathHelper.getPythonpath());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) PythonNature(org.python.pydev.plugin.nature.PythonNature) ArrayList(java.util.ArrayList) IJobManager(org.eclipse.core.runtime.jobs.IJobManager) IProject(org.eclipse.core.resources.IProject) PythonPathHelper(org.python.pydev.ast.codecompletion.revisited.PythonPathHelper) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) File(java.io.File)

Example 4 with PythonPathHelper

use of org.python.pydev.ast.codecompletion.revisited.PythonPathHelper in project Pydev by fabioz.

the class PythonModuleResolverExtensionPointTest method testGetModulesFoundStructureWithResolver.

/**
 * Checks that participants can explicitly curtail the results collected by
 * {@link PythonPathHelper#getModulesFoundStructure(IProject, IProgressMonitor)}.
 */
public void testGetModulesFoundStructureWithResolver() {
    PythonPathHelper helper = new PythonPathHelper();
    boolean isPython3Test = false;
    String path = TestDependent.getCompletePythonLib(true, isPython3Test) + "|" + TestDependent.TEST_PYSRC_TESTING_LOC;
    helper.setPythonPath(path);
    setTestingModuleResolver(new IPythonModuleResolver() {

        @Override
        public String resolveModule(IProject project, IPath moduleLocation, List<IPath> baseLocations) {
            if (moduleLocation.equals(Path.fromOSString("/root/x/y.py"))) {
                return "x.y";
            }
            return null;
        }

        @Override
        public Collection<IPath> findAllModules(IProject project, IProgressMonitor monitor) {
            List<IPath> modules = new ArrayList<>();
            modules.add(Path.fromOSString("/root/x/y.py"));
            return modules;
        }
    });
    ModulesFoundStructure modulesFoundStructure = helper.getModulesFoundStructure(null, null);
    Map<File, String> regularModules = modulesFoundStructure.regularModules;
    assertEquals(1, regularModules.keySet().size());
    assertEquals(new File("/root/x/y.py"), regularModules.keySet().iterator().next());
    assertEquals("x.y", regularModules.values().iterator().next());
}
Also used : ModulesFoundStructure(org.python.pydev.ast.codecompletion.revisited.ModulesFoundStructure) IPath(org.eclipse.core.runtime.IPath) IProject(org.eclipse.core.resources.IProject) PythonPathHelper(org.python.pydev.ast.codecompletion.revisited.PythonPathHelper) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) IPythonModuleResolver(org.python.pydev.ast.codecompletion.IPythonModuleResolver) File(java.io.File)

Example 5 with PythonPathHelper

use of org.python.pydev.ast.codecompletion.revisited.PythonPathHelper in project Pydev by fabioz.

the class InterpreterInfoBuilder method syncInfoToPythonPath.

public BuilderResult syncInfoToPythonPath(IProgressMonitor monitor, InterpreterInfo info, AbstractAdditionalDependencyInfo additionalInfo) {
    ISystemModulesManager modulesManager = info.getModulesManager();
    PythonPathHelper pythonPathHelper = (PythonPathHelper) modulesManager.getPythonPathHelper();
    if (pythonPathHelper == null) {
        // Is this even possible?
        pythonPathHelper = new PythonPathHelper();
    }
    // Just making sure it's consistent at this point.
    pythonPathHelper.setPythonPath(info.libs);
    return this.syncInfoToPythonPath(monitor, pythonPathHelper, additionalInfo, modulesManager, info);
}
Also used : ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) PythonPathHelper(org.python.pydev.ast.codecompletion.revisited.PythonPathHelper)

Aggregations

PythonPathHelper (org.python.pydev.ast.codecompletion.revisited.PythonPathHelper)10 File (java.io.File)5 ArrayList (java.util.ArrayList)4 IProject (org.eclipse.core.resources.IProject)3 IPath (org.eclipse.core.runtime.IPath)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)3 IModulesManager (org.python.pydev.core.IModulesManager)3 ModulesKey (org.python.pydev.core.ModulesKey)3 IOException (java.io.IOException)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IPythonModuleResolver (org.python.pydev.ast.codecompletion.IPythonModuleResolver)2 ModulesFoundStructure (org.python.pydev.ast.codecompletion.revisited.ModulesFoundStructure)2 PyFileInfo (org.python.pydev.ast.listing_utils.PyFileListing.PyFileInfo)2 ISystemModulesManager (org.python.pydev.core.ISystemModulesManager)2 MisconfigurationException (org.python.pydev.core.MisconfigurationException)2 AbstractAdditionalDependencyInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo)1 IBufferFiller (com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo.IBufferFiller)1