1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 __authors__ = ["Jérôme Kieffer" ]
29 __contact__ = "Jerome.Kieffer@esrf.eu"
30 __license__ = "LGPLv3+"
31 __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
32
33 import sys, os
34 from EDLogging import EDLogging
35
36
38 """
39 This class is a container for a module, it contains its name, it's path and the dict of modules
40 """
41
42 - def __init__(self, _strModuleName, _strMethodVersion=None):
43 EDLogging.__init__(self)
44 self.DEBUG("EDModule.__init__ on %s" % _strModuleName)
45 self.__name = _strModuleName
46 self.__module = None
47 self.__path = None
48 self.__dictSubModules = {}
49 self.__version = None
50 self.__strMethodVersion = _strMethodVersion
51
52
53 - def preImport(self, _strPath=None, _strMethodVersion=None):
54 """
55 Load the module in memory
56 @param _strPath: Path to the module to import
57 @param _strMethodVersion: property or method to get the version number (should return a string)
58 @return: reference to the module loaded
59 """
60 self.DEBUG("EDModule.preImport on %s from %s" % (self.__name, _strPath))
61 self.__strMethodVersion = _strMethodVersion
62 if (self.__module is None):
63 if _strPath and os.path.isdir(_strPath):
64 self.__path = _strPath
65 if _strPath in sys.path:
66 sys.path.remove(_strPath)
67 sys.path.insert(1, _strPath)
68 self.synchronizeOn()
69 dictModulesPreImport = sys.modules.copy()
70 try:
71 self.__module = __import__(self.__name)
72 self.DEBUG("preImport of %s from %s" % (self.__name, _strPath))
73 except Exception:
74 self.__module = None
75 self.ERROR("Import error while preImporting %s from %s" % (self.__name, _strPath))
76 self.writeErrorTrace()
77 self.ERROR("Revert fragments of imported stuff from %s" % (self.__name))
78
79 exc_type, exc_value, tb_root = sys.exc_info()
80 tb = tb_root
81 while tb is not None:
82 if tb.tb_frame.f_globals.get('__name__') == self.__name:
83 raise exc_type, exc_value, tb_root
84 tb = tb.tb_next
85 self.__module = None
86
87 dictModulesPostImport = sys.modules.copy()
88 for module in dictModulesPreImport:
89 if module in dictModulesPostImport:
90 dictModulesPostImport.pop(module)
91 self.__dictSubModules = dictModulesPostImport
92 self.synchronizeOff()
93
94 self.retrieveVersion(self.__strMethodVersion)
95
96 try:
97 self.__path = self.module.__file__
98 except Exception:
99 pass
100 return self.__module
101
103 """
104 Try to retrieve the version of a module using an attribute or a method ...
105 @param _strMethodVersion: property or method to get the version number (should return a string)
106 @return: Version number
107 """
108 self.__strMethodVersion = _strMethodVersion
109 if self.__strMethodVersion is None:
110 return self.__version
111
112 objVersion = self.__module
113 for submod in self.__strMethodVersion.split("."):
114 if submod in dir(objVersion):
115 objVersion = getattr(objVersion, submod)
116 else:
117 self.ERROR("EDModule.retrieveVersion: Module %s has no attr/method %s in %s." % (self.__name, self.__strMethodVersion, submod))
118 objVersion = None
119 break
120 if "__call__" in dir(objVersion):
121 try:
122 self.__version = objVersion()
123 except Exception:
124 pass
125 else:
126 try:
127 self.__version = objVersion
128 except Exception:
129 pass
130
131
132 return self.__version
133
134
136 """
137 Method that unload a module from the system memory.
138 """
139 self.DEBUG("EDModule.unImport on %s" % self.__name)
140 self.synchronizeOn()
141 if self.__module is not None:
142 for module in sys.modules.copy():
143 if module in self.__dictSubModules:
144 sys.modules.pop(module)
145 self.__dictSubModules = {}
146 self.__module = None
147 self.synchronizeOff()
148
149
152 name = property(getName)
153
155 return self.__version or ""
157 if self.__version is None:
158 self.__version = _version
159 else:
160 self.WARNING("Redefinition of version number for module %s (%s) is not allowed (%s)." % (self.name, self.__version, _version))
161 version = property(getVersion, setVersion)
162
165 module = property(getModule)
166
169 path = property(getPath)
170
172 return self.__dictSubModules
173 dictSubModules = property(getDictSubModules)
174