summaryrefslogtreecommitdiff
path: root/src/buildstream
diff options
context:
space:
mode:
Diffstat (limited to 'src/buildstream')
-rw-r--r--src/buildstream/_frontend/cli.py24
-rw-r--r--src/buildstream/_frontend/widget.py36
-rw-r--r--src/buildstream/_stream.py24
-rw-r--r--src/buildstream/element.py12
4 files changed, 96 insertions, 0 deletions
diff --git a/src/buildstream/_frontend/cli.py b/src/buildstream/_frontend/cli.py
index 276f81a6a..36383b3dd 100644
--- a/src/buildstream/_frontend/cli.py
+++ b/src/buildstream/_frontend/cli.py
@@ -1218,6 +1218,30 @@ def artifact_log(app, artifacts):
click.echo_via_pager(data)
+################################################################
+# Artifact List-Contents Command #
+################################################################
+@artifact.command(name='list-contents', short_help="List the contents of an artifact")
+@click.argument('artifacts', type=click.Path(), nargs=-1)
+@click.pass_obj
+def artifact_list_contents(app, artifacts):
+ """List the contents of an artifact.
+
+ Note that 'artifacts' can be element names, which must end in '.bst',
+ or artifact references, which must be in the format `<project_name>/<element>/<key>`.
+
+ """
+ # Note that the backticks in the above docstring are important for the
+ # generated docs. When sphinx is generating rst output from the help output
+ # of this command, the asterisks will be interpreted as emphasis tokens if
+ # they are not somehow escaped.
+
+ with app.initialized():
+ elements_to_files = app.stream.artifact_list_contents(artifacts)
+ click.echo(app.logger._pretty_print_dictionary(elements_to_files))
+ sys.exit(0)
+
+
###################################################################
# Artifact Delete Command #
###################################################################
diff --git a/src/buildstream/_frontend/widget.py b/src/buildstream/_frontend/widget.py
index fbde249a9..b0472bd45 100644
--- a/src/buildstream/_frontend/widget.py
+++ b/src/buildstream/_frontend/widget.py
@@ -801,3 +801,39 @@ class LogLine(Widget):
text += '\n'
return text
+
+ # _pretty_print_dictionary()
+ #
+ # Formats a dictionary so it can be easily read by the user
+ #
+ # Args:
+ # values: A dictionary
+ # style_value: Whether to use the content profile for the values
+ #
+ # Returns:
+ # (str): The formatted values
+ #
+ def _pretty_print_dictionary(self, values, style_value=True):
+ text = ''
+ max_key_len = 0
+ for key, value in values.items():
+ max_key_len = max(len(key), max_key_len)
+
+ for key, value in values.items():
+ if isinstance(value, str) and '\n' in value:
+ text += self.format_profile.fmt(" {}:".format(key))
+ text += textwrap.indent(value, self._indent)
+ continue
+
+ text += self.format_profile.fmt(" {}:{}".format(key, ' ' * (max_key_len - len(key))))
+
+ value_list = ''
+ for item in value:
+ value_list += "\n\t{}".format(item)
+ if style_value:
+ text += self.content_profile.fmt(value_list)
+ else:
+ text += value_list
+ text += '\n'
+
+ return text
diff --git a/src/buildstream/_stream.py b/src/buildstream/_stream.py
index cbd635af7..ad5bb89f7 100644
--- a/src/buildstream/_stream.py
+++ b/src/buildstream/_stream.py
@@ -617,6 +617,30 @@ class Stream():
return logsdirs
+ # artifact_list_contents()
+ #
+ # Show a list of contents of an artifact
+ #
+ # Args:
+ # targets (str): Targets to view the contents of
+ #
+ # Returns:
+ # elements_to_files (list): A list of tuples of the artifact name and it's contents
+ #
+ def artifact_list_contents(self, targets):
+ # Return list of Element and/or ArtifactElement objects
+ target_objects = self.load_selection(targets, selection=PipelineSelection.NONE, load_refs=True)
+
+ elements_to_files = {}
+ for obj in target_objects:
+ if isinstance(obj, ArtifactElement):
+ obj.name = obj.get_artifact_name()
+ files = obj.get_artifact_relative_file_paths()
+ if files == []:
+ files = ["This element has no associated artifacts"]
+ elements_to_files[obj.name] = files
+ return elements_to_files
+
# artifact_delete()
#
# Remove artifacts from the local cache
diff --git a/src/buildstream/element.py b/src/buildstream/element.py
index 21c38bc1a..9aea442c3 100644
--- a/src/buildstream/element.py
+++ b/src/buildstream/element.py
@@ -925,6 +925,18 @@ class Element(Plugin):
self.__batch_prepare_assemble_flags = flags
self.__batch_prepare_assemble_collect = collect
+ def get_artifact_relative_file_paths(self):
+ """ Gets the file paths in the artifact and return them in a list
+
+ Args:
+ targets (str): The name of the artifact to get files of
+
+ Returns:
+ (list): A list of the file paths in the artifact
+ """
+ casbd = self.__artifact.get_files()
+ return [f for f in casbd.list_relative_paths()]
+
#############################################################
# Private Methods used in BuildStream #
#############################################################