diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2020-01-03 15:59:45 -0800 |
---|---|---|
committer | Todd Gamblin <tgamblin@llnl.gov> | 2020-11-17 10:04:13 -0800 |
commit | 309ae856ab8c894b4635a07efb31c7643df1e91f (patch) | |
tree | ebd8568497220f8602048004454ed2cb475ad0cc /lib | |
parent | cb8ca505ef7987fff5f4e839386d780405c47016 (diff) | |
download | spack-309ae856ab8c894b4635a07efb31c7643df1e91f.tar.gz spack-309ae856ab8c894b4635a07efb31c7643df1e91f.tar.bz2 spack-309ae856ab8c894b4635a07efb31c7643df1e91f.tar.xz spack-309ae856ab8c894b4635a07efb31c7643df1e91f.zip |
commands: add --json argument to `spack solve`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/spack/cmd/solve.py | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/lib/spack/spack/cmd/solve.py b/lib/spack/spack/cmd/solve.py index 8c733fa0d8..a9197bb0cb 100644 --- a/lib/spack/spack/cmd/solve.py +++ b/lib/spack/spack/cmd/solve.py @@ -14,6 +14,7 @@ import llnl.util.tty as tty import spack import spack.cmd import spack.cmd.common.arguments as arguments +import spack.hash_types as ht import spack.package import spack.solver.asp as asp @@ -39,8 +40,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'], @@ -101,16 +105,20 @@ def solve(parser, args): assert best[1] == result.answers[-1][1] opt, i, answer = best - if not args.yaml: + if not args.format: tty.msg("Best of %d answers." % (i + 1)) tty.msg("Optimization: %s" % opt) # iterate over roots from command line - for spec in specs: - answer_spec = answer[spec.name] - if args.yaml: - out = answer_spec.to_yaml() + for input_spec in specs: + spec = answer[input_spec.name] + + # With -y, just print YAML to output. + if args.format == 'yaml': + # use write because to_yaml already has a newline. + sys.stdout.write(spec.to_yaml(hash=ht.build_hash)) + elif args.format == 'json': + sys.stdout.write(spec.to_json(hash=ht.build_hash)) else: - out = answer_spec.tree( - color=sys.stdout.isatty(), **kwargs) - sys.stdout.write(out) + sys.stdout.write( + spec.tree(color=sys.stdout.isatty(), **kwargs)) |