1 // Copyright (C) 2014-2023 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <Config_Keywords.h>
23 #include <Config_Common.h>
24 #include <Config_ModuleReader.h>
25 #include <Config_FeatureReader.h>
26 #include <Config_PluginMessage.h>
27 #include <Events_InfoMessage.h>
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
32 // Have to be included before std headers
34 #include <structmember.h>
42 #pragma warning(disable : 4996) // for getenv
47 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
48 std::set<std::string> Config_ModuleReader::myDependencyModules;
50 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
51 : Config_XMLReader(PLUGIN_FILE),
52 myEventGenerated(theEventGenerated)
56 Config_ModuleReader::~Config_ModuleReader()
60 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
62 return myFeaturesInFiles;
65 const std::map<std::string, std::string>& Config_ModuleReader::proprietaryFeatures() const
67 return myProprietaryFeatures;
70 const std::set<std::string>& Config_ModuleReader::proprietaryPlugins() const
72 return myProprietaryPlugins;
75 const std::set<std::string>& Config_ModuleReader::modulePluginFiles() const
81 * Get module name from plugins.xml
84 std::string Config_ModuleReader::getModuleName()
86 xmlNodePtr aRoot = findRoot();
87 return getProperty(aRoot, PLUGINS_MODULE);
91 void Config_ModuleReader::addFeature(const std::string& theFeatureName,
92 const std::string& thePluginConfig)
94 if (myProprietaryFeatures.count(theFeatureName)) {
95 myProprietaryFeatures.erase(theFeatureName);
98 if (myFeaturesInFiles.count(theFeatureName)) {
99 std::string anErrorMsg = "Can not register feature '%1' in plugin '%2'."
100 " There is a feature with the same ID.";
101 Events_InfoMessage("Config_ModuleReader", anErrorMsg)
102 .arg(theFeatureName).arg(thePluginConfig).send();
106 myFeaturesInFiles[theFeatureName] = thePluginConfig;
109 void Config_ModuleReader::addFeatureRequireLicense(const std::string& theFeatureName,
110 const std::string& thePluginConfig)
112 if (myFeaturesInFiles.count(theFeatureName) ||
113 myProprietaryFeatures.count(theFeatureName)) {
114 std::string anErrorMsg = "Can not register feature '%1' in plugin '%2'."
115 " There is a feature with the same ID.";
116 Events_InfoMessage("Config_ModuleReader", anErrorMsg)
117 .arg(theFeatureName).arg(thePluginConfig).send();
121 myProprietaryFeatures[theFeatureName] = thePluginConfig;
124 void Config_ModuleReader::processNode(xmlNodePtr theNode)
126 if (isNode(theNode, NODE_PLUGIN, NULL)) {
127 if (!hasRequiredModules(theNode))
129 std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
130 if (!aPluginConf.empty())
131 myPluginFiles.insert(aPluginConf);
132 std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
133 std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
134 std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
135 std::string aPluginDocSection = getProperty(theNode, PLUGIN_DOCSECTION);
136 std::string aUsesPlugin = getProperty(theNode, PLUGIN_USES);
137 if (!aUsesPlugin.empty()) { // send information about the plugin dependencies
138 std::shared_ptr<Config_PluginMessage> aMess(new Config_PluginMessage(
139 Events_Loop::loop()->eventByName(Config_PluginMessage::EVENT_ID()), aPluginName));
140 aMess->setUses(aUsesPlugin);
141 Events_Loop::loop()->send(aMess);
144 std::string aLicense = getProperty(theNode, PLUGIN_LICENSE);
145 std::transform(aLicense.begin(), aLicense.end(), aLicense.begin(), ::tolower);
146 bool isLicensed = aLicense == "true";
148 myProprietaryPlugins.insert(aPluginName);
150 std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf, aPluginDocSection);
151 std::list<std::string>::iterator it = aFeatures.begin();
152 for (; it != aFeatures.end(); it++) {
154 addFeatureRequireLicense(*it, aPluginConf);
156 addFeature(*it, aPluginConf);
161 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
163 return isNode(theNode, NODE_PLUGINS, NULL);
166 bool Config_ModuleReader::hasRequiredModules(xmlNodePtr theNode) const
168 std::string aRequiredModule = normalize(getProperty(theNode, PLUGIN_DEPENDENCY));
169 if(aRequiredModule.empty())
171 std::set<std::string>::iterator it = myDependencyModules.begin();
172 for ( ; it != myDependencyModules.end(); it++ ) {
173 if (*it == aRequiredModule) return true;
178 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
179 const std::string& thePluginXmlConf,
180 const std::string& thePluginDocSection)
182 if (thePluginXmlConf.empty()) { //probably a third party library
183 loadLibrary(thePluginLibrary);
184 return std::list<std::string>();
187 Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
192 return aReader.features();
195 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
196 const std::string& aPluginScript,
197 const std::string& aPluginConf)
199 PluginType aType = Config_ModuleReader::Binary;
200 std::string aPluginName;
201 if (!aPluginLibrary.empty()) {
202 aPluginName = aPluginLibrary;
203 if (aPluginConf.empty()) {
204 aType = Config_ModuleReader::Intrenal;
206 } else if (!aPluginScript.empty()) {
207 aPluginName = aPluginScript;
208 aType = Config_ModuleReader::Python;
210 if(!aPluginName.empty()) {
211 myPluginTypes[aPluginName] = aType;
213 addDependencyModule(aPluginName);
217 void Config_ModuleReader::loadPlugin(const std::string& thePluginName)
219 // informs model that plugin loading is started
220 static const Events_ID kEVENT_ID =
221 Events_Loop::loop()->eventByName(Config_PluginMessage::EVENT_ID());
222 std::shared_ptr<Config_PluginMessage> aMess(new Config_PluginMessage(kEVENT_ID, thePluginName));
223 Events_Loop::loop()->send(aMess);
225 PluginType aType = Config_ModuleReader::Binary;
226 if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
227 aType = myPluginTypes.at(thePluginName);
230 case Config_ModuleReader::Python:
231 loadScript(thePluginName);
233 case Config_ModuleReader::Binary:
234 case Config_ModuleReader::Intrenal:
236 loadLibrary(thePluginName);
243 // Handle Python exception
244 // Reuse code from KERNEL module
253 PyStdOut_dealloc(PyStdOut *self)
259 PyStdOut_write(PyStdOut* self, PyObject* args)
262 if (!PyArg_ParseTuple(args, "s", &c))
265 *(self->out) = *(self->out) + c;
271 static PyMethodDef PyStdOut_methods[] =
273 {"write", (PyCFunction)PyStdOut_write, METH_VARARGS,
274 PyDoc_STR("write(string) -> None")},
275 {0, 0, 0, 0} /* sentinel */
278 static PyMemberDef PyStdOut_memberlist[] =
280 {(char*)"softspace", T_INT, offsetof(PyStdOut, softspace), 0,
281 (char*)"flag indicating that a space needs to be printed; used by print"},
282 {0, 0, 0, 0, 0} /* sentinel */
285 static PyTypeObject PyStdOut_Type =
287 /* The ob_type field must be initialized in the module init function
288 * to be portable to Windows without using C++. */
289 PyVarObject_HEAD_INIT(NULL, 0)
292 sizeof(PyStdOut), /*tp_basicsize*/
295 (destructor)PyStdOut_dealloc, /*tp_dealloc*/
302 0, /*tp_as_sequence*/
307 PyObject_GenericGetAttr, /*tp_getattro*/
308 /* softspace is writable: we must supply tp_setattro */
309 PyObject_GenericSetAttr, /* tp_setattro */
311 Py_TPFLAGS_DEFAULT, /*tp_flags*/
315 0, /*tp_richcompare*/
316 0, /*tp_weaklistoffset*/
319 PyStdOut_methods, /*tp_methods*/
320 PyStdOut_memberlist, /*tp_members*/
338 0, /*tp_version_tag*/
342 PyObject* newPyStdOut(std::string& out)
344 PyStdOut* self = PyObject_New(PyStdOut, &PyStdOut_Type);
349 return (PyObject*)self;
352 std::string parseException()
355 if (PyErr_Occurred())
357 PyObject* new_stderr = newPyStdOut(error);
358 PyObject* old_stderr = PySys_GetObject((char*)"stderr");
359 Py_INCREF(old_stderr);
360 PySys_SetObject((char*)"stderr", new_stderr);
362 PySys_SetObject((char*)"stderr", old_stderr);
363 Py_DECREF(new_stderr);
369 void Config_ModuleReader::loadScript(const std::string& theFileName, bool theSendErr)
371 /* acquire python thread */
372 PyGILState_STATE gstate = PyGILState_Ensure();
374 PyObject* module = PyImport_ImportModule(theFileName.c_str());
376 std::string anErrorMsg = "An error occurred while importing " + theFileName;
377 // Get detailed error message (including traceback)
378 if (PyErr_Occurred()) {
379 std::string aPyError = parseException();
380 if (!aPyError.empty())
381 anErrorMsg += ":\n" + aPyError;
384 Events_InfoMessage("Config_ModuleReader", anErrorMsg).send();
387 /* release python thread */
388 PyGILState_Release(gstate);
391 void Config_ModuleReader::loadLibrary(const std::string& theLibName)
393 std::string aFileName = library(theLibName);
394 if (aFileName.empty())
398 HINSTANCE aModLib = ::LoadLibraryA(aFileName.c_str());
400 void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
402 if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
404 std::string anErrorMsg = "Failed to load " + aFileName;
406 DWORD dwLastError = ::GetLastError();
407 LPSTR messageBuffer = NULL;
408 size_t size = ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
409 FORMAT_MESSAGE_FROM_SYSTEM |
410 FORMAT_MESSAGE_IGNORE_INSERTS,
413 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
414 (LPSTR)&messageBuffer, 0, NULL);
415 anErrorMsg += ": " + std::string(messageBuffer, size);
417 anErrorMsg += ": " + std::string(dlerror());
419 std::cerr << anErrorMsg << std::endl;
420 Events_InfoMessage("Config_ModuleReader", anErrorMsg).send();
425 void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
427 myDependencyModules.insert(normalize(theModuleName));