1*0d24de9dSSimon Glass# Copyright (c) 2011 The Chromium OS Authors. 2*0d24de9dSSimon Glass# 3*0d24de9dSSimon Glass# See file CREDITS for list of people who contributed to this 4*0d24de9dSSimon Glass# project. 5*0d24de9dSSimon Glass# 6*0d24de9dSSimon Glass# This program is free software; you can redistribute it and/or 7*0d24de9dSSimon Glass# modify it under the terms of the GNU General Public License as 8*0d24de9dSSimon Glass# published by the Free Software Foundation; either version 2 of 9*0d24de9dSSimon Glass# the License, or (at your option) any later version. 10*0d24de9dSSimon Glass# 11*0d24de9dSSimon Glass# This program is distributed in the hope that it will be useful, 12*0d24de9dSSimon Glass# but WITHOUT ANY WARRANTY; without even the implied warranty of 13*0d24de9dSSimon Glass# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*0d24de9dSSimon Glass# GNU General Public License for more details. 15*0d24de9dSSimon Glass# 16*0d24de9dSSimon Glass# You should have received a copy of the GNU General Public License 17*0d24de9dSSimon Glass# along with this program; if not, write to the Free Software 18*0d24de9dSSimon Glass# Foundation, Inc., 59 Temple Place, Suite 330, Boston, 19*0d24de9dSSimon Glass# MA 02111-1307 USA 20*0d24de9dSSimon Glass# 21*0d24de9dSSimon Glass 22*0d24de9dSSimon Glassimport os 23*0d24de9dSSimon Glassimport subprocess 24*0d24de9dSSimon Glass 25*0d24de9dSSimon Glass"""Shell command ease-ups for Python.""" 26*0d24de9dSSimon Glass 27*0d24de9dSSimon Glassdef RunPipe(pipeline, infile=None, outfile=None, 28*0d24de9dSSimon Glass capture=False, oneline=False, hide_stderr=False): 29*0d24de9dSSimon Glass """ 30*0d24de9dSSimon Glass Perform a command pipeline, with optional input/output filenames. 31*0d24de9dSSimon Glass 32*0d24de9dSSimon Glass hide_stderr Don't allow output of stderr (default False) 33*0d24de9dSSimon Glass """ 34*0d24de9dSSimon Glass last_pipe = None 35*0d24de9dSSimon Glass while pipeline: 36*0d24de9dSSimon Glass cmd = pipeline.pop(0) 37*0d24de9dSSimon Glass kwargs = {} 38*0d24de9dSSimon Glass if last_pipe is not None: 39*0d24de9dSSimon Glass kwargs['stdin'] = last_pipe.stdout 40*0d24de9dSSimon Glass elif infile: 41*0d24de9dSSimon Glass kwargs['stdin'] = open(infile, 'rb') 42*0d24de9dSSimon Glass if pipeline or capture: 43*0d24de9dSSimon Glass kwargs['stdout'] = subprocess.PIPE 44*0d24de9dSSimon Glass elif outfile: 45*0d24de9dSSimon Glass kwargs['stdout'] = open(outfile, 'wb') 46*0d24de9dSSimon Glass if hide_stderr: 47*0d24de9dSSimon Glass kwargs['stderr'] = open('/dev/null', 'wb') 48*0d24de9dSSimon Glass 49*0d24de9dSSimon Glass last_pipe = subprocess.Popen(cmd, **kwargs) 50*0d24de9dSSimon Glass 51*0d24de9dSSimon Glass if capture: 52*0d24de9dSSimon Glass ret = last_pipe.communicate()[0] 53*0d24de9dSSimon Glass if not ret: 54*0d24de9dSSimon Glass return None 55*0d24de9dSSimon Glass elif oneline: 56*0d24de9dSSimon Glass return ret.rstrip('\r\n') 57*0d24de9dSSimon Glass else: 58*0d24de9dSSimon Glass return ret 59*0d24de9dSSimon Glass else: 60*0d24de9dSSimon Glass return os.waitpid(last_pipe.pid, 0)[1] == 0 61*0d24de9dSSimon Glass 62*0d24de9dSSimon Glassdef Output(*cmd): 63*0d24de9dSSimon Glass return RunPipe([cmd], capture=True) 64*0d24de9dSSimon Glass 65*0d24de9dSSimon Glassdef OutputOneLine(*cmd): 66*0d24de9dSSimon Glass return RunPipe([cmd], capture=True, oneline=True) 67*0d24de9dSSimon Glass 68*0d24de9dSSimon Glassdef Run(*cmd, **kwargs): 69*0d24de9dSSimon Glass return RunPipe([cmd], **kwargs) 70*0d24de9dSSimon Glass 71*0d24de9dSSimon Glassdef RunList(cmd): 72*0d24de9dSSimon Glass return RunPipe([cmd], capture=True) 73