#!/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 types[type] = num constellation[site] = type print constellation['chi2'] def main(): """ Main execution """ # Read from stdin or file given as first arg if len(sys.argv) == 1: parse_hostnames(sys.stdin.readlines()) else: try: hosts_file = open(sys.argv[1]) parse_hostnames(hosts_file) except IOError, e: print "*** File Open error: ", e if __name__ == '__main__': main() Run it against a file like this: tdist1-chi2 tdist2-chi2 tlue1-chi2 tlue2-chi2 tnm1-chi2 tnm2-chi2 tpe1-chi2 tpe2-chi2 tpe3-chi2 tpe4-chi2 tpe5-chi2 tpe6-chi2 tpe7-chi2 tpe8-chi2 ugrid1-chi2 ugrid10-chi2 ugrid11-chi2 ugrid12-chi2 ugrid2-chi2 ugrid3-chi2 ugrid4-chi2 ugrid5-chi2 ugrid6-chi2 ugrid7-chi2 ugrid8-chi2 ugrid9-chi2 uweb1-chi2 uweb10-chi2 uweb11-chi2 uweb12-chi2 uweb13-chi2 uweb14-chi2 uweb2-chi2 uweb3-chi2 uweb4-chi2 uweb5-chi2 uweb6-chi2 uweb7-chi2 uweb8-chi2 uweb9-chi2