Salome HOME
1b206fba6f97b3e59618101a058ee5e4515d9788
[modules/shaper.git] / src / Config / Config_ModuleReader.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * Config_ModuleReader.cpp
5  *
6  *  Created on: Mar 20, 2014
7  *      Author: sbh
8  */
9
10 #include <pyconfig.h>
11
12 #include <Config_Keywords.h>
13 #include <Config_Common.h>
14 #include <Config_ModuleReader.h>
15 #include <Config_FeatureReader.h>
16 #include <Events_InfoMessage.h>
17
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20
21 // Have to be included before std headers
22 #include <Python.h>
23
24 //Necessary for cerr
25 #include <iostream>
26 #include <algorithm>
27
28 #ifdef WIN32
29 #include <windows.h>
30 #pragma warning(disable : 4996) // for getenv
31 #else
32 #include <dlfcn.h>
33 #endif
34
35 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
36 std::set<std::string> Config_ModuleReader::myDependencyModules;
37
38 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
39     : Config_XMLReader(PLUGIN_FILE),
40       myEventGenerated(theEventGenerated)
41 {
42 }
43
44 Config_ModuleReader::~Config_ModuleReader()
45 {
46 }
47
48 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
49 {
50   return myFeaturesInFiles;
51 }
52
53 const std::set<std::string>& Config_ModuleReader::modulePluginFiles() const
54 {
55   return myPluginFiles;
56 }
57
58 /*!
59  * Get module name from plugins.xml
60  * (property "module")
61  */
62 std::string Config_ModuleReader::getModuleName()
63 {
64   xmlNodePtr aRoot = findRoot();
65   return getProperty(aRoot, PLUGINS_MODULE);
66 }
67
68
69 void Config_ModuleReader::addFeature(const std::string& theFeatureName,
70                                      const std::string& thePluginConfig)
71 {
72   if (myFeaturesInFiles.count(theFeatureName)) {
73     std::string anErrorMsg = "Can not register feature '%1' in plugin '%2'."
74       " There is a feature with the same ID.";
75     Events_InfoMessage("Config_ModuleReader", anErrorMsg)
76       .arg(theFeatureName).arg(thePluginConfig).send();
77     return;
78   }
79
80   myFeaturesInFiles[theFeatureName] = thePluginConfig;
81 }
82
83 void Config_ModuleReader::processNode(xmlNodePtr theNode)
84 {
85   if (isNode(theNode, NODE_PLUGIN, NULL)) {
86     if (!hasRequiredModules(theNode))
87       return;
88     std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
89     if (!aPluginConf.empty())
90       myPluginFiles.insert(aPluginConf);
91     std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
92     std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
93     std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
94
95     std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
96     std::list<std::string>::iterator it = aFeatures.begin();
97     for (; it != aFeatures.end(); it++) {
98       addFeature(*it, aPluginConf);
99     }
100   }
101 }
102
103 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
104 {
105   return isNode(theNode, NODE_PLUGINS, NULL);
106 }
107
108 bool Config_ModuleReader::hasRequiredModules(xmlNodePtr theNode) const
109 {
110   std::string aRequiredModule = normalize(getProperty(theNode, PLUGIN_DEPENDENCY));
111   if(aRequiredModule.empty())
112     return true;
113   std::set<std::string>::iterator it = myDependencyModules.begin();
114   for ( ; it != myDependencyModules.end(); it++ ) {
115     if (*it == aRequiredModule) return true;
116   }
117   return false;
118 }
119
120 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
121                                                          const std::string& thePluginXmlConf)
122 {
123   if (thePluginXmlConf.empty()) {  //probably a third party library
124     loadLibrary(thePluginLibrary);
125     return std::list<std::string>();
126   }
127
128   Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
129                                                       thePluginLibrary,
130                                                       myEventGenerated);
131   aReader.readAll();
132   return aReader.features();
133 }
134
135 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
136                                            const std::string& aPluginScript,
137                                            const std::string& aPluginConf)
138 {
139   PluginType aType = Config_ModuleReader::Binary;
140   std::string aPluginName;
141   if (!aPluginLibrary.empty()) {
142     aPluginName = aPluginLibrary;
143     if (aPluginConf.empty()) {
144       aType = Config_ModuleReader::Intrenal;
145     }
146   } else if (!aPluginScript.empty()) {
147     aPluginName = aPluginScript;
148     aType = Config_ModuleReader::Python;
149   }
150   if(!aPluginName.empty()) {
151     myPluginTypes[aPluginName] = aType;
152   }
153   addDependencyModule(aPluginName);
154   return aPluginName;
155 }
156
157 void Config_ModuleReader::loadPlugin(const std::string& thePluginName)
158 {
159   PluginType aType = Config_ModuleReader::Binary;
160   if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
161     aType = myPluginTypes.at(thePluginName);
162   }
163   switch (aType) {
164     case Config_ModuleReader::Python:
165       loadScript(thePluginName);
166       break;
167     case Config_ModuleReader::Binary:
168     case Config_ModuleReader::Intrenal:
169     default:
170       loadLibrary(thePluginName);
171       break;
172   }
173 }
174
175 void Config_ModuleReader::loadScript(const std::string& theFileName)
176 {
177   /* acquire python thread */
178   PyGILState_STATE gstate = PyGILState_Ensure();
179
180   PyObject* module = PyImport_ImportModule(theFileName.c_str());
181   if (!module) {
182     std::string anErrorMsg = "An error occurred while importing " + theFileName;
183     //Get detailed error message:
184     if (PyErr_Occurred()) {
185       PyObject *pstr, *ptype, *pvalue, *ptraceback;
186       PyErr_Fetch(&ptype, &pvalue, &ptraceback);
187       PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
188       pstr = PyObject_Str(pvalue);
189       std::string aPyError = std::string(PyString_AsString(pstr));
190       if (!aPyError.empty()) {
191         anErrorMsg += ":\n" + aPyError;
192       }
193       Py_XDECREF(pstr);
194       Py_XDECREF(ptype);
195       Py_XDECREF(pvalue);
196       Py_XDECREF(ptraceback);
197     }
198     Events_InfoMessage("Config_ModuleReader", anErrorMsg).send();
199   }
200
201   /* release python thread */
202   PyGILState_Release(gstate);
203 }
204
205 void Config_ModuleReader::loadLibrary(const std::string& theLibName)
206 {
207   std::string aFileName = library(theLibName);
208   if (aFileName.empty())
209     return;
210
211   #ifdef WIN32
212   HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
213   #else
214   void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
215   #endif
216   if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
217     std::string anErrorMsg = "Failed to load " + aFileName;
218     #ifdef WIN32
219     DWORD   dwLastError = ::GetLastError();
220     LPSTR messageBuffer = NULL;
221     size_t size = ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
222                                  FORMAT_MESSAGE_FROM_SYSTEM | 
223                                  FORMAT_MESSAGE_IGNORE_INSERTS,
224                                  NULL, 
225                                  dwLastError, 
226                                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
227                                  (LPSTR)&messageBuffer, 0, NULL);
228     anErrorMsg += ": " +  std::string(messageBuffer, size);
229     #else
230     anErrorMsg += ": " + std::string(dlerror());
231     #endif
232     std::cerr << anErrorMsg << std::endl;
233     Events_InfoMessage("Config_ModuleReader", anErrorMsg).send();
234   }
235 }
236
237 void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
238 {
239   myDependencyModules.insert(normalize(theModuleName));
240 }
241