build:wafsamba: Remove unnecessary parameters to cmd_and_log
[bbaumbach/samba-autobuild/.git] / third_party / waf / waflib / extras / print_commands.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
7 """
8 Illustrate how to override a class method to do something
9
10 In this case, print the commands being executed as strings
11 (the commands are usually lists, so this can be misleading)
12 """
13
14 import sys
15 from waflib import Context, Utils, Errors, Logs
16
17 def exec_command(self, cmd, **kw):
18         subprocess = Utils.subprocess
19         kw['shell'] = isinstance(cmd, str)
20
21         if isinstance(cmd, str):
22                 kw['shell'] = True
23                 txt = cmd
24         else:
25                 txt = ' '.join(repr(x) if ' ' in x else x for x in cmd)
26
27         Logs.debug('runner: %s', txt)
28         Logs.debug('runner_env: kw=%s', kw)
29
30         if self.logger:
31                 self.logger.info(cmd)
32
33         if 'stdout' not in kw:
34                 kw['stdout'] = subprocess.PIPE
35         if 'stderr' not in kw:
36                 kw['stderr'] = subprocess.PIPE
37
38         if Logs.verbose and not kw['shell'] and not Utils.check_exe(cmd[0]):
39                 raise Errors.WafError("Program %s not found!" % cmd[0])
40
41         wargs = {}
42         if 'timeout' in kw:
43                 if kw['timeout'] is not None:
44                         wargs['timeout'] = kw['timeout']
45                 del kw['timeout']
46         if 'input' in kw:
47                 if kw['input']:
48                         wargs['input'] = kw['input']
49                         kw['stdin'] = Utils.subprocess.PIPE
50                 del kw['input']
51
52         if 'cwd' in kw:
53                 if not isinstance(kw['cwd'], str):
54                         kw['cwd'] = kw['cwd'].abspath()
55
56         try:
57                 if kw['stdout'] or kw['stderr']:
58                         p = subprocess.Popen(cmd, **kw)
59                         (out, err) = p.communicate(**wargs)
60                         ret = p.returncode
61                 else:
62                         out, err = (None, None)
63                         ret = subprocess.Popen(cmd, **kw).wait(**wargs)
64         except Exception ,e:
65                 raise Errors.WafError('Execution failure: %s' % str(e), ex=e)
66
67         if out:
68                 if not isinstance(out, str):
69                         out = out.decode(sys.stdout.encoding or 'iso8859-1')
70                 if self.logger:
71                         self.logger.debug('out: %s' % out)
72                 else:
73                         Logs.info(out, extra={'stream':sys.stdout, 'c1': ''})
74         if err:
75                 if not isinstance(err, str):
76                         err = err.decode(sys.stdout.encoding or 'iso8859-1')
77                 if self.logger:
78                         self.logger.error('err: %s' % err)
79                 else:
80                         Logs.info(err, extra={'stream':sys.stderr, 'c1': ''})
81
82         return ret
83
84 Context.Context.exec_command = exec_command