Created
October 1, 2019 15:01
-
-
Save ebirn/cf52876120648d7d85501fcbf185ff07 to your computer and use it in GitHub Desktop.
magic slurm node list parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
… using EBNF grammar to parse - https://github.com/commonism/slurm_node_list_parser