enable to explicitly set the environment

required for running MPI processes when mpi4py is imported
This commit is contained in:
Martin Diehl 2019-06-07 17:51:27 +02:00
parent 4ef66b4e28
commit cede188e76
2 changed files with 6 additions and 3 deletions

View File

@ -267,7 +267,7 @@ class Test():
logging.critical('Current2Current: Unable to copy file "{}"'.format(f))
raise
def execute_inCurrentDir(self,cmd,streamIn=None):
def execute_inCurrentDir(self,cmd,streamIn=None,env=None):
logging.info(cmd)
out,error = damask.util.execute(cmd,streamIn,self.dirCurrent())

View File

@ -97,14 +97,17 @@ def strikeout(what):
# -----------------------------
def execute(cmd,
streamIn = None,
wd = './'):
wd = './',
env = None):
"""Executes a command in given directory and returns stdout and stderr for optional stdin"""
initialPath = os.getcwd()
os.chdir(wd)
myEnv = os.environ if env is None else env
process = subprocess.Popen(shlex.split(cmd),
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
stdin = subprocess.PIPE)
stdin = subprocess.PIPE,
env = myEnv)
out,error = [i for i in (process.communicate() if streamIn is None
else process.communicate(streamIn.read().encode('utf-8')))]
out = out.decode('utf-8').replace('\x08','')