run_m_script.py 3.0 KB

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