Skip to content

Instantly share code, notes, and snippets.

@ebirn
Created October 1, 2019 15:01
Show Gist options
  • Save ebirn/cf52876120648d7d85501fcbf185ff07 to your computer and use it in GitHub Desktop.
Save ebirn/cf52876120648d7d85501fcbf185ff07 to your computer and use it in GitHub Desktop.
magic slurm node list parser
#!/usr/bin/env python
import re
node_lists = [
"clip-g1-[0-1],clip-g2-[2-3]",
"clip-g1-0,clip-g2-0",
"clip-g1-0,clip-g2-1",
"clip-g1-1",
"clip-a-[1,3,5]",
"clip-b-[1-3,5]",
"clip-c-[1-3,5,9-12]",
"clip-d-[5,9-12]",
"clip-e-[5,9],clip-e-[15-19]",
"clip-f-[5,9],clip-f-[15,17]",
"clip-f-5,clip-f-[15,17]",
"clip-f-[5,9],clip-f-175"
]
# we assume index parts are sane
def expand_idx(idx):
if idx.startswith('['):
idx = idx[1:-1]
idx_parts = idx.split(',')
indexes = []
for part in idx_parts:
if "-" in part:
start, stop = part.split('-', 2)
indexes.extend(range(int(start), int(stop)+1))
else:
indexes.append(int(part))
indexes.sort()
return indexes
def expand_nodestr(nodestri, idx_list):
return [nodestri + str(idx) for idx in idx_list]
index_pat = re.compile(r'((clip-(\w+)-)(\d+|\[[\d\,\-]+\]),?)')
for node_str in node_lists:
print node_str
for match in index_pat.finditer(node_str):
print " ", match.groups()
#full = match.group(1)
prefix = match.group(2)
node_type = match.group(3)
idx = match.group(4)
idx_list = expand_idx(idx)
print "prefix> ", prefix
print "type> ", node_type
print "idx> ", idx
print "count> ", len(idx_list)
print "list> ", expand_nodestr(prefix, idx_list)
~
@commonism
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment