#!/usr/bin/env python
from esys.lsm.util import InstallInfo
from esys.lsm.util import PathSearcher

import os
import sys

moduleName = sys.argv[1]
exec("import " + moduleName)
moduleFileName = eval(moduleName + ".__file__")

#
# LAM mpiexec needs a -machinefile option to tell it
# on which hosts to boot LAM. Run unit tests it on the localhost.
#
machineFileName = os.tmpnam() + "_lam_hosts.txt"
f=file(machineFileName, "w")
f.write(os.uname()[1])
f.close()

mpiexec = PathSearcher().find("mpiexec")
os.execv(
  mpiexec,
  [
    mpiexec,                           # argv[0]
    "-machinefile", machineFileName,
    "-np", "1",                        # Start one MPI process and
                                       # the workers get spawned.
    os.path.join(InstallInfo.binDir, "mpipython"),
    moduleFileName
  ] +\
  sys.argv[2:]
)
