From 63d5c4d9c17236f2edcf7ffb0962fca93e5962f7 Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" Date: Thu, 27 Feb 2025 16:05:44 -0800 Subject: [PATCH 1/3] flux-hostlist: add `-F, --find=HOSTS` option Problem: Other hostlist commands have a `-F, --find` option, but flux-hostlist(1) does not. Also, there is not another good way to determine the index of the current host in a hostlist. Add a `-F, --find=HOSTS` option to flux-hostlist which takes a hostlist and returns a space-separated list of indices of each host in the target hostlist. --- src/cmd/flux-hostlist.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cmd/flux-hostlist.py b/src/cmd/flux-hostlist.py index 31e1b3d0c0d5..19af80170e9b 100755 --- a/src/cmd/flux-hostlist.py +++ b/src/cmd/flux-hostlist.py @@ -72,6 +72,14 @@ def parse_args(): metavar="[-]IDS", help="Output hosts at indices in idset IDS (-IDS to index from end)", ) + parser.add_argument( + "-F", + "--find", + type=str, + metavar="HOSTS", + help="Output indices of HOSTS in target in hostlist." + + " Fails if any host is not found.", + ) parser.add_argument( "-L", "--limit", @@ -394,7 +402,9 @@ def main(): else: hl = hl[IDset(args.nth)] - if args.count: + if args.find: + print(" ".join(map(str, hl.index(args.find)))) + elif args.count: print(f"{hl.count()}") elif args.expand: # Convert '\n' specified on command line to actual newline char From 454df2a13cec5092cd1c50ce8eec111566eab0c8 Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" Date: Thu, 27 Feb 2025 16:19:17 -0800 Subject: [PATCH 2/3] testsuite: add tests for `flux hostlist -F, --find` Problem: There are no tests of `flux hostlist -F, --find`. Add some to t2814-hostlist-cmd.t. --- t/t2814-hostlist-cmd.t | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/t/t2814-hostlist-cmd.t b/t/t2814-hostlist-cmd.t index 7fd2120d1a23..1cec00a0cc58 100755 --- a/t/t2814-hostlist-cmd.t +++ b/t/t2814-hostlist-cmd.t @@ -115,6 +115,15 @@ test_expect_success 'flux-hostlist -n errors with invalid index' ' test_must_fail flux hostlist -n 10 foo[1-10] && test_must_fail flux hostlist -n 1,10 foo[1-10] ' +test_expect_success 'flux-hostlist -F, --find=HOSTS works' ' + test "$(flux hostlist -F foo1 foo[1-10])" = "0" && + test "$(flux hostlist -F foo10 foo[1-10])" = "9" && + test "$(flux hostlist -F foo[1-2] foo[1-10])" = "0 1" +' +test_expect_success 'flux-hostlist -F, --find=HOSTS fails if host not found' ' + test_must_fail flux hostlist -F foo1 foo[2-10] && + test_must_fail flux hostlist -F foo[1-10] foo[2-10] +' test_expect_success 'flux-hostlist -x, --exclude works' ' test "$(flux hostlist -x foo1 foo[0-10])" = "foo[0,2-10]" && test "$(flux hostlist -x foo[0-9] foo[0-10])" = "foo10" From 520eae5622e2234e4f24b3e33ca1d053839a3f29 Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" Date: Thu, 27 Feb 2025 16:22:41 -0800 Subject: [PATCH 3/3] doc: document `-F, --find` option in flux-hostlist(1) Problem: The `-F, --find=HOSTS` option for flux-hostlist is not documented. Document it in flux-hostlist(1). --- doc/man1/flux-hostlist.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/man1/flux-hostlist.rst b/doc/man1/flux-hostlist.rst index 29a5dfe78c4a..5d383cf7b2b2 100644 --- a/doc/man1/flux-hostlist.rst +++ b/doc/man1/flux-hostlist.rst @@ -70,6 +70,12 @@ OPTIONS '0-1' will return the first and second, '-1' returns the last host). The command will fail if any id in *IDS* is not a valid index. +.. option:: -F, --find=HOSTS + + Output a list of space-separated indices of *HOSTS* in the result hostlist, + where *HOSTS* should be one or more hosts in hostlist form. The command + will fail if any host in *HOSTS* is not found. + .. option:: -L, --limit=N Output at most *N* hosts (*-N* to output the last *N* hosts).