@@ -73,69 +73,39 @@ def expand(nodelist):
7373 :return: The expanded hostlist string.
7474 """
7575 node_list = nodelist .split (", " )
76- # print node_list
77-
7876 result_hostlist = []
77+
7978 for node in node_list :
8079 nodelist_match = r"(\w+-?)\[((,?[0-9]+-?,?-?){0,})\](.*)?"
81- if re .search (nodelist_match , node ):
82- match = re .search (nodelist_match , node )
80+ match = re .search (nodelist_match , node )
8381
84- # holds the ranges of nodes as a string
85- # now we can manipulate the string and cast it to a list of numbers
86- oldstr = str (match .group (2 ))
87- left_br = oldstr .replace ("[" , "" )
88- right_br = left_br .replace ("]" , "" )
89- num_list = right_br .split (',' )
82+ if match :
83+ prefix = match .group (1 ) # hostname prefix
84+ ranges_str = match .group (2 ) # the range of numbers within brackets
85+ suffix = match .group (4 ) or "" # optional suffix
9086
91- # if the node numbers contain leading zeros, store them to be
92- # prepended in the final list
93- final_list = []
94- lead_zeros = 0
95- lead_zeros_str = ''
96- for elem in num_list :
97- # if it is a range of numbers, break it by the hyphen and
98- # create a list
99- #
100- # will then be merged with final list
101- if '-' in elem :
102- tmp_list = elem .replace ("-" , "," ).split ("," )
103-
104- for digit in tmp_list [0 ]:
105- if digit == '0' :
106- lead_zeros = lead_zeros + 1
107- lead_zeros_str = lead_zeros_str + '0'
108- else :
109- break
110-
111- rng_list = range (int (tmp_list [0 ]), int (tmp_list [1 ]) + 1 )
112- final_list .extend (rng_list )
113- else :
114- final_list .append (int (elem ))
115-
116- # put final list in ascending order and append cluster name to
117- # each node number
118- final_list .sort ()
119-
120- # prepend leading zeros to numbers required
121- hostlist_tmp = []
122- for elem in final_list :
123- if ((lead_zeros > 0 ) and (len (str (elem )) <= len (lead_zeros_str ))):
124- hostlist_tmp .append (str (elem ).zfill (lead_zeros + 1 ))
87+ final_hostlist = []
88+ ranges = ranges_str .split (',' )
89+
90+ for rng in ranges :
91+ if '-' in rng :
92+ start , end = rng .split ('-' )
93+ # handle leading zeros
94+ width = len (start )
95+ start , end = int (start ), int (end )
96+ for i in range (start , end + 1 ):
97+ final_hostlist .append (f"{ prefix } { str (i ).zfill (width )} { suffix } " )
12598 else :
126- hostlist_tmp .append (str ( elem ) )
99+ final_hostlist .append (f" { prefix } { rng . zfill ( len ( rng )) } { suffix } " )
127100
128- # append hostname to the node numbers
129- hostlist_no_suffix = []
130- for elem in hostlist_tmp :
131- hostlist_no_suffix .append (match .group (1 ) + elem )
101+ # append sorted hostnames
102+ result_hostlist .extend (final_hostlist )
132103
133- # append suffix to hostlist if there is one
134- final_hostlist = []
135- for elem in hostlist_no_suffix :
136- final_hostlist .append (elem + match .group (4 ))
104+ # sort the entire hostlist numerically based on the numeric part
105+ def numeric_key (hostname ):
106+ return int (re .search (r'(\d+)' , hostname ).group (1 ))
137107
138- result_hostlist .append ( '%s' % ',' . join ( map ( str , final_hostlist )) )
108+ result_hostlist .sort ( key = numeric_key )
139109
140110 return ',' .join (result_hostlist )
141111
0 commit comments