| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Namespace List
- Namespace Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Functions | Variables
genconfuser Namespace Reference

## Functions
def | _inheritsfrom
def | loadConfigurableDb
def | getConfigurableUsers
def | main

## Variables
tuple | retcode main()

## Function Documentation

def genconfuser._inheritsfrom | ( |  | derived,
|  |  | basename
| ) |  |
| private

    Check if the class name 'basename' is anywhere in the base classes of the
    class 'derived'.
    If 'derived' _is_ 'basename', returns False.

Definition at line 21 of file genconfuser.py.

21
22 def _inheritsfrom(derived, basename):
23 """
24 Check if the class name 'basename' is anywhere in the base classes of the
25 class 'derived'.
26 If 'derived' _is_ 'basename', returns False.
27 """
28 for b in derived.__bases__:
29 if b.__name__ == basename:
30 return True
31 else:
32 if _inheritsfrom(b, basename):
33 return True
34 return False

def genconfuser.getConfigurableUsers | ( |  | modulename,
|  |  | root,
|  |  | mayNotExist = `False`
| ) |  |

    Find in the module 'modulename' all the classes that derive from ConfigurableUser.
    Return the list of the names.
    The flag mayNotExist is used to choose the level of the logging message in case
    the requested module does not exist.

Definition at line 54 of file genconfuser.py.

54
55 def getConfigurableUsers(modulename, root, mayNotExist = False):
56 """
57 Find in the module 'modulename' all the classes that derive from ConfigurableUser.
58 Return the list of the names.
59 The flag mayNotExist is used to choose the level of the logging message in case
60 the requested module does not exist.
61 """
62 # remember the old system path
63 oldpath = list(sys.path)
64 # we need to hack the sys.path to add the first part of the module name after root
65 moduleelements = modulename.split('.')
66 if len(moduleelements) > 1:
67 moddir = os.sep.join([root] + moduleelements[:-1])
68 else:
69 moddir = root
70 # this is the name of the submodule to import
71 shortmodname = moduleelements[-1]
72 # check if the module file actually exists
73 if not os.path.exists(os.path.join(moddir, shortmodname) + ".py"):
74 msg = "Module %s does not exist" % modulename
75 if mayNotExist:
76 logging.verbose(msg)
77 else:
78 logging.error(msg)
79 # no file -> do not try to import
80 return []
81 # prepend moddir to the path
82 sys.path.insert(0, moddir)
83 logging.verbose("sys.path prepended with %r", sys.path[0])
84
85 logging.info("Looking for ConfigurableUser in %r", modulename)
86 g, l = {}, {}
87 try:
88 logging.verbose("importing %s", shortmodname)
89 exec "import %s as mod" % shortmodname in g, l
90 finally:
91 # restore old sys.path
92 logging.verbose("restoring old sys.path")
93 sys.path = oldpath
94 mod = l["mod"]
95 if "__all__" in dir(mod) and mod.__all__:
96 all = mod.__all__
97 else:
98 all = [ n for n in dir(mod) if not n.startswith("_")]
99 result = []
100 for name in all:
101 cfg = cfgDb.get(name)
102 if cfg and cfg["module"] != modulename:
103 # This name comes from another module
104 logging.verbose("Object %r already found in module %r", name, cfg["module"])
105 continue
106 t = getattr(mod, name)
107 if isinstance(t, type) and _inheritsfrom(t, "ConfigurableUser"):
108 result.append(name)
109 logging.verbose("Found %r", result)
110 return result

def genconfuser.loadConfigurableDb | ( |  | ) |

    Equivalent to GaudiKernel.ConfigurableDb.loadConfigurableDb(), but does a
    deep search and executes the '*_confDb.py' files instead of importing them.

Definition at line 35 of file genconfuser.py.

35
36 def loadConfigurableDb():
37 '''
38 Equivalent to GaudiKernel.ConfigurableDb.loadConfigurableDb(), but does a
39 deep search and executes the '*_confDb.py' files instead of importing them.
40 '''
41 # find the '*_confDb.py' files that are not merged ones
42 for p in sys.path:
43 for f in [f for f in glob(os.path.join(p, '*', '*_confDb.py'))
44 if 'merged' not in f and os.path.isfile(f)]:
45 logging.verbose('Loading %s', f)
46 try:
47 execfile(f, {}, {})
48 except:
49 # It may happen that the file is found but not completely
50 # written, usually during parallel builds, but we do not care.
51 pass
52 # top up with the regular merged confDb (for the used projects)
53 GaudiKernel.ConfigurableDb.loadConfigurableDb()

def genconfuser.main | ( |  | ) |

Definition at line 111 of file genconfuser.py.

