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);
}
}
}
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()]));
}
}
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);
}
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);
}
}
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;
}
Aggregations