diff options
author | James Ennis <james.ennis@codethink.com> | 2019-01-18 12:54:34 +0000 |
---|---|---|
committer | James Ennis <james.ennis@codethink.com> | 2019-02-13 09:35:45 +0000 |
commit | 775ac472a3bb86fe048924d843fbfde69ff2b911 (patch) | |
tree | 4acebd8e3629a05cf6e970e13cd3859bf6a1abee | |
parent | 77345317405c680064487817e7da622dc7bdd5ad (diff) | |
download | buildstream-775ac472a3bb86fe048924d843fbfde69ff2b911.tar.gz |
cli.py: Move artifact ref handling logic to stream
The loading of elements and the handling of artifacts does not belong
in this module. Such logic should be invoked using the Stream API
-rw-r--r-- | buildstream/_frontend/cli.py | 65 | ||||
-rw-r--r-- | buildstream/_stream.py | 25 |
2 files changed, 43 insertions, 47 deletions
diff --git a/buildstream/_frontend/cli.py b/buildstream/_frontend/cli.py index b3c4634a9..43c5269cd 100644 --- a/buildstream/_frontend/cli.py +++ b/buildstream/_frontend/cli.py @@ -1111,53 +1111,24 @@ def artifact_push(app, elements, deps, remote): @click.pass_obj def artifact_log(app, 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 - - elements, artifacts = _classify_artifacts(artifacts, cache.cas, - app.project.directory) - - vdirs = [] - extractdirs = [] - if artifacts: - for ref in artifacts: - try: - cache_id = cache.cas.resolve_ref(ref, update_mtime=True) - vdir = CasBasedDirectory(cache.cas, cache_id) - 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()) - cache_id = cache.cas.resolve_ref(ref, update_mtime=True) - vdir = CasBasedDirectory(cache.cas, cache_id) - 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) + with app.initialized(): + logsdirs = app.stream.artifact_log(artifacts) + + with ExitStack() as stack: + extractdirs = [] + for logsdir in logsdirs: + # NOTE: If reading the logs feels unresponsive, here would be a good place + # to provide progress information. + 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) ################################################################## diff --git a/buildstream/_stream.py b/buildstream/_stream.py index 6fc5c45fb..6e97aeccd 100644 --- a/buildstream/_stream.py +++ b/buildstream/_stream.py @@ -495,6 +495,31 @@ class Stream(): raise StreamError("Error while staging dependencies into a sandbox" ": '{}'".format(e), detail=e.detail, reason=e.reason) from e + # artifact_log() + # + # Show the full log of an artifact + # + # Args: + # targets (str): Targets to view the logs of + # + # Returns: + # logsdir (list): A list of CasBasedDirectory objects containing artifact logs + # + def artifact_log(self, targets): + # Return list of Element and/or ArtifactElement objects + target_objects = self.load_selection(targets, selection=PipelineSelection.NONE, load_refs=True) + + logsdirs = [] + for obj in target_objects: + ref = obj.get_artifact_name() + if not obj._cached(): + self._message(MessageType.WARN, "{} is not cached".format(ref)) + continue + + logsdirs.append(self._artifacts.get_artifact_logs(ref)) + + return logsdirs + # source_checkout() # # Checkout sources of the target element to the specified location |