Package backend :: Package server :: Module rhnImport
[hide private]
[frames] | no frames]

Source Code for Module backend.server.rhnImport

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # 
 16   
 17  import os 
 18  import sys 
 19   
 20  from spacewalk.common.rhnLog import log_debug, log_error 
 21   
 22   
23 -class Loader:
24 # Class that saves the state of imported objects 25 _imports = {} 26
27 - def load(self, dir, interface_signature='rpcClasses'):
28 # The key we use for caching 29 root_dir = "/usr/share/rhn" 30 key = (dir, root_dir, interface_signature) 31 32 if key in self._imports: 33 return self._imports[key] 34 35 dirname = "%s/%s" % (root_dir, dir) 36 37 # We need to import things 38 if root_dir is not None and root_dir not in sys.path: 39 sys.path.append(root_dir) 40 41 fromcomps = dir.split('/') 42 _imports = {} 43 44 # Keep track of the modules we've already tried to load, to avoid loading 45 # them twice 46 modules = [] 47 # Load each module (that is not internal - i.e. doesn't start with _) 48 for module in os.listdir(dirname): 49 log_debug(5, "Attempting to load module %s from %s %s" % ( 50 module, '.'.join(fromcomps), dirname)) 51 if module[0] in ('_', '.'): 52 # We consider it 'internal' and we don't load it 53 log_debug(6, "Ignoring module %s" % module) 54 continue 55 56 # Importing files or directories with . in them is broken, so keep 57 # only the first part 58 module = module.split('.', 1)[0] 59 if module in modules: 60 log_debug(6, "Already tried to load Module %s" % (module, )) 61 continue 62 63 # Add it to the list, so we don't load it again 64 modules.append(module) 65 66 # We use fromclause to build the full module path 67 fromclause = '.'.join(fromcomps + [module]) 68 69 # Try to import the module 70 try: 71 m = __import__(fromclause, {}, {}, [module]) 72 except ImportError: 73 e = sys.exc_info()[1] 74 log_error("Error importing %s: %s" % (module, e)) 75 log_debug(6, "Details: sys.path: %s" % (sys.path, )) 76 continue 77 78 if not hasattr(m, interface_signature): 79 # The module does not support our API 80 log_error("Module %s doesn't support our API" % (module, )) 81 continue 82 log_debug(5, "Module %s loaded" % (module, )) 83 84 _imports[module] = getattr(m, interface_signature) 85 86 self._imports[key] = _imports 87 return _imports
88 89
90 -def load(dir, root_dir=None, interface_signature='rpcClasses'):
91 """ 92 Load modules (handlers) beneath the handlers/ tree. 93 94 root_dir: which directory to use as a top-level directory 95 """ 96 97 l = Loader() 98 return l.load(dir, interface_signature=interface_signature)
99