kevinschoon commented on code in PR #2035:
URL: https://github.com/apache/buildstream/pull/2035#discussion_r2193127946


##########
src/buildstream/_frontend/inspect.py:
##########
@@ -0,0 +1,184 @@
+import json
+import sys
+
+from dataclasses import dataclass
+
+from ruamel.yaml import YAML
+
+from ..types import _Encoding, _ElementState, _PipelineSelection, _Scope
+
+# Inspectable Elements as serialized to the terminal
+@dataclass
+class _InspectElement:
+    name: str
+    description: str
+    workspace: any
+    key: str
+    key_full: str
+    state: str
+    environment: dict[str, str]
+    variables: dict[str, str]
+    artifact: any
+    dependencies: list[str]
+    build_dependencies: list[str]
+    runtime_dependencies: list[str]
+    sources: list[dict[str, str]]
+
+@dataclass
+class _ProjectOutput:
+    name: str
+    directory: str
+
+@dataclass
+class _InspectOutput:
+    project: _ProjectOutput
+    elements: list[_InspectElement]
+
+# Inspect elements from a given Buildstream project
+class Inspector:
+    def __init__(self, stream, project):
+        self.stream = stream
+        self.project = project
+
+    def _read_state(self, element):
+        try:
+            if not element._has_all_sources_resolved():
+                return _ElementState.NO_REFERENCE
+            else:
+                if element.get_kind() == "junction":
+                    return _ElementState.JUNCTION
+                elif not element._can_query_cache():
+                    return _ElementState.WAITING
+                elif element._cached_failure():
+                    return _ElementState.FAILED
+                elif element._cached_success():
+                    return _ElementState.CACHED
+                elif not element._can_query_source_cache():
+                    return _ElementState.WAITING
+                elif element._fetch_needed():
+                    return _ElementState.FETCH_NEEDED
+                elif element._buildable():
+                    return _ElementState.BUILDABLE
+                else:
+                    return _ElementState.WAITING
+        except BstError as e:
+            # Provide context to plugin error
+            e.args = ("Failed to determine state for {}: 
{}".format(element._get_full_name(), str(e)),)
+            raise e
+
+    def _elements(self, dependencies, with_state=False):
+        for element in dependencies:
+
+            name = element._get_full_name()
+            description = " ".join(element._description.splitlines())
+            workspace = element._get_workspace()
+            variables = dict(element._Element__variables)
+            environment = dict(element._Element__environment)
+
+            sources = []
+            for source in element.sources():
+                source_infos = source.collect_source_info()
+
+                if source_infos is not None:
+                    serialized_sources = []
+                    for s in source_infos:
+                        serialized = s.serialize()
+                        serialized_sources.append(serialized)
+
+                    sources += serialized_sources
+
+            # Show dependencies
+            dependencies = [e._get_full_name() for e in 
element._dependencies(_Scope.ALL, recurse=False)]
+
+            # Show build dependencies
+            build_dependencies = [e._get_full_name() for e in 
element._dependencies(_Scope.BUILD, recurse=False)]
+
+            # Show runtime dependencies
+            runtime_dependencies = runtime_dependencies = [e._get_full_name() 
for e in element._dependencies(_Scope.RUN, recurse=False)]
+
+            # These operations require state and are only shown if requested
+            key = None
+            key_full = None
+            state = None
+            artifact = None
+
+            if with_state:
+                key = element._get_display_key().brief
+
+                key_full = element._get_display_key().full
+
+                state = self._read_state(element).value
+
+                # BUG: Due to the assersion within .get_artifact this will
+                # error but there is no other way to determine if an artifact
+                # exists and we only want to show this value for informational
+                # purposes.
+                try:
+                    _artifact = element._get_artifact()
+                    if _artifact.cached():
+                        artifact = {
+                            "files": artifact.get_files(),
+                            "digest": artifact_files._get_digest(),
+                        }
+                except:
+                    pass
+
+            yield _InspectElement(
+                name=name,
+                description=description,
+                workspace=workspace,
+                key=key,
+                key_full=key_full,
+                state=state,
+                environment=environment,
+                variables=variables,
+                artifact=artifact,
+                dependencies=dependencies,
+                build_dependencies=build_dependencies,
+                runtime_dependencies=runtime_dependencies,
+                sources=sources,
+            )
+
+
+    def _dump_project(self):
+        # TODO: What else do we want here?

Review Comment:
   Please take note of this, I'm unsure what other items we should include in 
the project output.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to