Repository: RedHatInsights/insights-core

Issue:
The new `find` does not work as expected
It reports the following errors:

~~~
Traceback
(most recent call last):
  File "/Users/liuxc/work/insights/insights-core/insights/core/dr.py", line 953, in run
    result = DELEGATES[component].process(broker)
  File "/Users/liuxc/work/insights/insights-core/insights/core/dr.py", line 672, in process
    return self.invoke(broker)
  File "/Users/liuxc/work/insights/insights-core/insights/core/plugins.py", line 64, in invoke
    return super(PluginType, self).invoke(broker)
  File "/Users/liuxc/work/insights/insights-core/insights/core/dr.py", line 652, in invoke
    return self.component(*args)
  File "/Users/liuxc/work/insights/insights-core/insights/core/spec_factory.py", line 920, in __call__
    origin = d.cmd or d.path or dr.get_name(self.spec)
AttributeError: 'Context' object has no attribute 'cmd'
~~~





Discussion:

Resolving patch:
diff --git a/insights/core/context.py b/insights/core/context.py
--- a/insights/core/context.py
+++ b/insights/core/context.py
@@ -98,6 +98,8 @@ class Context(object):
     def __init__(self, **kwargs):
         self.version = kwargs.pop("version", DEFAULT_VERSION)
         self.metadata = kwargs.pop("metadata", {})
+        self.loaded = True
+        self.cmd = None
         optional_attrs = [
             "content", "path", "hostname", "release",
             "machine_id", "target", "last_client_run", "relative_path"
@@ -105,6 +107,8 @@ def __init__(self, **kwargs):
         for k in optional_attrs:
             setattr(self, k, kwargs.pop(k, None))
 
+        self.relative_path = self.path
+
         for p in GLOBAL_PRODUCTS:
             if p.name in kwargs:
                 setattr(self, p.name, kwargs.pop(p.name))
diff --git a/insights/core/spec_factory.py b/insights/core/spec_factory.py
--- a/insights/core/spec_factory.py
+++ b/insights/core/spec_factory.py
@@ -890,8 +890,12 @@ def report(starts):
             patterns supported)
 
     Returns:
-        A dict where each key is a command, path, or spec name, and each
-        value is a list of matching lines.
+        A dict where each key is a command, path, or spec name, and each value
+        is a non-empty list of matching lines. Only paths with matching lines
+        are included.
+
+    Raises:
+        dr.SkipComponent if no paths have matching lines.
     """
 
     def __init__(self, spec, pattern):
@@ -917,13 +921,21 @@ def __call__(self, ds):
         results = {}
         ds = ds if isinstance(ds, list) else [ds]
         for d in ds:
-            origin = d.cmd or d.path or dr.get_name(self.spec)
+            if d.relative_path:
+                origin = os.path.join("/", d.relative_path.lstrip("/"))
+            elif d.cmd:
+                origin = d.cmd
+            else:
+                origin = dr.get_name(self.spec)
             stream = d.content if d.loaded else d.stream()
             lines = []
             for line in stream:
                 if any(p in line for p in self.pattern):
                     lines.append(line)
-            results[origin] = lines
+            if lines:
+                results[origin] = lines
+        if not results:
+            raise dr.SkipComponent()
         return dict(results)
 
 


Test patch: