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