def _loadOnePlugin(self, plugin): if plugin.startswith('.'): return if not (plugin.endswith('.py') or plugin.endswith('.pyc')): return if plugin.endswith('.pyc'): realname = plugin[:-4] else: realname = plugin[:-3] try: mod = imp.find_module(realname) except: return try: loaded = imp.load_module(realname, mod[0], mod[1], mod[2]) klass = loaded.AMIConfigPlugin if not klass.__dict__.has_key('name'): return return klass except: return self._info('loading plugin %s' % plugin)
def returnClassForVersion(version=DEVEL): """Return the class of the syntax handler for version. version can be either a string or the matching constant. Raises KickstartValueError if version does not match anything. """ try: version = int(version) module = "%s" % versionToString(version, skipDevel=True) except ValueError: module = "%s" % version version = stringToVersion(version) module = module.lower() try: import pykickstart.handlers sys.path.extend(pykickstart.handlers.__path__) found = imp.find_module(module) loaded = imp.load_module(module, found[0], found[1], found[2]) for (k, v) in list(loaded.__dict__.items()): if k.lower().endswith("%shandler" % module): return v except: found = None raise KickstartVersionError( _("Unsupported version specified: %s") % version) finally: # Closing opened files in imp.load_module if found and len(found) > 0: found[0].close()
def returnClassForVersion(version=DEVEL): """Return the class of the syntax handler for version. version can be either a string or the matching constant. Raises KickstartValueError if version does not match anything. """ try: version = int(version) module = "%s" % versionToString(version, skipDevel=True) except ValueError: module = "%s" % version version = stringToVersion(version) module = module.lower() try: import pykickstart.handlers sys.path.extend(pykickstart.handlers.__path__) found = imp.find_module(module) loaded = imp.load_module(module, found[0], found[1], found[2]) for (k, v) in loaded.__dict__.items(): if k.lower().endswith("%shandler" % module): return v except: raise KickstartVersionError(_("Unsupported version specified: %s") % version) finally: if found[0]: found[0].close()
def returnClassForVersion(version=DEVEL): # type: (Union[int, str]) -> Callable[[], BaseHandler] """Return the class of the syntax handler for version. version can be either a string or the matching constant. Raises KickstartVersionError if version does not match anything. """ try: version = int(version) module = "%s" % versionToString(cast(int, version), skipDevel=True) except ValueError: module = "%s" % version version = stringToVersion(cast(str, version)) module = module.lower() try: import pykickstart.handlers # mypy does not understand module.__path__, skip sys.path.extend(pykickstart.handlers.__path__) # type: ignore found = imp.find_module(module) loaded = imp.load_module(module, found[0], found[1], found[2]) for (k, v) in list(loaded.__dict__.items()): if k.lower().endswith("%shandler" % module): return v except: found = None raise KickstartVersionError(_("Unsupported version specified: %s") % version) finally: # Closing opened files in imp.load_module if found and len(found) > 0: found[0].close()
def loadModules(moduleDir, cls_pattern="_TestCase", skip_list=None): '''taken from firstboot/loader.py''' if skip_list is None: skip_list = ["__init__", "baseclass"] # Guaruntee that __init__ is skipped if skip_list.count("__init__") == 0: skip_list.append("__init__") tests = list() # Make sure moduleDir is in the system path so imputil works. if not moduleDir in sys.path: sys.path.insert(0, moduleDir) # Get a list of all *.py files in moduleDir lst = [ os.path.splitext(os.path.basename(x))[0] for x in glob.glob(moduleDir + "/*.py") ] # Inspect each .py file found for module in lst: if module in skip_list: continue # Attempt to load the found module. try: try: found = imp.find_module(module) loaded = imp.load_module(module, found[0], found[1], found[2]) except ImportError as e: print(_("Error loading module %s: %s") % (module, e)) found = None continue # Find class names that match the supplied pattern (default: "_TestCase") beforeCount = len(tests) for obj in list(loaded.__dict__.keys()): if obj.endswith(cls_pattern): tests.append(loaded.__dict__[obj]) afterCount = len(tests) # Warn if no tests found if beforeCount == afterCount: print( _("Module %s does not contain any test cases; skipping.") % module) continue finally: # Closing opened files in imp.load_module if found and len(found) > 0: found[0].close() return tests
def loadModules(moduleDir, cls_pattern="_TestCase", skip_list=None): '''taken from firstboot/loader.py''' if skip_list is None: skip_list = ["__init__", "baseclass"] # Guaruntee that __init__ is skipped if skip_list.count("__init__") == 0: skip_list.append("__init__") tests = list() # Make sure moduleDir is in the system path so imputil works. if not moduleDir in sys.path: sys.path.insert(0, moduleDir) # Get a list of all *.py files in moduleDir lst = [os.path.splitext(os.path.basename(x))[0] for x in glob.glob(moduleDir + "/*.py")] # Inspect each .py file found for module in lst: if module in skip_list: continue # Attempt to load the found module. try: try: found = imp.find_module(module) loaded = imp.load_module(module, found[0], found[1], found[2]) except ImportError as e: print(_("Error loading module %s: %s") % (module, e)) found = None continue # Find class names that match the supplied pattern (default: "_TestCase") beforeCount = len(tests) for obj in list(loaded.__dict__.keys()): if obj.endswith(cls_pattern): tests.append(loaded.__dict__[obj]) afterCount = len(tests) # Warn if no tests found if beforeCount == afterCount: print(_("Module %s does not contain any test cases; skipping.") % module) continue finally: # Closing opened files in imp.load_module if found and len(found) > 0: found[0].close() return tests