diff options
author | Rebecca Grayson <becky.grayson1@hotmail.co.uk> | 2019-08-20 09:55:54 +0100 |
---|---|---|
committer | Rebecca Grayson <becky.grayson1@hotmail.co.uk> | 2019-08-29 14:54:37 +0100 |
commit | 20a8f9e812c9f7dc2ad9c44325afcc1732c1de52 (patch) | |
tree | bba12beb9853522b0eb6c98000e84c5570b342af /src/buildstream/_frontend | |
parent | b794f058c0ff2709ae6a382fff5744a98da97c76 (diff) | |
download | buildstream-20a8f9e812c9f7dc2ad9c44325afcc1732c1de52.tar.gz |
Addition of --out option to bst artifact log:becky/artifact_log_file_option
A --out option has been added, allowing an artifact log to be
written to a logfile. This is particularly useful when more
than one artifact's log is wanting to be read; It will write
a file for each log.
A test and NEWS entry have also been added.
Diffstat (limited to 'src/buildstream/_frontend')
-rw-r--r-- | src/buildstream/_frontend/cli.py | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/src/buildstream/_frontend/cli.py b/src/buildstream/_frontend/cli.py index 661e39575..c1b06b2d1 100644 --- a/src/buildstream/_frontend/cli.py +++ b/src/buildstream/_frontend/cli.py @@ -4,6 +4,7 @@ import sys from functools import partial import fcntl +import shutil import click from .. import _yaml from .._exceptions import BstError, LoadError, AppError @@ -1219,19 +1220,45 @@ def artifact_push(app, elements, deps, remote): # Artifact Log Command # ################################################################ @artifact.command(name='log', short_help="Show logs of artifacts") +@click.option('--out', + type=click.Path(file_okay=True, writable=True), + help="Output logs to individual files in the specified path. If absent, logs are written to stdout.") @click.argument('artifacts', type=click.Path(), nargs=-1) @click.pass_obj -def artifact_log(app, artifacts): +def artifact_log(app, artifacts, out): """Show build logs of artifacts""" - with app.initialized(): - log_file_paths = app.stream.artifact_log(artifacts) - - for log in log_file_paths: - with open(log) as f: - data = f.read() + artifact_logs = app.stream.artifact_log(artifacts) + + if not out: + try: + for log in list(artifact_logs.values()): + with open(log[0], 'r') as f: + data = f.read() + click.echo_via_pager(data) + except (OSError, FileNotFoundError): + click.echo("Error: file cannot be opened", err=True) + sys.exit(1) - click.echo_via_pager(data) + else: + try: + os.mkdir(out) + except FileExistsError: + click.echo("Error: {} already exists".format(out), err=True) + sys.exit(1) + + for name, log_files in artifact_logs.items(): + if len(log_files) > 1: + os.mkdir(name) + for log in log_files: + dest = os.path.join(out, name, log) + shutil.copy(log, dest) + # make a dir and write in log files + else: + log_name = os.path.splitext(name)[0] + '.log' + dest = os.path.join(out, log_name) + shutil.copy(log_files[0], dest) + # write a log file ################################################################ |