commands: add --json argument to spack solve

This commit is contained in:
Todd Gamblin 2020-01-03 15:59:45 -08:00
parent cb8ca505ef
commit 309ae856ab

View file

@ -14,6 +14,7 @@
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))