Search in sources :

Example 1 with SyncSystemModulesManagerScheduler

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

use of org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManagerScheduler 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)

Aggregations

SyncSystemModulesManagerScheduler (org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManagerScheduler)2 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 ZipEntry (java.util.zip.ZipEntry)1 BackingStoreException (org.osgi.service.prefs.BackingStoreException)1 DefaultSyncSystemModulesManagerScheduler (org.python.pydev.ast.codecompletion.revisited.DefaultSyncSystemModulesManagerScheduler)1 PythonPathHelper (org.python.pydev.ast.codecompletion.revisited.PythonPathHelper)1 IInfoTrackerListener (org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManagerScheduler.IInfoTrackerListener)1 InfoTracker (org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManagerScheduler.InfoTracker)1 IInterpreterManager (org.python.pydev.core.IInterpreterManager)1 IInterpreterManagerListener (org.python.pydev.core.IInterpreterManagerListener)1 ISystemModulesManager (org.python.pydev.core.ISystemModulesManager)1 ShellId (org.python.pydev.core.ShellId)1