1#!/usr/bin/env python 2# encoding: utf-8 3# Hans-Martin von Gaudecker, 2012 4 5""" 6Run a Matlab script. 7 8Note that the script is run in the directory where it lives -- Matlab won't 9allow it any other way. 10 11For error-catching purposes, keep an own log-file that is destroyed if the 12task finished without error. If not, it will show up as mscript_[index].log 13in the bldnode directory. 14 15Usage:: 16 17 ctx(features='run_m_script', 18 source='some_script.m', 19 target=['some_table.tex', 'some_figure.eps'], 20 deps='some_data.mat') 21""" 22 23import os, sys 24from waflib import Task, TaskGen, Logs 25 26MATLAB_COMMANDS = ['matlab'] 27 28def configure(ctx): 29 ctx.find_program(MATLAB_COMMANDS, var='MATLABCMD', errmsg = """\n 30No Matlab executable found!\n\n 31If Matlab is needed:\n 32 1) Check the settings of your system path. 33 2) Note we are looking for Matlab executables called: %s 34 If yours has a different name, please report to hmgaudecker [at] gmail\n 35Else:\n 36 Do not load the 'run_m_script' tool in the main wscript.\n\n""" % MATLAB_COMMANDS) 37 ctx.env.MATLABFLAGS = '-wait -nojvm -nosplash -minimize' 38 39class run_m_script_base(Task.Task): 40 """Run a Matlab script.""" 41 run_str = '"${MATLABCMD}" ${MATLABFLAGS} -logfile "${LOGFILEPATH}" -r "try, ${MSCRIPTTRUNK}, exit(0), catch err, disp(err.getReport()), exit(1), end"' 42 shell = True 43 44class run_m_script(run_m_script_base): 45 """Erase the Matlab overall log file if everything went okay, else raise an 46 error and print its 10 last lines. 47 """ 48 def run(self): 49 ret = run_m_script_base.run(self) 50 logfile = self.env.LOGFILEPATH 51 if ret: 52 mode = 'r' 53 if sys.version_info.major >= 3: 54 mode = 'rb' 55 with open(logfile, mode=mode) as f: 56 tail = f.readlines()[-10:] 57 Logs.error("""Running Matlab on %r returned the error %r\n\nCheck the log file %s, last 10 lines\n\n%s\n\n\n""", 58 self.inputs[0], ret, logfile, '\n'.join(tail)) 59 else: 60 os.remove(logfile) 61 return ret 62 63@TaskGen.feature('run_m_script') 64@TaskGen.before_method('process_source') 65def apply_run_m_script(tg): 66 """Task generator customising the options etc. to call Matlab in batch 67 mode for running a m-script. 68 """ 69 70 # Convert sources and targets to nodes 71 src_node = tg.path.find_resource(tg.source) 72 tgt_nodes = [tg.path.find_or_declare(t) for t in tg.to_list(tg.target)] 73 74 tsk = tg.create_task('run_m_script', src=src_node, tgt=tgt_nodes) 75 tsk.cwd = src_node.parent.abspath() 76 tsk.env.MSCRIPTTRUNK = os.path.splitext(src_node.name)[0] 77 tsk.env.LOGFILEPATH = os.path.join(tg.bld.bldnode.abspath(), '%s_%d.log' % (tsk.env.MSCRIPTTRUNK, tg.idx)) 78 79 # dependencies (if the attribute 'deps' changes, trigger a recompilation) 80 for x in tg.to_list(getattr(tg, 'deps', [])): 81 node = tg.path.find_resource(x) 82 if not node: 83 tg.bld.fatal('Could not find dependency %r for running %r' % (x, src_node.abspath())) 84 tsk.dep_nodes.append(node) 85 Logs.debug('deps: found dependencies %r for running %r', tsk.dep_nodes, src_node.abspath()) 86 87 # Bypass the execution of process_source by setting the source to an empty list 88 tg.source = [] 89