]> git.ipfire.org Git - thirdparty/u-boot.git/blame - tools/patman/command.py
Add GPL-2.0+ SPDX-License-Identifier to source files
[thirdparty/u-boot.git] / tools / patman / command.py
CommitLineData
0d24de9d
SG
1# Copyright (c) 2011 The Chromium OS Authors.
2#
1a459660 3# SPDX-License-Identifier: GPL-2.0+
0d24de9d
SG
4#
5
6import os
a10fd93c 7import cros_subprocess
0d24de9d
SG
8
9"""Shell command ease-ups for Python."""
10
a10fd93c
SG
11class CommandResult:
12 """A class which captures the result of executing a command.
13
14 Members:
15 stdout: stdout obtained from command, as a string
16 stderr: stderr obtained from command, as a string
17 return_code: Return code from command
18 exception: Exception received, or None if all ok
19 """
20 def __init__(self):
21 self.stdout = None
22 self.stderr = None
23 self.return_code = None
24 self.exception = None
25
26
27def RunPipe(pipe_list, infile=None, outfile=None,
28 capture=False, capture_stderr=False, oneline=False,
dc191505 29 raise_on_error=True, cwd=None, **kwargs):
0d24de9d
SG
30 """
31 Perform a command pipeline, with optional input/output filenames.
32
a10fd93c
SG
33 Args:
34 pipe_list: List of command lines to execute. Each command line is
35 piped into the next, and is itself a list of strings. For
36 example [ ['ls', '.git'] ['wc'] ] will pipe the output of
37 'ls .git' into 'wc'.
38 infile: File to provide stdin to the pipeline
39 outfile: File to store stdout
40 capture: True to capture output
41 capture_stderr: True to capture stderr
42 oneline: True to strip newline chars from output
43 kwargs: Additional keyword arguments to cros_subprocess.Popen()
44 Returns:
45 CommandResult object
0d24de9d 46 """
a10fd93c 47 result = CommandResult()
0d24de9d 48 last_pipe = None
a10fd93c 49 pipeline = list(pipe_list)
dc191505 50 user_pipestr = '|'.join([' '.join(pipe) for pipe in pipe_list])
0d24de9d
SG
51 while pipeline:
52 cmd = pipeline.pop(0)
0d24de9d
SG
53 if last_pipe is not None:
54 kwargs['stdin'] = last_pipe.stdout
55 elif infile:
56 kwargs['stdin'] = open(infile, 'rb')
57 if pipeline or capture:
a10fd93c 58 kwargs['stdout'] = cros_subprocess.PIPE
0d24de9d
SG
59 elif outfile:
60 kwargs['stdout'] = open(outfile, 'wb')
a10fd93c
SG
61 if capture_stderr:
62 kwargs['stderr'] = cros_subprocess.PIPE
0d24de9d 63
a10fd93c
SG
64 try:
65 last_pipe = cros_subprocess.Popen(cmd, cwd=cwd, **kwargs)
66 except Exception, err:
67 result.exception = err
dc191505
SG
68 if raise_on_error:
69 raise Exception("Error running '%s': %s" % (user_pipestr, str))
70 result.return_code = 255
71 return result
0d24de9d
SG
72
73 if capture:
a10fd93c
SG
74 result.stdout, result.stderr, result.combined = (
75 last_pipe.CommunicateFilter(None))
76 if result.stdout and oneline:
77 result.output = result.stdout.rstrip('\r\n')
78 result.return_code = last_pipe.wait()
0d24de9d 79 else:
a10fd93c 80 result.return_code = os.waitpid(last_pipe.pid, 0)[1]
dc191505
SG
81 if raise_on_error and result.return_code:
82 raise Exception("Error running '%s'" % user_pipestr)
a10fd93c 83 return result
0d24de9d
SG
84
85def Output(*cmd):
dc191505 86 return RunPipe([cmd], capture=True, raise_on_error=False).stdout
0d24de9d 87
a10fd93c 88def OutputOneLine(*cmd, **kwargs):
dc191505 89 raise_on_error = kwargs.pop('raise_on_error', True)
a10fd93c 90 return (RunPipe([cmd], capture=True, oneline=True,
dc191505 91 raise_on_error=raise_on_error,
a10fd93c 92 **kwargs).stdout.strip())
0d24de9d
SG
93
94def Run(*cmd, **kwargs):
a10fd93c 95 return RunPipe([cmd], **kwargs).stdout
0d24de9d
SG
96
97def RunList(cmd):
a10fd93c
SG
98 return RunPipe([cmd], capture=True).stdout
99
100def StopAll():
101 cros_subprocess.stay_alive = False