File: print_commands.py

package info (click to toggle)
xmds2 3.0.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 52,068 kB
  • sloc: python: 63,652; javascript: 9,230; cpp: 3,929; ansic: 1,463; makefile: 121; sh: 54
file content (45 lines) | stat: -rw-r--r-- 1,054 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#! /usr/bin/env python3

"""
Illustrate how to override a class method to do something

In this case, print the commands being executed as strings
(the commands are usually lists, so this can be misleading)
"""

from waflib import Context, Utils, Logs

def exec_command(self, cmd, **kw):
	subprocess = Utils.subprocess
	kw['shell'] = isinstance(cmd, str)

	txt = cmd
	if isinstance(cmd, list):
		txt = ' '.join(cmd)

	print(txt)
	Logs.debug('runner_env: kw=%s' % kw)

	try:
		if self.logger:
			# warning: may deadlock with a lot of output (subprocess limitation)

			self.logger.info(cmd)

			kw['stdout'] = kw['stderr'] = subprocess.PIPE
			p = subprocess.Popen(cmd, **kw)
			(out, err) = p.communicate()
			if out:
				self.logger.debug('out: %s' % out.decode(sys.stdout.encoding or 'iso8859-1'))
			if err:
				self.logger.error('err: %s' % err.decode(sys.stdout.encoding or 'iso8859-1'))
			return p.returncode
		else:
			p = subprocess.Popen(cmd, **kw)
			return p.wait()
	except OSError:
		return -1

Context.Context.exec_command = exec_command