def dump_info(comps): for c in comps: comp = dr.get_component(c) if not comp: print("Unknown component: %s" % c) else: print_component(comp, verbose=True)
def _load(self, spec): comp = dr.get_component(spec["name"]) if comp is None: raise Exception(f"Couldn't find {spec['name']}.") args = spec.get("args", []) kwargs = spec.get("kwargs", {}) return comp(*args, **kwargs)
def evaluate(self, name): """ Evaluate a component and return its result. Prints diagnostic information in the case of failure. This function is useful when a component's name contains characters that aren't valid for python identifiers so you can't access it with ``models.<name>``. Args: name (str): the name of the component as shown by :func:`Models.find()`. """ comp = self.get(name) or dr.get_component(name) if not comp: return if not plugins.is_rule(comp): self._requested.add((name, comp)) if comp in self._broker: return self._broker[comp] if comp in self._broker.exceptions or comp in self._broker.missing_requirements: self._dump_diagnostics(comp) return val = dr.run(comp, broker=self._broker).get(comp) if comp not in self._broker: if comp in self._broker.exceptions or comp in self._broker.missing_requirements: self._dump_diagnostics(comp) else: print("{} chose to skip.".format(dr.get_name(comp))) return val
def get_rule_hit_info(archive, rule_name, timeout=None, tmp_dir=None): # We have to load everything again for multiprocessing or clustering to # work. Even though imports are cached internally after the first call, we # can still optimize a bit with this LOADED hack. rule_func = dr.get_component(rule_name) if not LOADED[0]: load_default_plugins() LOADED[0] = True # this is also cached behind get_deps after the first call. graph, bool_deps = get_deps(rule_func) with extract(archive, timeout=timeout, extract_dir=tmp_dir) as arc: ctx = create_context(arc.tmp_dir, None) broker = dr.Broker() broker[ctx.__class__] = ctx results = dr.run(graph, broker=broker) rule_result = results.get(rule_func) rhr = results.get(RedHatRelease) result = extract_hits(bool_deps, results) result["archive"] = archive result["key"] = rule_result.get_key() if rule_result else None result["type"] = rule_result.__class__.__name__ if rule_result else None result["make_fail"] = True if rule_result and isinstance(rule_result, make_response) else False result["major"] = rhr.major if rhr else -1 result["minor"] = rhr.minor if rhr else -1 return result
def _get_format(self): if "format" not in self.service: return HumanReadableFormat name = self.service["format"] fmt = dr.get_component(name) if fmt is None: raise Exception(f"Couldn't find {name}.") return fmt
def _get_publisher(self): if "publisher" not in self.service: return StdOut() spec = self.service["publisher"] Publisher = dr.get_component(spec["name"]) if Publisher is None: raise Exception(f"Couldn't find {spec['name']}.") args = spec.get("args", []) kwargs = spec.get("kwargs", {}) return Publisher(*args, **kwargs)
def _get_consumer(self, publisher, downloader, engine): if "consumer" not in self.service: return Interactive(publisher, downloader, engine) spec = self.service["consumer"] Consumer = dr.get_component(spec["name"]) if Consumer is None: raise Exception(f"Couldn't find {spec['name']}.") args = spec.get("args", []) kwargs = spec.get("kwargs", {}) return Consumer(publisher, downloader, engine, *args, **kwargs)
def setup(self, config): self._runner = Runner( packages=config.get("packages", []), format=dr.get_component(config["format"]) if "format" in config else JsonFormat, include_timings=config.get("include_timings", False), include_tracebacks=config.get("include_tracebacks", False), target_components=config.get("target_components", []), component_config=config.get("component_config", {}), )
def _resolve_engine_config(self, config): return { "formatter": dr.get_component(config.get("format")), "target_components": self._get_graphs(config.get("target_components", [])), "extract_timeout": config.get("extract_timeout"), "extract_tmp_dir": config.get("extract_tmp_dir"), }
def create_context(ctx): """ Loads and constructs the specified context with the specified arguments. If a '.' isn't in the class name, the 'insights.core.context' package is assumed. """ ctx_cls_name = ctx.get("class", "insights.core.context.HostContext") if "." not in ctx_cls_name: ctx_cls_name = "insights.core.context." + ctx_cls_name ctx_cls = dr.get_component(ctx_cls_name) ctx_args = ctx.get("args", {}) return ctx_cls(**ctx_args)
def get_components(comps, default_module): if not default_module.endswith("."): default_module = default_module + "." results = [] if comps: comps = [t.strip() for t in comps.split(",")] comps = [t if "." in t else default_module + t for t in comps] for t in comps: c = dr.get_component(t) if not c: raise Exception("Could not load %s." % t) results.append(c) return results
def main(client): args = parse_args() rule_func = dr.get_component(args.plugin) if not is_type(rule_func, rule): print(f"{dr.get_name(rule_func)} is not a rule.") return archives = load_archives(args.input) hit_data = run_rule(archives, rule_func) hit_data = client.persist(hit_data) analysis = analyze(hit_data, rule_func) print(json.dumps(analysis))
def _get_engine(self): engine_config = self._resolve_engine_config(self.service) if "engine" not in self.service: return Engine(**engine_config) spec = self.service["engine"] kwargs = spec.get("kwargs", {}) engine_config.update(self._resolve_engine_config(kwargs)) EngineCls = dr.get_component(spec["name"]) if EngineCls is None: raise Exception(f"Couldn't find {spec['name']}.") return EngineCls(**engine_config)
def show_source(self, comp): """ Show source for the given module, class, or function. Also accepts the string names, with the side effect that the component will be imported. """ try: if isinstance(comp, six.string_types): comp = self.get(comp) or dr.get_component( comp) or importlib.import_module(comp) comp = inspect.getmodule(comp) ip = IPython.get_ipython() if self._cov: path, runnable, excluded, not_run, _ = self._cov.analysis2( comp) runnable, not_run = set(runnable), set(not_run) src = ip.pycolorize(inspect.getsource(comp)).splitlines() width = len(str(len(src))) template = "{0:>%s}" % width results = [] file_line = "{} {}".format(ansiformat("red", "File:"), os.path.realpath(path)) explain_line = "{} numbered lines have executed. python standard libs are excluded.".format( ansiformat("*brightgreen*", "Green")) results.append(file_line) results.append(explain_line) results.append("") for i, line in enumerate(src, start=1): prefix = template.format(i) if i in runnable and i not in not_run: color = "*brightgreen*" else: color = "gray" results.append("{} {}".format(ansiformat(color, prefix), line)) IPython.core.page.page(six.u(os.linesep.join(results))) else: ip.inspector.pinfo(comp, detail_level=1) except: traceback.print_exc()
def __init__( self, packages=None, format=JsonFormat, include_timings=False, include_tracebacks=False, target_components=None, component_config=None, ): load_default_plugins() load_packages(packages or []) config = component_config or {} apply_default_enabled(config) apply_configs(config) target_components = [ dr.get_component(c) for c in target_components or [] ] self._Format = format self._include_timings = include_timings self._include_tracebacks = include_tracebacks self._target_components = target_components or None
def get_component(fqdn): return dr.get_component(fqdn)
def get_spec(fqdn): if "." not in fqdn: fqdn = "insights.specs.Specs.%s" % fqdn return dr.get_component(fqdn)
def load_obj(spec): try: return dr.get_component(spec) or importlib.import_module(spec) except: pass