@@ -506,24 +506,15 @@ def resources_uniq_lines(resources, states, formatter, config):
506506 return lines
507507
508508
509- def list_handler (args ):
509+ def get_resource_list (args ):
510+ """
511+ Common function for list_handler() and emit_R()
512+ """
510513 valid_states = ["up" , "down" , "allocated" , "free" , "all" ]
511- headings = {
512- "state" : "STATE" ,
513- "queue" : "QUEUE" ,
514- "properties" : "PROPERTIES" ,
515- "propertiesx" : "PROPERTIES" ,
516- "nnodes" : "NNODES" ,
517- "ncores" : "NCORES" ,
518- "ngpus" : "NGPUS" ,
519- "ranks" : "RANKS" ,
520- "nodelist" : "NODELIST" ,
521- "rlist" : "LIST" ,
522- }
523514 config = None
524515
525- states = args .states .split ("," )
526- for state in states :
516+ args . states = args .states .split ("," )
517+ for state in args . states :
527518 if state not in valid_states :
528519 LOGGER .error ("Invalid resource state %s specified" , state )
529520 sys .exit (1 )
@@ -544,11 +535,28 @@ def list_handler(args):
544535 resources .filter (include = args .include )
545536 except (ValueError , TypeError ) as exc :
546537 raise ValueError (f"--include: { exc } " ) from None
538+ return resources , config
539+
540+
541+ def list_handler (args ):
542+ headings = {
543+ "state" : "STATE" ,
544+ "queue" : "QUEUE" ,
545+ "properties" : "PROPERTIES" ,
546+ "propertiesx" : "PROPERTIES" ,
547+ "nnodes" : "NNODES" ,
548+ "ncores" : "NCORES" ,
549+ "ngpus" : "NGPUS" ,
550+ "ranks" : "RANKS" ,
551+ "nodelist" : "NODELIST" ,
552+ "rlist" : "LIST" ,
553+ }
554+ resources , config = get_resource_list (args )
547555
548556 fmt = FluxResourceConfig ("list" ).load ().get_format_string (args .format )
549557 formatter = flux .util .OutputFormat (fmt , headings = headings )
550558
551- lines = resources_uniq_lines (resources , states , formatter , config )
559+ lines = resources_uniq_lines (resources , args . states , formatter , config )
552560 formatter .print_items (lines .values (), no_header = args .no_header )
553561
554562
@@ -561,6 +569,19 @@ def info(args):
561569 list_handler (args )
562570
563571
572+ def emit_R (args ):
573+ """Emit R in JSON on stdout for requested set of resources"""
574+ resources , config = get_resource_list (args )
575+
576+ rset = ResourceSet ()
577+ for state in args .states :
578+ try :
579+ rset .add (resources [state ])
580+ except AttributeError :
581+ raise ValueError (f"unknown state { state } " )
582+ print (rset .encode ())
583+
584+
564585LOGGER = logging .getLogger ("flux-resource" )
565586
566587
@@ -758,6 +779,24 @@ def main():
758779 help = "allow resources to contain invalid ranks" ,
759780 )
760781
782+ R_parser = subparsers .add_parser ("R" , formatter_class = flux .util .help_formatter ())
783+ R_parser .set_defaults (func = emit_R )
784+ R_parser .add_argument (
785+ "-s" ,
786+ "--states" ,
787+ metavar = "STATE,..." ,
788+ default = "all" ,
789+ help = "Emit R for resources in given states" ,
790+ )
791+ R_parser .add_argument (
792+ "-i" ,
793+ "--include" ,
794+ metavar = "TARGETS" ,
795+ help = "Include only specified targets in output set. TARGETS may be "
796+ + "provided as an idset or hostlist." ,
797+ )
798+ R_parser .add_argument ("--from-stdin" , action = "store_true" , help = argparse .SUPPRESS )
799+
761800 args = parser .parse_args ()
762801 args .func (args )
763802
0 commit comments