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
import spack.cmd import spack.cmd
import spack.cmd.common.arguments as arguments import spack.cmd.common.arguments as arguments
import spack.hash_types as ht
import spack.package import spack.package
import spack.solver.asp as asp import spack.solver.asp as asp
@ -39,8 +40,11 @@ def setup_parser(subparser):
arguments.add_common_arguments( arguments.add_common_arguments(
subparser, ['long', 'very_long', 'install_status']) subparser, ['long', 'very_long', 'install_status'])
subparser.add_argument( subparser.add_argument(
'-y', '--yaml', action='store_true', default=False, '-y', '--yaml', action='store_const', dest='format', default=None,
help='print concrete spec as YAML') 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( subparser.add_argument(
'-c', '--cover', action='store', '-c', '--cover', action='store',
default='nodes', choices=['nodes', 'edges', 'paths'], default='nodes', choices=['nodes', 'edges', 'paths'],
@ -101,16 +105,20 @@ def solve(parser, args):
assert best[1] == result.answers[-1][1] assert best[1] == result.answers[-1][1]
opt, i, answer = best opt, i, answer = best
if not args.yaml: if not args.format:
tty.msg("Best of %d answers." % (i + 1)) tty.msg("Best of %d answers." % (i + 1))
tty.msg("Optimization: %s" % opt) tty.msg("Optimization: %s" % opt)
# iterate over roots from command line # iterate over roots from command line
for spec in specs: for input_spec in specs:
answer_spec = answer[spec.name] spec = answer[input_spec.name]
if args.yaml:
out = answer_spec.to_yaml() # 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: else:
out = answer_spec.tree( sys.stdout.write(
color=sys.stdout.isatty(), **kwargs) spec.tree(color=sys.stdout.isatty(), **kwargs))
sys.stdout.write(out)