Skip to content

Commit 2e5a142

Browse files
committed
fix hostlist_expression for different padding lengths
1 parent a28e84d commit 2e5a142

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

filter_plugins/slurm_conf.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from ansible import errors
1919
import jinja2
2020
import re
21+
import pprint
2122

2223
# Pattern to match a hostname with numerical ending
2324
pattern = re.compile("^(.*\D(?=\d))(\d+)$")
@@ -49,6 +50,8 @@ def hostlist_expression(hosts):
4950
Then "{{ groups[compute] | hostlist_expression }}" will return:
5051
5152
['dev-foo-[00,04-05,3]', 'dev-compute-[000-001]', 'my-random-host']
53+
54+
NB: This does not guranteed to return parts in the same order as `scontrol hostlist`, but its output should return the same hosts when passed to `scontrol hostnames`.
5255
"""
5356

5457
results = {}
@@ -57,24 +60,25 @@ def hostlist_expression(hosts):
5760
m = pattern.match(v)
5861
if m:
5962
prefix, suffix = m.groups()
60-
print(prefix,suffix)
6163
r = results.setdefault(prefix, [])
6264
r.append(suffix)
6365
else:
6466
unmatchable.append(v)
6567
return ['{}[{}]'.format(k, _group_numbers(v)) for k, v in results.items()] + unmatchable
6668

6769
def _group_numbers(numbers):
68-
print('numbers:', sorted(numbers))
6970
units = []
70-
prev = min(int(n) for n in numbers)
71-
for v in sorted(numbers):
72-
if int(v) == prev + 1:
73-
units[-1].append(v)
71+
ints = [int(n) for n in numbers]
72+
lengths = [len(n) for n in numbers]
73+
# sort numbers by int value and length:
74+
ints, lengths, numbers = zip(*sorted(zip(ints, lengths, numbers)))
75+
prev = min(ints)
76+
for i, v in enumerate(sorted(ints)):
77+
if v == prev + 1:
78+
units[-1].append(numbers[i])
7479
else:
75-
units.append([v])
76-
print('units:', units)
77-
prev = int(v)
80+
units.append([numbers[i]])
81+
prev = v
7882
return ','.join(['{}-{}'.format(u[0], u[-1]) if len(u) > 1 else str(u[0]) for u in units])
7983

8084
def error(condition, msg):

0 commit comments

Comments
 (0)