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.

88 lines
2.7KB

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