def load_components_from_file(filename): components = {} # Synthesize a module for this component file in batou.c defdir = os.path.dirname(filename) module_name = os.path.basename(defdir) module_path = 'batou.c.{}'.format(module_name) module = types.ModuleType( module_name, 'Component definition module for {}'.format(filename)) sys.modules[module_path] = module setattr(batou.c, module_name, module) exec(compile(open(filename).read(), filename, 'exec'), module.__dict__) for candidate in list(module.__dict__.values()): if candidate in [Component]: continue if not (isinstance(candidate, type) and issubclass(candidate, Component)): continue compdef = ComponentDefinition(candidate, filename, defdir) if compdef.name in components: raise DuplicateComponent(compdef) components[compdef.name] = compdef return components
def test_configurationerrors_can_be_sorted(root): errors = [] errors.append(ConfigurationError("asdffdas", root.component)) errors.append( ConversionError(root.component, "testkey", "testvalue", str, "foobar")) errors.append(MissingOverrideAttributes(root.component, ["asdf", "bsdfg"])) errors.append( DuplicateComponent( ComponentDefinition(root.component.__class__, "asdf.py"), ComponentDefinition(root.component.__class__, "bsdf.py"), )) try: raise ValueError("asdf") except Exception: _, exc_value, exc_traceback = sys.exc_info() errors.append( UnknownComponentConfigurationError(root, exc_value, exc_traceback)) errors.append(UnusedResources({"asdf": [(root.component, 1)]})) errors.append(UnsatisfiedResources({"asdf": [root]})) errors.append(MissingEnvironment(root.environment)) errors.append(ComponentLoadingError("asdf.py", ValueError("asdf"))) errors.append(MissingComponent("component", "hostname")) errors.append(SuperfluousSection("asdf")) errors.append(SuperfluousComponentSection("asdf")) errors.append(SuperfluousSecretsSection("asdf")) errors.append(CycleErrorDetected("foo")) errors.append(NonConvergingWorkingSet([root])) errors.append(DuplicateHostError("asdf")) errors.append(InvalidIPAddressError(("127.0.0.256/24"))) errors.sort(key=lambda x: x.sort_key)