jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.0KB

  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. @Task.update_outputs
  31. class run_m_script_base(Task.Task):
  32. """Run a Matlab script."""
  33. run_str = '"${MATLABCMD}" ${MATLABFLAGS} -logfile "${LOGFILEPATH}" -r "try, ${MSCRIPTTRUNK}, exit(0), catch err, disp(err.getReport()), exit(1), end"'
  34. shell = True
  35. class run_m_script(run_m_script_base):
  36. """Erase the Matlab overall log file if everything went okay, else raise an
  37. error and print its 10 last lines.
  38. """
  39. def run(self):
  40. ret = run_m_script_base.run(self)
  41. logfile = self.env.LOGFILEPATH
  42. if ret:
  43. mode = 'r'
  44. if sys.version_info.major >= 3:
  45. mode = 'rb'
  46. with open(logfile, mode=mode) as f:
  47. tail = f.readlines()[-10:]
  48. 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""" % (
  49. self.inputs[0].abspath(), ret, logfile, '\n'.join(tail)))
  50. else:
  51. os.remove(logfile)
  52. return ret
  53. @TaskGen.feature('run_m_script')
  54. @TaskGen.before_method('process_source')
  55. def apply_run_m_script(tg):
  56. """Task generator customising the options etc. to call Matlab in batch
  57. mode for running a m-script.
  58. """
  59. # Convert sources and targets to nodes
  60. src_node = tg.path.find_resource(tg.source)
  61. tgt_nodes = [tg.path.find_or_declare(t) for t in tg.to_list(tg.target)]
  62. tsk = tg.create_task('run_m_script', src=src_node, tgt=tgt_nodes)
  63. tsk.cwd = src_node.parent.abspath()
  64. tsk.env.MSCRIPTTRUNK = os.path.splitext(src_node.name)[0]
  65. tsk.env.LOGFILEPATH = os.path.join(tg.bld.bldnode.abspath(), '%s_%d.log' % (tsk.env.MSCRIPTTRUNK, tg.idx))
  66. # dependencies (if the attribute 'deps' changes, trigger a recompilation)
  67. for x in tg.to_list(getattr(tg, 'deps', [])):
  68. node = tg.path.find_resource(x)
  69. if not node:
  70. tg.bld.fatal('Could not find dependency %r for running %r' % (x, src_node.abspath()))
  71. tsk.dep_nodes.append(node)
  72. Logs.debug('deps: found dependencies %r for running %r' % (tsk.dep_nodes, src_node.abspath()))
  73. # Bypass the execution of process_source by setting the source to an empty list
  74. tg.source = []