This is an automated email from the ASF dual-hosted git repository.

not-in-ldap pushed a commit to branch richardmaw/wip-artifact-subcommands
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit ce9ec9a595fa7ae01211d8c0e4dd8ce654462809
Author: Richard Maw <[email protected]>
AuthorDate: Thu Nov 1 15:24:32 2018 +0000

    cli: Add artifact log command
---
 buildstream/_frontend/cli.py | 56 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/buildstream/_frontend/cli.py b/buildstream/_frontend/cli.py
index e07ec53..8dd1883 100644
--- a/buildstream/_frontend/cli.py
+++ b/buildstream/_frontend/cli.py
@@ -1,5 +1,7 @@
 import os
 import sys
+from contextlib import ExitStack
+from tempfile import TemporaryDirectory
 
 import click
 from .. import _yaml
@@ -889,3 +891,57 @@ def source_bundle(app, element, force, directory,
 def artifact():
     """Manipulate cached Artifacts"""
     pass
+
+
+################################################################
+#                     Artifact Log Command                     #
+################################################################
[email protected](name='log', short_help="Show logs of an artifact")
[email protected]('-e', '--element', 'elements', help="Show logs for artifacts of 
this element",
+              type=click.Path(readable=False), multiple=True, required=False)
[email protected]('artifacts', type=click.Path(), nargs=-1)
[email protected]_obj
+def artifact_log(app, elements, artifacts):
+    """Show logs of all artifacts"""
+    from .._exceptions import CASError
+    from .._message import MessageType
+    from .._pipeline import PipelineSelection
+    from ..storage._casbaseddirectory import CasBasedDirectory
+
+    with ExitStack() as stack:
+        stack.enter_context(app.initialized())
+        cache = app.context.artifactcache
+
+        vdirs = []
+        extractdirs = []
+        if artifacts:
+            for ref in artifacts:
+                try:
+                    vdir = CasBasedDirectory(cache.cas, 
cache.cas.resolve_ref(ref, update_mtime=True))
+                    vdirs.append(vdir)
+                except CASError as e:
+                    app._message(MessageType.WARN, "Artifact {} is not 
cached".format(ref), detail=str(e))
+                    continue
+        if elements:
+            elements = app.stream.load_selection(elements, 
selection=PipelineSelection.NONE)
+            for element in elements:
+                if not element._cached():
+                    app._message(MessageType.WARN, "Element {} is not 
cached".format(element))
+                    continue
+                ref = cache.get_artifact_fullname(element, 
element._get_cache_key())
+                vdir = CasBasedDirectory(cache.cas, cache.cas.resolve_ref(ref, 
update_mtime=True))
+                vdirs.append(vdir)
+
+        for vdir in vdirs:
+            # NOTE: If reading the logs feels unresponsive, here would be a 
good place to provide progress information.
+            logsdir = vdir.descend(["logs"])
+            td = stack.enter_context(TemporaryDirectory())
+            logsdir.export_files(td, can_link=True)
+            extractdirs.append(td)
+
+        for extractdir in extractdirs:
+            for log in (os.path.join(extractdir, log) for log in 
os.listdir(extractdir)):
+                # NOTE: Should click gain the ability to pass files to the 
pager this can be optimised.
+                with open(log) as f:
+                    data = f.read()
+                    click.echo_via_pager(data)

Reply via email to