2
0
mirror of https://gitlab.com/ita1024/waf.git synced 2024-12-01 22:40:46 +01:00
waf/waflib/extras/run_m_script.py

90 lines
3.0 KiB
Python
Raw Normal View History

2012-04-05 22:10:08 +02:00
#!/usr/bin/env python
# encoding: utf-8
# Hans-Martin von Gaudecker, 2012
"""
Run a Matlab script.
Note that the script is run in the directory where it lives -- Matlab won't
allow it any other way.
For error-catching purposes, keep an own log-file that is destroyed if the
2012-04-13 08:36:02 +02:00
task finished without error. If not, it will show up as mscript_[index].log
2012-04-05 22:10:08 +02:00
in the bldnode directory.
2012-04-13 08:36:02 +02:00
Usage::
ctx(features='run_m_script',
source='some_script.m',
target=['some_table.tex', 'some_figure.eps'],
deps='some_data.mat')
2012-04-05 22:10:08 +02:00
"""
2012-04-13 08:37:48 +02:00
import os, sys
2012-04-05 22:10:08 +02:00
from waflib import Task, TaskGen, Logs
MATLAB_COMMANDS = ['matlab']
def configure(ctx):
2012-04-13 08:36:02 +02:00
ctx.find_program(MATLAB_COMMANDS, var='MATLABCMD', errmsg = """\n
No Matlab executable found!\n\n
If Matlab is needed:\n
2012-04-05 22:10:08 +02:00
1) Check the settings of your system path.
2) Note we are looking for Matlab executables called: %s
If yours has a different name, please report to hmgaudecker [at] gmail\n
2012-04-13 08:36:02 +02:00
Else:\n
Do not load the 'run_m_script' tool in the main wscript.\n\n""" % MATLAB_COMMANDS)
2012-04-05 22:10:08 +02:00
ctx.env.MATLABFLAGS = '-wait -nojvm -nosplash -minimize'
@Task.update_outputs
2012-04-11 19:23:57 +02:00
class run_m_script_base(Task.Task):
2012-04-05 22:10:08 +02:00
"""Run a Matlab script."""
2012-04-11 19:05:46 +02:00
run_str = '"${MATLABCMD}" ${MATLABFLAGS} -logfile "${LOGFILEPATH}" -r "try, ${MSCRIPTTRUNK}, exit(0), catch err, disp(err.getReport()), exit(1), end"'
2012-04-05 22:10:08 +02:00
shell = True
2012-04-11 19:23:57 +02:00
class run_m_script(run_m_script_base):
"""Erase the Matlab overall log file if everything went okay, else raise an
error and print its 10 last lines.
"""
def run(self):
ret = run_m_script_base.run(self)
logfile = self.env.LOGFILEPATH
if ret:
mode = 'r'
if sys.version_info.major >= 3:
mode = 'rb'
with open(logfile, mode=mode) as f:
tail = f.readlines()[-10:]
2012-04-13 08:36:02 +02:00
Logs.error("""Running Matlab on %s returned the error %r\n\nCheck the log file %s, last 10 lines\n\n%s\n\n\n""" % (
2014-05-19 21:32:24 +02:00
self.inputs[0].abspath(), ret, logfile, '\n'.join(tail)))
2012-04-11 19:23:57 +02:00
else:
os.remove(logfile)
return ret
2012-04-05 22:10:08 +02:00
@TaskGen.feature('run_m_script')
@TaskGen.before_method('process_source')
def apply_run_m_script(tg):
2012-04-13 08:36:02 +02:00
"""Task generator customising the options etc. to call Matlab in batch
2012-04-11 19:05:46 +02:00
mode for running a m-script.
2012-04-05 22:10:08 +02:00
"""
# Convert sources and targets to nodes
src_node = tg.path.find_resource(tg.source)
tgt_nodes = [tg.path.find_or_declare(t) for t in tg.to_list(tg.target)]
2012-04-11 19:23:57 +02:00
tsk = tg.create_task('run_m_script', src=src_node, tgt=tgt_nodes)
2012-04-05 22:10:08 +02:00
tsk.cwd = src_node.parent.abspath()
tsk.env.MSCRIPTTRUNK = os.path.splitext(src_node.name)[0]
tsk.env.LOGFILEPATH = os.path.join(tg.bld.bldnode.abspath(), '%s_%d.log' % (tsk.env.MSCRIPTTRUNK, tg.idx))
# dependencies (if the attribute 'deps' changes, trigger a recompilation)
for x in tg.to_list(getattr(tg, 'deps', [])):
node = tg.path.find_resource(x)
if not node:
2014-05-19 21:32:24 +02:00
tg.bld.fatal('Could not find dependency %r for running %r' % (x, src_node.abspath()))
2012-04-10 18:30:10 +02:00
tsk.dep_nodes.append(node)
2014-05-19 21:32:24 +02:00
Logs.debug('deps: found dependencies %r for running %r' % (tsk.dep_nodes, src_node.abspath()))
2012-04-05 22:10:08 +02:00
# Bypass the execution of process_source by setting the source to an empty list
tg.source = []