run_do_script.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Hans-Martin von Gaudecker, 2012
  4. """
  5. Run a Stata do-script in the directory specified by **ctx.bldnode**. The
  6. first and only argument will be the name of the do-script (no extension),
  7. which can be accessed inside the do-script by the local macro `1'. Useful
  8. for keeping a log file.
  9. The tool uses the log file that is automatically kept by Stata only
  10. for error-catching purposes, it will be destroyed if the task finished
  11. without error. In case of an error in **some_script.do**, you can inspect
  12. it as **some_script.log** in the **ctx.bldnode** directory.
  13. Note that Stata will not return an error code if it exits abnormally --
  14. catching errors relies on parsing the log file mentioned before. Should
  15. the parser behave incorrectly please send an email to hmgaudecker [at] gmail.
  16. **WARNING**
  17. The tool will not work if multiple do-scripts of the same name---but in
  18. different directories---are run at the same time! Avoid this situation.
  19. Usage::
  20. ctx(features='run_do_script',
  21. source='some_script.do',
  22. target=['some_table.tex', 'some_figure.eps'],
  23. deps='some_data.csv')
  24. """
  25. import os, re, sys
  26. from waflib import Task, TaskGen, Logs
  27. if sys.platform == 'darwin':
  28. STATA_COMMANDS = ['Stata64MP', 'StataMP',
  29. 'Stata64SE', 'StataSE',
  30. 'Stata64', 'Stata']
  31. STATAFLAGS = '-e -q do'
  32. STATAENCODING = 'MacRoman'
  33. elif sys.platform.startswith('linux'):
  34. STATA_COMMANDS = ['stata-mp', 'stata-se', 'stata']
  35. STATAFLAGS = '-b -q do'
  36. # Not sure whether this is correct...
  37. STATAENCODING = 'Latin-1'
  38. elif sys.platform.lower().startswith('win'):
  39. STATA_COMMANDS = ['StataMP-64', 'StataMP-ia',
  40. 'StataMP', 'StataSE-64',
  41. 'StataSE-ia', 'StataSE',
  42. 'Stata-64', 'Stata-ia',
  43. 'Stata.e', 'WMPSTATA',
  44. 'WSESTATA', 'WSTATA']
  45. STATAFLAGS = '/e do'
  46. STATAENCODING = 'Latin-1'
  47. else:
  48. raise Exception("Unknown sys.platform: %s " % sys.platform)
  49. def configure(ctx):
  50. ctx.find_program(STATA_COMMANDS, var='STATACMD', errmsg="""\n
  51. No Stata executable found!\n\n
  52. If Stata is needed:\n
  53. 1) Check the settings of your system path.
  54. 2) Note we are looking for Stata executables called: %s
  55. If yours has a different name, please report to hmgaudecker [at] gmail\n
  56. Else:\n
  57. Do not load the 'run_do_script' tool in the main wscript.\n\n""" % STATA_COMMANDS)
  58. ctx.env.STATAFLAGS = STATAFLAGS
  59. ctx.env.STATAENCODING = STATAENCODING
  60. class run_do_script_base(Task.Task):
  61. """Run a Stata do-script from the bldnode directory."""
  62. run_str = '"${STATACMD}" ${STATAFLAGS} "${SRC[0].abspath()}" "${DOFILETRUNK}"'
  63. shell = True
  64. class run_do_script(run_do_script_base):
  65. """Use the log file automatically kept by Stata for error-catching.
  66. Erase it if the task finished without error. If not, it will show
  67. up as do_script.log in the bldnode directory.
  68. """
  69. def run(self):
  70. run_do_script_base.run(self)
  71. ret, log_tail = self.check_erase_log_file()
  72. if ret:
  73. Logs.error("""Running Stata on %r failed with code %r.\n\nCheck the log file %s, last 10 lines\n\n%s\n\n\n""",
  74. self.inputs[0], ret, self.env.LOGFILEPATH, log_tail)
  75. return ret
  76. def check_erase_log_file(self):
  77. """Parse Stata's default log file and erase it if everything okay.
  78. Parser is based on Brendan Halpin's shell script found here:
  79. http://teaching.sociology.ul.ie/bhalpin/wordpress/?p=122
  80. """
  81. if sys.version_info.major >= 3:
  82. kwargs = {'file': self.env.LOGFILEPATH, 'mode': 'r', 'encoding': self.env.STATAENCODING}
  83. else:
  84. kwargs = {'name': self.env.LOGFILEPATH, 'mode': 'r'}
  85. with open(**kwargs) as log:
  86. log_tail = log.readlines()[-10:]
  87. for line in log_tail:
  88. error_found = re.match("r\(([0-9]+)\)", line)
  89. if error_found:
  90. return error_found.group(1), ''.join(log_tail)
  91. else:
  92. pass
  93. # Only end up here if the parser did not identify an error.
  94. os.remove(self.env.LOGFILEPATH)
  95. return None, None
  96. @TaskGen.feature('run_do_script')
  97. @TaskGen.before_method('process_source')
  98. def apply_run_do_script(tg):
  99. """Task generator customising the options etc. to call Stata in batch
  100. mode for running a do-script.
  101. """
  102. # Convert sources and targets to nodes
  103. src_node = tg.path.find_resource(tg.source)
  104. tgt_nodes = [tg.path.find_or_declare(t) for t in tg.to_list(tg.target)]
  105. tsk = tg.create_task('run_do_script', src=src_node, tgt=tgt_nodes)
  106. tsk.env.DOFILETRUNK = os.path.splitext(src_node.name)[0]
  107. tsk.env.LOGFILEPATH = os.path.join(tg.bld.bldnode.abspath(), '%s.log' % (tsk.env.DOFILETRUNK))
  108. # dependencies (if the attribute 'deps' changes, trigger a recompilation)
  109. for x in tg.to_list(getattr(tg, 'deps', [])):
  110. node = tg.path.find_resource(x)
  111. if not node:
  112. tg.bld.fatal('Could not find dependency %r for running %r' % (x, src_node.abspath()))
  113. tsk.dep_nodes.append(node)
  114. Logs.debug('deps: found dependencies %r for running %r', tsk.dep_nodes, src_node.abspath())
  115. # Bypass the execution of process_source by setting the source to an empty list
  116. tg.source = []