Commit 775ac472 authored by James Ennis's avatar James Ennis
Browse files

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
parent 77345317
Loading
Loading
Loading
Loading
+18 −47
Original line number Diff line number Diff line
@@ -1111,43 +1111,14 @@ 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 app.initialized():
        logsdirs = app.stream.artifact_log(artifacts)

        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"])
            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)
+25 −0
Original line number Diff line number Diff line
@@ -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