diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2019-10-24 16:01:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-24 16:01:31 -0700 |
commit | 757387dc2a43a0add5a6d49ccc8bbc6a73352720 (patch) | |
tree | 9da550d690a894e3ee09b4af54a560ed9e72ad67 /lib | |
parent | 77af4684aa2986ae3860962d7007b790e0a97f50 (diff) | |
download | spack-757387dc2a43a0add5a6d49ccc8bbc6a73352720.tar.gz spack-757387dc2a43a0add5a6d49ccc8bbc6a73352720.tar.bz2 spack-757387dc2a43a0add5a6d49ccc8bbc6a73352720.tar.xz spack-757387dc2a43a0add5a6d49ccc8bbc6a73352720.zip |
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`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/spack/cmd/spec.py | 16 | ||||
-rw-r--r-- | 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') |