Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-23 02:25:46

0001 """
0002 This is a modifed version of /Demo/imputil/knee.py from python 2.6.
0003 After importing this module, the __import__ builtin is replaced by this
0004 customised version:
0005  - modules are imported normally
0006  - for all instances of 'cms.Sequcne', 'cms.Path', 'cms.EndPath' their name
0007    are added to original_sequence, original_paths, original_endpaths, respectively, 
0008    in the order in which they are defined
0009 """
0010 
0011 import sys, imp, 
0012 import builtins
0013 import re
0014 
0015 # patterns to discover cms.Path and cms.EndPath definitions in imported files
0016 pattern_path     = re.compile(r'(\w+)\s*=\s*cms\.Path')
0017 pattern_endpath  = re.compile(r'(\w+)\s*=\s*cms\.EndPath')
0018 pattern_sequence = re.compile(r'(\w+)\s*=\s*cms\.Sequence')
0019 
0020 # keep track of the original order of Paths and EndPaths
0021 original_paths     = []
0022 original_endpaths  = []
0023 original_sequences = []
0024 
0025 # replacement for __import__() as in Python 2.4 - the "level" parameter is not used
0026 def import_hook(name, globals=None, locals=None, fromlist=None, level=-1):
0027     parent = determine_parent(globals)
0028     q, tail = find_head_package(parent, name)
0029     m = load_tail(q, tail)
0030     if not fromlist:
0031         return q
0032     if hasattr(m, "__path__"):
0033         ensure_fromlist(m, fromlist)
0034     return m
0035 
0036 def determine_parent(globals):
0037     if not globals or "__name__" not in globals:
0038         return None
0039     pname = globals['__name__']
0040     if "__path__" in globals:
0041         parent = sys.modules[pname]
0042         assert globals is parent.__dict__
0043         return parent
0044     if '.' in pname:
0045         i = pname.rfind('.')
0046         pname = pname[:i]
0047         parent = sys.modules[pname]
0048         assert parent.__name__ == pname
0049         return parent
0050     return None
0051 
0052 def find_head_package(parent, name):
0053     if '.' in name:
0054         i = name.find('.')
0055         head = name[:i]
0056         tail = name[i+1:]
0057     else:
0058         head = name
0059         tail = ""
0060     if parent:
0061         qname = "%s.%s" % (parent.__name__, head)
0062     else:
0063         qname = head
0064     q = import_module(head, qname, parent)
0065     if q: return q, tail
0066     if parent:
0067         qname = head
0068         parent = None
0069         q = import_module(head, qname, parent)
0070         if q: return q, tail
0071     raise ImportError("No module named " + qname)
0072 
0073 def load_tail(q, tail):
0074     m = q
0075     while tail:
0076         i = tail.find('.')
0077         if i < 0: i = len(tail)
0078         head, tail = tail[:i], tail[i+1:]
0079         mname = "%s.%s" % (m.__name__, head)
0080         m = import_module(head, mname, m)
0081         if not m:
0082             raise ImportError("No module named " + mname)
0083     return m
0084 
0085 def ensure_fromlist(m, fromlist, recursive=0):
0086     for sub in fromlist:
0087         if sub == "*":
0088             if not recursive:
0089                 try:
0090                     all = m.__sorted__
0091                 except AttributeError:
0092                     pass
0093                 else:
0094                     ensure_fromlist(m, all, 1)
0095             continue
0096         if sub != "*" and not hasattr(m, sub):
0097             subname = "%s.%s" % (m.__name__, sub)
0098             submod = import_module(sub, subname, m)
0099             if not submod:
0100                 raise ImportError("No module named " + subname)
0101 
0102 def import_module(partname, fqname, parent):
0103     try:
0104         return sys.modules[fqname]
0105     except KeyError:
0106         pass
0107     try:
0108         fp, pathname, stuff = imp.find_module(partname,
0109                                               parent and parent.__path__)
0110     except ImportError:
0111         return None
0112     try:
0113         if fp: 
0114             rewind = fp.tell()
0115         m = imp.load_module(fqname, fp, pathname, stuff)
0116         if fp:
0117             fp.seek(rewind)
0118             (suffix, mode, type) = stuff
0119             if type == imp.PY_SOURCE:
0120                 source = fp.read()
0121                 for item in pattern_sequence.findall(source):
0122                     if item not in original_sequences: original_sequences.append( item )
0123                 for item in pattern_path.findall(source):
0124                     if item not in original_paths: original_paths.append( item )
0125                 for item in pattern_endpath.findall(source):
0126                     if item not in original_endpaths: original_endpaths.append( item )
0127             elif type == imp.PY_COMPILED:
0128                 # can we do something about "compiled" python modules ?
0129                 pass
0130     finally:
0131         if fp: fp.close()
0132     if parent:
0133         setattr(parent, partname, m)
0134     return m
0135 
0136 
0137 # Replacement for reload()
0138 def reload_hook(module):
0139     name = module.__name__
0140     if '.' not in name:
0141         return import_module(name, name, None)
0142     i = name.rfind('.')
0143     pname = name[:i]
0144     parent = sys.modules[pname]
0145     return import_module(name[i+1:], name, parent)
0146 
0147 
0148 # Save the original hooks
0149 original_import = builtins.__import__
0150 original_reload = builtins.reload
0151 
0152 # Now install our hooks
0153 builtins.__import__ = import_hook
0154 builtins.reload = reload_hook