richardmaw-codethink pushed to branch richardmaw/artifact-log at BuildStream / buildstream
Commits:
-
39ecfff3
by Richard Maw at 2018-11-08T18:16:12Z
-
86b41fc9
by Richard Maw at 2018-11-08T18:16:12Z
-
3b72acaf
by Richard Maw at 2018-11-08T18:16:13Z
3 changed files:
Changes:
| ... | ... | @@ -2,6 +2,8 @@ |
| 2 | 2 |
buildstream 1.3.1
|
| 3 | 3 |
=================
|
| 4 | 4 |
|
| 5 |
+ o Added `bst artifact log` subcommand for viewing build logs.
|
|
| 6 |
+ |
|
| 5 | 7 |
o BREAKING CHANGE: The 'manual' element lost its default 'MAKEFLAGS' and 'V'
|
| 6 | 8 |
environment variables. There is already a 'make' element with the same
|
| 7 | 9 |
variables. Note that this is a breaking change, it will require users to
|
| ... | ... | @@ -852,3 +852,52 @@ def source_bundle(app, element, force, directory, |
| 852 | 852 |
def artifact():
|
| 853 | 853 |
"""Manipulate cached Artifacts"""
|
| 854 | 854 |
pass
|
| 855 |
+ |
|
| 856 |
+ |
|
| 857 |
+################################################################
|
|
| 858 |
+# Artifact Log Command #
|
|
| 859 |
+################################################################
|
|
| 860 |
+@artifact.command(name='log', short_help="Show logs of an artifact")
|
|
| 861 |
+@click.option('-e', '--element', 'elements', help="Show logs for artifacts of this element",
|
|
| 862 |
+ type=click.Path(readable=False), multiple=True, required=False)
|
|
| 863 |
+@click.argument('artifacts', type=click.Path(), nargs=-1)
|
|
| 864 |
+@click.pass_obj
|
|
| 865 |
+def artifact_log(app, elements, artifacts):
|
|
| 866 |
+ """Show logs of all artifacts"""
|
|
| 867 |
+ from .._exceptions import CASError
|
|
| 868 |
+ from .._message import MessageType
|
|
| 869 |
+ from .._pipeline import PipelineSelection
|
|
| 870 |
+ |
|
| 871 |
+ with app.initialized():
|
|
| 872 |
+ cache = app.context.artifactcache
|
|
| 873 |
+ |
|
| 874 |
+ extractdirs = []
|
|
| 875 |
+ if artifacts:
|
|
| 876 |
+ for ref in artifacts:
|
|
| 877 |
+ try:
|
|
| 878 |
+ # NOTE: This duplicates logic from ArtifactCache in a
|
|
| 879 |
+ # fragile way to avoid having to extract the contents of
|
|
| 880 |
+ # the whole artifact just to see its logs, since it'll be
|
|
| 881 |
+ # extracted for other uses amortising the cost of
|
|
| 882 |
+ # extracting the other content.
|
|
| 883 |
+ destdir = cache.cas.extract(ref, os.path.join(cache.extractdir, os.path.dirname(ref)))
|
|
| 884 |
+ extractdirs.append(destdir)
|
|
| 885 |
+ except CASError as e:
|
|
| 886 |
+ app._message(MessageType.WARN, "Artifact {} is not cached".format(ref), detail=str(e))
|
|
| 887 |
+ continue
|
|
| 888 |
+ if elements:
|
|
| 889 |
+ elements = app.stream.load_selection(elements, selection=PipelineSelection.NONE)
|
|
| 890 |
+ for element in elements:
|
|
| 891 |
+ if not element._cached():
|
|
| 892 |
+ app._message(MessageType.WARN, "Element {} is not cached".format(element))
|
|
| 893 |
+ continue
|
|
| 894 |
+ destdir = cache.extract(element, element._get_cache_key())
|
|
| 895 |
+ extractdirs.append(destdir)
|
|
| 896 |
+ |
|
| 897 |
+ for extractdir in extractdirs:
|
|
| 898 |
+ logsdir = os.path.join(extractdir, "logs")
|
|
| 899 |
+ for log in (os.path.join(logsdir, log) for log in os.listdir(logsdir)):
|
|
| 900 |
+ # NOTE: Should click gain the ability to pass files to the pager this can be optimised.
|
|
| 901 |
+ with open(log) as f:
|
|
| 902 |
+ data = f.read()
|
|
| 903 |
+ click.echo_via_pager(data)
|
| 1 |
+#
|
|
| 2 |
+# Copyright (C) 2018 Codethink Limited
|
|
| 3 |
+# Copyright (C) 2018 Bloomberg Finance LP
|
|
| 4 |
+#
|
|
| 5 |
+# This program is free software; you can redistribute it and/or
|
|
| 6 |
+# modify it under the terms of the GNU Lesser General Public
|
|
| 7 |
+# License as published by the Free Software Foundation; either
|
|
| 8 |
+# version 2 of the License, or (at your option) any later version.
|
|
| 9 |
+#
|
|
| 10 |
+# This library is distributed in the hope that it will be useful,
|
|
| 11 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 12 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
| 13 |
+# Lesser General Public License for more details.
|
|
| 14 |
+#
|
|
| 15 |
+# You should have received a copy of the GNU Lesser General Public
|
|
| 16 |
+# License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
| 17 |
+#
|
|
| 18 |
+# Authors: Richard Maw <richard maw codethink co uk>
|
|
| 19 |
+#
|
|
| 20 |
+ |
|
| 21 |
+import os
|
|
| 22 |
+import pytest
|
|
| 23 |
+ |
|
| 24 |
+from tests.testutils import cli_integration as cli
|
|
| 25 |
+ |
|
| 26 |
+ |
|
| 27 |
+pytestmark = pytest.mark.integration
|
|
| 28 |
+ |
|
| 29 |
+ |
|
| 30 |
+# Project directory
|
|
| 31 |
+DATA_DIR = os.path.join(
|
|
| 32 |
+ os.path.dirname(os.path.realpath(__file__)),
|
|
| 33 |
+ "project",
|
|
| 34 |
+)
|
|
| 35 |
+ |
|
| 36 |
+ |
|
| 37 |
+@pytest.mark.integration
|
|
| 38 |
+@pytest.mark.datafiles(DATA_DIR)
|
|
| 39 |
+def test_artifact_log(cli, tmpdir, datafiles):
|
|
| 40 |
+ project = os.path.join(datafiles.dirname, datafiles.basename)
|
|
| 41 |
+ |
|
| 42 |
+ # Get the cache key of our test element
|
|
| 43 |
+ result = cli.run(project=project, silent=True, args=[
|
|
| 44 |
+ '--no-colors',
|
|
| 45 |
+ 'show', '--deps', 'none', '--format', '%{full-key}',
|
|
| 46 |
+ 'base.bst'
|
|
| 47 |
+ ])
|
|
| 48 |
+ key = result.output.strip()
|
|
| 49 |
+ |
|
| 50 |
+ # Ensure we have an artifact to read
|
|
| 51 |
+ result = cli.run(project=project, args=['build', 'base.bst'])
|
|
| 52 |
+ assert result.exit_code == 0
|
|
| 53 |
+ |
|
| 54 |
+ # Read the log via the element name
|
|
| 55 |
+ result = cli.run(project=project, args=['artifact', 'log', '-e', 'base.bst'])
|
|
| 56 |
+ assert result.exit_code == 0
|
|
| 57 |
+ log = result.output
|
|
| 58 |
+ |
|
| 59 |
+ # Read the log via the key
|
|
| 60 |
+ result = cli.run(project=project, args=['artifact', 'log', 'test/base/' + key])
|
|
| 61 |
+ assert result.exit_code == 0
|
|
| 62 |
+ assert log == result.output
|
