]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/patman/command.py
1 # Copyright (c) 2011 The Chromium OS Authors.
3 # SPDX-License-Identifier: GPL-2.0+
9 """Shell command ease-ups for Python."""
12 """A class which captures the result of executing a command.
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
24 self
.return_code
= None
27 def __init__(self
, stdout
='', stderr
='', combined
='', return_code
=0,
31 self
.combined
= combined
32 self
.return_code
= return_code
33 self
.exception
= exception
36 # This permits interception of RunPipe for test purposes. If it is set to
37 # a function, then that function is called with the pipe list being
38 # executed. Otherwise, it is assumed to be a CommandResult object, and is
39 # returned as the result for every RunPipe() call.
40 # When this value is None, commands are executed as normal.
43 def RunPipe(pipe_list
, infile
=None, outfile
=None,
44 capture
=False, capture_stderr
=False, oneline
=False,
45 raise_on_error
=True, cwd
=None, **kwargs
):
47 Perform a command pipeline, with optional input/output filenames.
50 pipe_list: List of command lines to execute. Each command line is
51 piped into the next, and is itself a list of strings. For
52 example [ ['ls', '.git'] ['wc'] ] will pipe the output of
54 infile: File to provide stdin to the pipeline
55 outfile: File to store stdout
56 capture: True to capture output
57 capture_stderr: True to capture stderr
58 oneline: True to strip newline chars from output
59 kwargs: Additional keyword arguments to cros_subprocess.Popen()
64 if hasattr(test_result
, '__call__'):
65 return test_result(pipe_list
=pipe_list
)
67 result
= CommandResult()
69 pipeline
= list(pipe_list
)
70 user_pipestr
= '|'.join([' '.join(pipe
) for pipe
in pipe_list
])
71 kwargs
['stdout'] = None
72 kwargs
['stderr'] = None
75 if last_pipe
is not None:
76 kwargs
['stdin'] = last_pipe
.stdout
78 kwargs
['stdin'] = open(infile
, 'rb')
79 if pipeline
or capture
:
80 kwargs
['stdout'] = cros_subprocess
.PIPE
82 kwargs
['stdout'] = open(outfile
, 'wb')
84 kwargs
['stderr'] = cros_subprocess
.PIPE
87 last_pipe
= cros_subprocess
.Popen(cmd
, cwd
=cwd
, **kwargs
)
88 except Exception, err
:
89 result
.exception
= err
91 raise Exception("Error running '%s': %s" % (user_pipestr
, str))
92 result
.return_code
= 255
96 result
.stdout
, result
.stderr
, result
.combined
= (
97 last_pipe
.CommunicateFilter(None))
98 if result
.stdout
and oneline
:
99 result
.output
= result
.stdout
.rstrip('\r\n')
100 result
.return_code
= last_pipe
.wait()
102 result
.return_code
= os
.waitpid(last_pipe
.pid
, 0)[1]
103 if raise_on_error
and result
.return_code
:
104 raise Exception("Error running '%s'" % user_pipestr
)
107 def Output(*cmd
, **kwargs
):
108 raise_on_error
= kwargs
.get('raise_on_error', True)
109 return RunPipe([cmd
], capture
=True, raise_on_error
=raise_on_error
).stdout
111 def OutputOneLine(*cmd
, **kwargs
):
112 raise_on_error
= kwargs
.pop('raise_on_error', True)
113 return (RunPipe([cmd
], capture
=True, oneline
=True,
114 raise_on_error
=raise_on_error
,
115 **kwargs
).stdout
.strip())
117 def Run(*cmd
, **kwargs
):
118 return RunPipe([cmd
], **kwargs
).stdout
121 return RunPipe([cmd
], capture
=True).stdout
124 cros_subprocess
.stay_alive
= False