]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - tools/patman/command.py
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2011 The Chromium OS Authors.
8 """Shell command ease-ups for Python."""
11 """A class which captures the result of executing a command.
14 stdout: stdout obtained from command, as a string
15 stderr: stderr obtained from command, as a string
16 return_code: Return code from command
17 exception: Exception received, or None if all ok
23 self
.return_code
= None
26 def __init__(self
, stdout
='', stderr
='', combined
='', return_code
=0,
30 self
.combined
= combined
31 self
.return_code
= return_code
32 self
.exception
= exception
35 # This permits interception of RunPipe for test purposes. If it is set to
36 # a function, then that function is called with the pipe list being
37 # executed. Otherwise, it is assumed to be a CommandResult object, and is
38 # returned as the result for every RunPipe() call.
39 # When this value is None, commands are executed as normal.
42 def RunPipe(pipe_list
, infile
=None, outfile
=None,
43 capture
=False, capture_stderr
=False, oneline
=False,
44 raise_on_error
=True, cwd
=None, **kwargs
):
46 Perform a command pipeline, with optional input/output filenames.
49 pipe_list: List of command lines to execute. Each command line is
50 piped into the next, and is itself a list of strings. For
51 example [ ['ls', '.git'] ['wc'] ] will pipe the output of
53 infile: File to provide stdin to the pipeline
54 outfile: File to store stdout
55 capture: True to capture output
56 capture_stderr: True to capture stderr
57 oneline: True to strip newline chars from output
58 kwargs: Additional keyword arguments to cros_subprocess.Popen()
63 if hasattr(test_result
, '__call__'):
64 return test_result(pipe_list
=pipe_list
)
66 result
= CommandResult()
68 pipeline
= list(pipe_list
)
69 user_pipestr
= '|'.join([' '.join(pipe
) for pipe
in pipe_list
])
70 kwargs
['stdout'] = None
71 kwargs
['stderr'] = None
74 if last_pipe
is not None:
75 kwargs
['stdin'] = last_pipe
.stdout
77 kwargs
['stdin'] = open(infile
, 'rb')
78 if pipeline
or capture
:
79 kwargs
['stdout'] = cros_subprocess
.PIPE
81 kwargs
['stdout'] = open(outfile
, 'wb')
83 kwargs
['stderr'] = cros_subprocess
.PIPE
86 last_pipe
= cros_subprocess
.Popen(cmd
, cwd
=cwd
, **kwargs
)
87 except Exception as err
:
88 result
.exception
= err
90 raise Exception("Error running '%s': %s" % (user_pipestr
, str))
91 result
.return_code
= 255
95 result
.stdout
, result
.stderr
, result
.combined
= (
96 last_pipe
.CommunicateFilter(None))
97 if result
.stdout
and oneline
:
98 result
.output
= result
.stdout
.rstrip('\r\n')
99 result
.return_code
= last_pipe
.wait()
101 result
.return_code
= os
.waitpid(last_pipe
.pid
, 0)[1]
102 if raise_on_error
and result
.return_code
:
103 raise Exception("Error running '%s'" % user_pipestr
)
106 def Output(*cmd
, **kwargs
):
107 raise_on_error
= kwargs
.get('raise_on_error', True)
108 return RunPipe([cmd
], capture
=True, raise_on_error
=raise_on_error
).stdout
110 def OutputOneLine(*cmd
, **kwargs
):
111 raise_on_error
= kwargs
.pop('raise_on_error', True)
112 return (RunPipe([cmd
], capture
=True, oneline
=True,
113 raise_on_error
=raise_on_error
,
114 **kwargs
).stdout
.strip())
116 def Run(*cmd
, **kwargs
):
117 return RunPipe([cmd
], **kwargs
).stdout
120 return RunPipe([cmd
], capture
=True).stdout
123 cros_subprocess
.stay_alive
= False