All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 22 of file genconfuser.py.

22 
23 def _inheritsfrom(derived, basename):
24  """
25  Check if the class name 'basename' is anywhere in the base classes of the
26  class 'derived'.
27  If 'derived' _is_ 'basename', returns False.
28  """
29  for b in derived.__bases__:
30  if b.__name__ == basename:
31  return True
32  else:
33  if _inheritsfrom(b, basename):
34  return True
35  return False
def _inheritsfrom
Definition: genconfuser.py:22
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 67 of file genconfuser.py.

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

Definition at line 36 of file genconfuser.py.

36 
37 def loadConfigurableDb():
38  '''
39  Equivalent to GaudiKernel.ConfigurableDb.loadConfigurableDb(), but does a
40  deep search and executes the '*.confdb' files instead of importing them.
41  '''
42  log = GaudiKernel.ConfigurableDb.log
43  from os.path import join as path_join
44  # look for the confdb files in all the reasonable places
45  # - CMake builds
46  confDbFiles = []
47  for path in sys.path:
48  confDbFiles += [f for f in glob(path_join(path, '*', '*.confdb'))
49  if os.path.isfile(f)]
50  # - used projects and local merged file
51  pathlist = os.getenv("LD_LIBRARY_PATH", "").split(os.pathsep)
52  for path in filter(os.path.isdir, pathlist):
53  confDbFiles += [f for f in [path_join(path, f) for f in os.listdir(path)
54  if f.endswith('.confdb')]]
55  # - load the confdb files
56  for confDb in confDbFiles:
57  log.debug( "\t-loading [%s]..." % confDb )
58  try:
59  cfgDb._loadModule( confDb )
60  except Exception, err:
61  # It may happen that the file is found but not completely
62  # written, usually during parallel builds, but we do not care.
63  log.warning( "Could not load file [%s] !", confDb )
64  log.warning( "Reason: %s", err )
65  # top up with the regular merged confDb (for the used projects)
def loadConfigurableDb
Definition: genconfuser.py:36
def loadConfigurableDb
Helper function to load all ConfigurableDb files holding informations.
def genconfuser.main ( )

Definition at line 124 of file genconfuser.py.

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

Variable Documentation

tuple genconfuser.retcode = main()

Definition at line 277 of file genconfuser.py.