Skip to content

Commit a6bae1f

Browse files
committed
Function for repeated code to allow MPI as root.
Fix indentation.
1 parent 79a3bf9 commit a6bae1f

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

TestCases/TestCase.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,25 @@ class Command:
5151
"""
5252

5353
def __init__(self, launch = "", exec = "", param = ""):
54-
self.launch = launch
55-
self.exec = exec
56-
self.param = param
54+
self.launch = launch
55+
self.exec = exec
56+
self.param = param
5757

5858
def empty(self):
59-
return self.launch == "" and self.exec == "" and self.param == ""
59+
return self.launch == "" and self.exec == "" and self.param == ""
6060

6161
def assemble(self):
62-
assert self.exec != ""
63-
return " ".join([part for part in [self.launch, self.exec, self.param] if part != ""])
62+
assert self.exec != ""
63+
return " ".join([part for part in [self.launch, self.exec, self.param] if part != ""])
64+
65+
def allow_mpi_as_root(self):
66+
if os.geteuid() == 0:
67+
if self.launch.startswith('mpirun'):
68+
self.launch = self.launch.replace('mpirun', 'mpirun --allow-run-as-root')
6469

6570
def killall(self):
66-
""" Issues a shell command that kills all processes matching self.exec, except this process. """
67-
os.system('pgrep %s | grep -vx %d | xargs kill -9' % (self.exec, os.getpid()))
71+
""" Issues a shell command that kills all processes matching self.exec, except this process. """
72+
os.system('pgrep %s | grep -vx %d | xargs kill -9' % (self.exec, os.getpid()))
6873

6974
def __init__(self,tag_in):
7075

@@ -118,9 +123,7 @@ def run_test(self):
118123
start_solver = True
119124

120125
# if root, add flag to mpirun
121-
if os.geteuid()==0:
122-
if self.command.launch.startswith('mpirun'):
123-
self.command.launch = self.command.launch.replace('mpirun', 'mpirun --allow-run-as-root')
126+
self.command.allow_mpi_as_root()
124127

125128
# Adjust the number of iterations in the config file
126129
if len(self.test_vals) != 0:
@@ -276,9 +279,7 @@ def run_filediff(self):
276279
self.adjust_test_data()
277280

278281
# if root, add flag to mpirun
279-
if os.geteuid()==0:
280-
if self.command.launch.startswith('mpirun'):
281-
self.command.launch = self.command.launch.replace('mpirun', 'mpirun --allow-run-as-root')
282+
self.command.allow_mpi_as_root()
282283

283284
# Assemble the shell command to run
284285
logfilename = '%s.log' % os.path.splitext(self.cfg_file)[0]
@@ -501,9 +502,7 @@ def run_geo(self):
501502
self.adjust_test_data()
502503

503504
# if root, add flag to mpirun
504-
if os.geteuid()==0:
505-
if self.command.launch.startswith('mpirun'):
506-
self.command.launch = self.command.launch.replace('mpirun', 'mpirun --allow-run-as-root')
505+
self.command.allow_mpi_as_root()
507506

508507
# Assemble the shell command to run SU2
509508
logfilename = '%s.log' % os.path.splitext(self.cfg_file)[0]
@@ -630,9 +629,7 @@ def run_def(self):
630629
self.adjust_test_data()
631630

632631
# if root, add flag to mpirun
633-
if os.geteuid()==0:
634-
if self.command.launch.startswith('mpirun'):
635-
self.command.launch = self.command.launch.replace('mpirun', 'mpirun --allow-run-as-root')
632+
self.command.allow_mpi_as_root()
636633

637634
# Assemble the shell command to run SU2
638635
logfilename = '%s.log' % os.path.splitext(self.cfg_file)[0]

0 commit comments

Comments
 (0)