1 # Copyright (c) 2011 The Chromium OS Authors.
3 # See file CREDITS for list of people who contributed to this
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of
9 # the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 import cros_subprocess
25 """Shell command ease-ups for Python."""
28 """A class which captures the result of executing a command.
31 stdout: stdout obtained from command, as a string
32 stderr: stderr obtained from command, as a string
33 return_code: Return code from command
34 exception: Exception received, or None if all ok
39 self.return_code = None
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()
63 result = CommandResult()
65 pipeline = list(pipe_list)
66 user_pipestr = '|'.join([' '.join(pipe) for pipe in pipe_list])
69 if last_pipe is not None:
70 kwargs['stdin'] = last_pipe.stdout
72 kwargs['stdin'] = open(infile, 'rb')
73 if pipeline or capture:
74 kwargs['stdout'] = cros_subprocess.PIPE
76 kwargs['stdout'] = open(outfile, 'wb')
78 kwargs['stderr'] = cros_subprocess.PIPE
81 last_pipe = cros_subprocess.Popen(cmd, cwd=cwd, **kwargs)
82 except Exception, err:
83 result.exception = err
85 raise Exception("Error running '%s': %s" % (user_pipestr, str))
86 result.return_code = 255
90 result.stdout, result.stderr, result.combined = (
91 last_pipe.CommunicateFilter(None))
92 if result.stdout and oneline:
93 result.output = result.stdout.rstrip('\r\n')
94 result.return_code = last_pipe.wait()
96 result.return_code = os.waitpid(last_pipe.pid, 0)[1]
97 if raise_on_error and result.return_code:
98 raise Exception("Error running '%s'" % user_pipestr)
102 return RunPipe([cmd], capture=True, raise_on_error=False).stdout
104 def OutputOneLine(*cmd, **kwargs):
105 raise_on_error = kwargs.pop('raise_on_error', True)
106 return (RunPipe([cmd], capture=True, oneline=True,
107 raise_on_error=raise_on_error,
108 **kwargs).stdout.strip())
110 def Run(*cmd, **kwargs):
111 return RunPipe([cmd], **kwargs).stdout
114 return RunPipe([cmd], capture=True).stdout
117 cros_subprocess.stay_alive = False