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,
25 """Shell command ease-ups for Python."""
27 def RunPipe(pipeline, infile=None, outfile=None,
28 capture=False, oneline=False, hide_stderr=False):
30 Perform a command pipeline, with optional input/output filenames.
32 hide_stderr Don't allow output of stderr (default False)
38 if last_pipe is not None:
39 kwargs['stdin'] = last_pipe.stdout
41 kwargs['stdin'] = open(infile, 'rb')
42 if pipeline or capture:
43 kwargs['stdout'] = subprocess.PIPE
45 kwargs['stdout'] = open(outfile, 'wb')
47 kwargs['stderr'] = open('/dev/null', 'wb')
49 last_pipe = subprocess.Popen(cmd, **kwargs)
52 ret = last_pipe.communicate()[0]
56 return ret.rstrip('\r\n')
60 return os.waitpid(last_pipe.pid, 0)[1] == 0
63 return RunPipe([cmd], capture=True)
65 def OutputOneLine(*cmd):
66 return RunPipe([cmd], capture=True, oneline=True)
68 def Run(*cmd, **kwargs):
69 return RunPipe([cmd], **kwargs)
72 return RunPipe([cmd], capture=True)