#!/usr/bin/env python
# generate .mpc config
import sys, re
def parse_hostnames(hosts_file):
"""Read from stdin or a file given as the first command line argument
and parse through a list of hosts that use the hostname convention
typenum-site
"""
constellation = {}
types = {}
for hostname in hosts_file:
hostname = hostname.strip()
host_site = hostname.split('-', 1)
if len(host_site) != 2:
continue
host, site = host_site
type_num_pattern = re.compile(r"([A-Za-z_]+)([0-9]+)")
type_num = type_num_pattern.match(host)
type, num = type_num.groups()
print site, type, num
# how should this work?
#types[type] = num
#constellation[site] = types
if __name__ == '__main__':
if len(sys.argv) == 1:
parse_hostnames(sys.stdin.readlines())
else:
parse_hostnames(open(sys.argv[1]).readlines())
Run this against a file or stdin with this input...
dist1-cn
dist2-cn
jnm1-cn
jnm2-cn
jns1-cn
jns2-cn
jns3-cn
jns4-cn
nm1-cn
nm2-cn
nr1-cn
ns1-cn
ns2-cn
pe1-cn