#!/usr/bin/python import sys, os, re ############################################ ## APP SPECIFIC DEFINES ## # # Usage when (-h, --help) is used, this is the first line showing how to call the app # optionGroups which option groups from etc/options to import. common is always imported. App = "mdb-merge.py" Usage = "usage: %prog [options] command simulationname" optionGroups = [] Purpose = "merge scriptFile run into each mdb entry" ############################################ ############################################ ## INIT ## if os.path.basename(sys.argv[0]) == App: rp = os.path.realpath(__file__) paths = rp.split(os.sep) paths.pop() paths.pop() global BASE_PATH BASE_PATH = os.sep.join(paths) sys.path.append(BASE_PATH) #print "BASE_PATH: %s" % BASE_PATH from lib import * def getCmd(scriptFile): global BASE_PATH filename = "%s/etc/scriptfiles/%s" % (BASE_PATH, scriptFile) full_command = str() try: fptr = open(filename, "r") except: #print "could not open %s for reading" % filename return full_command contents = fptr.read() lines = contents.split("\n") mpichdir = str() cmd = str() mpilist = ['MPICHDIR', 'MPICH_DIR', 'OPENMPI_DIR'] for mpidef in mpilist: if contents.count(mpidef) > 0: for line in lines: line = line.strip() try: if line.count("CACTUS_START") > 0: if len(mpichdir) > 0: mpichdir = "%s && %s" % (mpichdir, line) else: mpichdir = line continue except: pass try: if line.count("%s=" % mpidef) > 0: if len(mpichdir) > 0: mpichdir = "%s && %s" % (mpichdir, line) else: mpichdir = line continue except: pass try: if line.index("time") == 0: if line.count("psrun") > 0: continue cmd = line break except: pass try: if line.index("${%s}" % mpidef) == 0: if line.count('run') > 0: cmd = line break except: pass if len(mpichdir) > 0: full_command = "%s && %s" % (mpichdir, cmd) else: full_command = cmd break alt_commands = ['/poe', 'srun', 'aprun', 'mpirun', 'ibrun', 'mpiexec'] for alt in alt_commands: if contents.count(alt) > 0: for line in lines: line = line.strip() try: if line.count("CACTUS_START") > 0: if len(mpichdir) == 0: mpichdir = "%s %s" % (mpichdir, line) else: mpichdir = line except: pass try: if line.count(alt) > 0: cmd = line except: pass if len(mpichdir) > 0: full_command = "%s && %s" % (mpichdir, cmd) else: full_command = cmd return full_command def main(): global BASE_PATH parser = pyini.IniParser("%s/etc/mdb.ini" % BASE_PATH) scriptdir = "%s/etc/scriptfiles" machines = parser.GetSections() cmds = dict() for machine in machines: scriptfile = parser.GetOption(machine, 'scriptfile').Value cmd = getCmd(scriptfile).strip() cmds[machine] = cmd fptr = open("%s/etc/mdb.ini" % BASE_PATH, "r") lines = fptr.readlines() machine = None nary = list() offset = 0 for i in range(len(lines)): line = lines[i].strip() nary.append(line) rr = "^\[([\w]+)\]$" rx = re.compile(rr) results = rx.match(line) if results != None: if machine != None: cmd = cmds[machine] nary.insert((i+offset)-3, "mpirun = %s\n" % cmd) offset = offset + 1 machine = None #print "inserting %s at line %s" % (cmd, i-5) machine = results.group(1) #print "[%s]: line %s" % (machine, i) if machine != None: #print "machine is not none..." cmd = cmds[machine] #print "cmd is %s" % cmd nary.insert((i+offset)-3, "mpirun = %s\n" % cmd) machine = None #print "inserting %s at line %s" % (cmd, i-5) for i in range(len(nary)): print "%s" % nary[i] main()