From 757387dc2a43a0add5a6d49ccc8bbc6a73352720 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Thu, 24 Oct 2019 16:01:31 -0700 Subject: commands: Add `--json` argument to `spack spec` (#13431) We've had `spack spec --yaml` for a while, and we've had methods for JSON for a while as well. We just haven't has a `--json` argument for `spack spec`. - [x] Add a `--json` argument to `spack spec`, just like `--yaml` --- lib/spack/spack/cmd/spec.py | 16 +++++++++++----- lib/spack/spack/test/cmd/spec.py | 12 ++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/spack/spack/cmd/spec.py b/lib/spack/spack/cmd/spec.py index f1a2a08119..f63ac638c4 100644 --- a/lib/spack/spack/cmd/spec.py +++ b/lib/spack/spack/cmd/spec.py @@ -25,8 +25,11 @@ def setup_parser(subparser): arguments.add_common_arguments( subparser, ['long', 'very_long', 'install_status']) subparser.add_argument( - '-y', '--yaml', action='store_true', default=False, - help='print concrete spec as YAML') + '-y', '--yaml', action='store_const', dest='format', default=None, + const='yaml', help='print concrete spec as YAML') + subparser.add_argument( + '-j', '--json', action='store_const', dest='format', default=None, + const='json', help='print concrete spec as YAML') subparser.add_argument( '-c', '--cover', action='store', default='nodes', choices=['nodes', 'edges', 'paths'], @@ -59,12 +62,15 @@ def spec(parser, args): for spec in spack.cmd.parse_specs(args.specs): # With -y, just print YAML to output. - if args.yaml: + if args.format: if spec.name in spack.repo.path or spec.virtual: spec.concretize() - # use write because to_yaml already has a newline. - sys.stdout.write(spec.to_yaml(hash=ht.build_hash)) + if args.format == 'yaml': + # use write because to_yaml already has a newline. + sys.stdout.write(spec.to_yaml(hash=ht.build_hash)) + else: + print(spec.to_json(hash=ht.build_hash)) continue kwargs['hashes'] = False # Always False for input spec diff --git a/lib/spack/spack/test/cmd/spec.py b/lib/spack/spack/test/cmd/spec.py index 3038f4d9e4..4637e14a1f 100644 --- a/lib/spack/spack/test/cmd/spec.py +++ b/lib/spack/spack/test/cmd/spec.py @@ -37,6 +37,18 @@ def test_spec_yaml(): assert 'mpich' in mpileaks +def test_spec_json(): + output = spec('--json', 'mpileaks') + + mpileaks = spack.spec.Spec.from_json(output) + assert 'mpileaks' in mpileaks + assert 'callpath' in mpileaks + assert 'dyninst' in mpileaks + assert 'libdwarf' in mpileaks + assert 'libelf' in mpileaks + assert 'mpich' in mpileaks + + def _parse_types(string): """Parse deptypes for specs from `spack spec -t` output.""" lines = string.strip().split('\n') -- cgit v1.2.3-70-g09d2