111
112 def main():
113 from optparse import OptionParser
114 parser = OptionParser(prog = os.path.basename(sys.argv[0]),
115 usage = "%prog [options] <PackageName> [<Module1> ...]")
116 parser.add_option("-o", "--output", action="store", type="string",
117 help="output file for confDb data [default = '../genConf/<PackageName>_user_confDb.py'].")
118 parser.add_option("-r", "--root", action="store", type="string",
119 help="root directory of the python modules [default = '../python'].")
120 parser.add_option("-v", "--verbose", action="store_true",
121 help="print some debugging information")
122 parser.add_option("--debug", action="store_true",
123 help="print more debugging information")
124 parser.add_option("--lockerpath", action="store",
125 metavar = "DIRNAME",
126 help="directory where to find the module 'locker'")
127 parser.set_defaults(root = os.path.join("..","python"))
128
129 opts, args = parser.parse_args()
130
131 if opts.debug:
132 log_level = logging.DEBUG
133 elif opts.verbose:
134 log_level = logging.VERBOSE
135 else:
136 log_level = logging.INFO
137 logging.basicConfig(format = "%(levelname)s: %(message)s",
138 stream = sys.stdout,
139 level = log_level)
140
141 if len(args) < 1:
142 parser.error("PackageName is required")
143
144 package_name = args.pop(0)
145
146 usingConvention = False
147 if not args:
148 # use the conventional module name <package>.Configuration
149 args = [package_name + ".Configuration"]
150 usingConvention = True
151
152 genConfDir = os.path.join("..", os.environ.get("CMTCONFIG", ""), "genConf")
153 if not os.path.exists(genConfDir):
154 genConfDir = os.path.join("..", "genConf")
155
156 if not opts.output:
157 outputfile = os.path.join(genConfDir, package_name + '_user_confDb.py')
158 else:
159 outputfile = opts.output
160
161
162 # The locking ensures that nobody tries to modify the python.zip file while
163 # we read it.
164 dbLock = None
165 if "GAUDI_BUILD_LOCK" in os.environ:
166 if opts.lockerpath:
167 sys.path.append(opts.lockerpath)
168 # Get the LockFile class from the locker module in GaudiPolicy or use a fake
169 # factory.
170 try:
171 from locker import LockFile
172 except ImportError:
173 def LockFile(*args, **kwargs):
174 return None
175 # obtain the lock
176 dbLock = LockFile(os.environ["GAUDI_BUILD_LOCK"], temporary = True)
177
178 # We can disable the error on missing configurables only if we can import Gaudi.Configurables
179 # It must be done at this point because it may conflict with logging.basicConfig
180 try:
181 import Gaudi.Configurables
182 Gaudi.Configurables.ignoreMissingConfigurables = True
183 except:
184 pass
185 # load configurables database to avoid fake duplicates
186 loadConfigurableDb()
187 # ensure that local configurables are in the database
188 try:
189 # Add the local python directories to the python path to be able to import the local
190 # configurables
191 sys.path.insert(0, genConfDir)
192 sys.path.insert(0, os.path.join("..", "python"))
193 localConfDb = os.path.join(genConfDir, package_name, package_name + '_confDb.py')
194 if os.path.exists(localConfDb):
195 execfile(localConfDb, {}, {})
196 # Extend the search path of the package module to find the configurables
197 package_module = __import__(package_name)
198 package_module.__path__.insert(0, os.path.join(genConfDir, package_name))
199 except:
200 pass # ignore failures (not important)
201 del dbLock # Now we can let the others operate on the install area python directory
202
203 # Collecting ConfigurableUser specializations
204 cus = {}
205 for mod in args:
206 lst = None
207 try:
208 lst = getConfigurableUsers(mod, root = opts.root, mayNotExist = usingConvention)
209 except ImportError:
210 import traceback
211 logging.error("Cannot import module %r:\n%s", mod,
212 traceback.format_exc().rstrip()) # I remove the trailing '\n'
213 return 2
214 if lst:
215 cus[mod] = lst
216 # Add the configurables to the database as fake entries to avoid duplicates
217 for m in lst:
218 cfgDb.add(configurable = m,
219 package = 'None',
220 module = 'None',
221 lib = 'None')
222 elif not usingConvention:
223 logging.warning("Specified module %r does not contain ConfigurableUser specializations", mod)
224
225 if cus:
226 logging.info("ConfigurableUser found:\n%s", pformat(cus))
227 # header
228 output = """## -*- python -*-
229 # db file automatically generated by %s on: %s
230 ## insulates outside world against anything bad that could happen
231 ## also prevents global scope pollution
232 def _fillCfgDb():
233 from GaudiKernel.Proxy.ConfigurableDb import CfgDb
234
235 # get a handle on the repository of Configurables
236 cfgDb = CfgDb()
237
238 # populate the repository with informations on Configurables
239 """ % (parser.prog, time.asctime())
240
241 for mod in cus:
242 for cu in cus[mod]:
243 output += """
244 cfgDb.add( configurable = '%s',
245 package = '%s',
246 module = '%s',
247 lib = 'None' )""" % (cu, package_name, mod)
248
249 # trailer
250 output += """
251
252 return #_fillCfgDb
253
254 # fill cfgDb at module import...
255 try:
256 _fillCfgDb()
257 #house cleaning...
258 del _fillCfgDb
259 except Exception,err:
260 print "Py:ConfigurableDb ERROR Problem with [%%s] content!" %% __name__
261 print "Py:ConfigurableDb ERROR",err
262 print "Py:ConfigurableDb ERROR ==> culprit is package [%s] !"
263 """ % package_name
264 elif usingConvention:
265 logging.info("No ConfigurableUser found")
266 output = ("# db file automatically generated by %s on: %s\n"
267 "# No ConfigurableUser specialization in %s\n") % (parser.prog, time.asctime(), package_name)
268 else:
269 logging.error("No ConfigurableUser specialization found")
270 return 1
271
272 # create the destination directory if not there
273 output_dir = os.path.dirname(outputfile)
274 if not os.path.exists(output_dir):
275 logging.info("Creating directory %r", output_dir)
276 os.makedirs(output_dir, 0755)
277
278 # write output to file
279 logging.verbose("Writing confDb data to %r", outputfile)
280 open(outputfile, "w").write(output)
281 return 0

## Variable Documentation

tuple genconfuser.retcode main()

Definition at line 283 of file genconfuser.py.