Skip to content

Commit 79c3c13

Browse files
committed
Fix Matlab
1 parent 27fec7e commit 79c3c13

5 files changed

Lines changed: 21 additions & 54 deletions

File tree

sumatra/core.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_encoding():
5151
return encoding
5252

5353

54-
def run(args, cwd=None, shell=False, kill_tree=True, timeout=-1, env=None):
54+
def run(args, cwd=None, shell=False, kill_tree=True, timeout=None, env=None):
5555
"""
5656
Run a command with a timeout.
5757
"""
@@ -65,9 +65,8 @@ def run(args, cwd=None, shell=False, kill_tree=True, timeout=-1, env=None):
6565

6666

6767
def _get_process_children(pid):
68-
p = subprocess.Popen('ps --no-headers -o pid --ppid %d' % pid, shell=True,
69-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
70-
stdout, stderr = p.communicate()
68+
completed_command = subprocess.run(['ps','--no-headers', '-o', 'pid', '--ppid', pid], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
69+
stdout = completed_command.stdout
7170
return [int(child_pid) for child_pid in stdout.split()]
7271

7372

sumatra/dependency_finder/matlab.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ def __init__(self, module_name, path, version='unknown', diff='', source=None):
2121
super(Dependency, self).__init__(module_name, path, version, diff, source)
2222

2323

24-
def save_dependencies(cmd, filename):
25-
''' save all dependencies to the file in the current folder '''
26-
file_dep = "depfun %s -toponly -quiet -print depfun.data;" %filename # save dependencies to a file
27-
mat_args = cmd.split('-r ')[-1]
28-
cmd = "%s; %s quit" %(mat_args, file_dep)
29-
p = subprocess.Popen(['matlab','-nodesktop', '-nosplash', '-nojvm', ' -nodisplay', '-wait', '-r', cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
30-
result = p.wait()
31-
output = p.stdout.read()
32-
# import pdb; pdb.set_trace()
33-
return result, output
34-
35-
3624
def find_dependencies(filename, executable):
3725
#ifile = os.path.join(os.getcwd(), 'depfun.data')
3826
with open('depfun.data', 'r') as file_data:

sumatra/launch.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ def pre_run(self, executable):
7676
"""Run tasks before the simulation/analysis proper.""" # e.g. nrnivmodl
7777
# this implementation is a temporary hack. "pre_run" should probably be an Executable instance, not a string
7878
if hasattr(executable, "pre_run"):
79-
p = subprocess.Popen(executable.pre_run, shell=True, stdout=None,
80-
stderr=None, close_fds=True, cwd=self.working_directory)
81-
result = p.wait()
79+
completed_command = subprocess.run(executable.pre_run, shell=True, stdout=None, stderr=None, close_fds=True, cwd=self.working_directory)
80+
result = completed_command.returncode
8281

8382
def check_files(self, executable, main_file):
8483
"""Check that all files exist and are accessible."""
@@ -95,16 +94,16 @@ def run(self, executable, main_file, arguments, append_label=None, capture_stder
9594
command line. Return resultcode.
9695
"""
9796
self.check_files(executable, main_file)
98-
cmd = self.generate_command(executable, main_file, arguments)
97+
cmd = executable.generate_command(main_file, arguments)
9998
if append_label:
100-
cmd += " " + append_label
101-
if 'matlab' in executable.name.lower():
99+
cmd = [*cmd, append_label]
100+
if isinstance(executable, MatlabExecutable):
102101
''' we will be executing Matlab and at the same time saving the
103102
dependencies in order to avoid opening of Matlab shell two times '''
104-
result, output = save_dependencies(cmd, main_file)
105-
else:
106-
result, output = tee.system2(cmd, cwd=self.working_directory, stdout=True, capture_stderr=capture_stderr) # cwd only relevant for local launch, not for MPI, for example
107-
self.stdout_stderr = "".join(output)
103+
cmd[-1] = cmd[-1] + ';deps = matlab.codetools.requiredFilesAndProducts(\'%s\', \'toponly\');writelines(deps, \'depfun.data\');' % main_file
104+
105+
result, output = tee.system2(cmd, cwd=self.working_directory, stdout=True, capture_stderr=capture_stderr) # cwd only relevant for local launch, not for MPI, for example
106+
self.stdout_stderr = output
108107
return result
109108

110109
def __key(self):

sumatra/programs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ def _find_executable(self, executable_name):
100100
print('Multiple versions found, using %s. If you wish to use a different version, please specify it explicitly' % executable)
101101
return executable
102102

103+
def generate_command(self, main_file, arguments):
104+
return [self.path, main_file, arguments]
105+
103106
def _get_version(self):
104107
returncode, output, err = run([self.path, "--version"],
105108
shell=False, timeout=5)
@@ -173,6 +176,8 @@ def _get_version(self):
173176
command_line_output=output + err,
174177
pattern=version_pattern_matlab
175178
)
179+
def generate_command(self, main_file, arguments):
180+
return [self.path, '-batch', main_file.replace('.m','')]
176181

177182

178183
@component

sumatra/tee.py

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -137,35 +137,11 @@ def nop(msg):
137137
# reason: if I have 'quote_command' Sumatra does not work in Windows (it encloses the command in quotes. I did not understand why should we quote)
138138
# I have never catched "The input line is too long" (yet?)
139139
# cmd = quote_command(cmd)
140-
p = subprocess.Popen(cmd, cwd=cwd, shell=True, stdout=subprocess.PIPE, stderr=stderr, close_fds=(platform.system() == 'Linux'))
141140
if(log_command):
142141
mylogger("Running: %s" % cmd)
143-
try:
144-
while True:
145-
try:
146-
line = p.stdout.readline()
147-
line = line.decode(encoding)
148-
except Exception as e:
149-
logging.error(e)
150-
logging.error("The output of the command could not be decoded as %s\ncmd: %s\n line ignored: %s" %\
151-
(encoding, cmd, repr(line)))
152-
pass
153-
154-
output.append(line)
155-
if not line:
156-
break
157-
#line = line.rstrip('\n\r')
158-
mylogger(line.rstrip('\n\r')) # they are added by logging anyway
159-
#import pdb; pdb.set_trace()
160-
if(stdout):
161-
print(line, end="")
162-
sys.stdout.flush()
163-
returncode = p.wait()
164-
except KeyboardInterrupt:
165-
# Popen.returncode:
166-
# "A negative value -N indicates that the child was terminated by signal N (Unix only)."
167-
# see https://docs.python.org/2/library/subprocess.html#subprocess.Popen.returncode
168-
returncode = -signal.SIGINT
142+
completed_command = subprocess.run(cmd, cwd=cwd, shell=False, stdout=subprocess.PIPE, stderr=stderr, close_fds=(platform.system() == 'Linux'));
143+
returncode = completed_command.returncode
144+
output = completed_command.stdout
169145
if(log_command):
170146
if(timing):
171147
def secondsToStr(t):
@@ -176,7 +152,7 @@ def secondsToStr(t):
176152
mylogger("Returned: %d\n" % (returncode))
177153

178154
if not returncode == 0: # running a tool that returns non-zero? this deserves a warning
179-
logging.warning("Returned: %d from: %s\nOutput %s" % (returncode, cmd, ''.join(output)))
155+
logging.warning("Returned: %d from: %s\nOutput %s" % (returncode, cmd, output))
180156

181157
return(returncode, output)
182158

0 commit comments

Comments
 (0)