10d24de9dSSimon Glass# Copyright (c) 2011 The Chromium OS Authors. 20d24de9dSSimon Glass# 3*1a459660SWolfgang Denk# SPDX-License-Identifier: GPL-2.0+ 40d24de9dSSimon Glass# 50d24de9dSSimon Glass 60d24de9dSSimon Glassimport os 7a10fd93cSSimon Glassimport cros_subprocess 80d24de9dSSimon Glass 90d24de9dSSimon Glass"""Shell command ease-ups for Python.""" 100d24de9dSSimon Glass 11a10fd93cSSimon Glassclass CommandResult: 12a10fd93cSSimon Glass """A class which captures the result of executing a command. 13a10fd93cSSimon Glass 14a10fd93cSSimon Glass Members: 15a10fd93cSSimon Glass stdout: stdout obtained from command, as a string 16a10fd93cSSimon Glass stderr: stderr obtained from command, as a string 17a10fd93cSSimon Glass return_code: Return code from command 18a10fd93cSSimon Glass exception: Exception received, or None if all ok 19a10fd93cSSimon Glass """ 20a10fd93cSSimon Glass def __init__(self): 21a10fd93cSSimon Glass self.stdout = None 22a10fd93cSSimon Glass self.stderr = None 23a10fd93cSSimon Glass self.return_code = None 24a10fd93cSSimon Glass self.exception = None 25a10fd93cSSimon Glass 26a10fd93cSSimon Glass 27a10fd93cSSimon Glassdef RunPipe(pipe_list, infile=None, outfile=None, 28a10fd93cSSimon Glass capture=False, capture_stderr=False, oneline=False, 29dc191505SSimon Glass raise_on_error=True, cwd=None, **kwargs): 300d24de9dSSimon Glass """ 310d24de9dSSimon Glass Perform a command pipeline, with optional input/output filenames. 320d24de9dSSimon Glass 33a10fd93cSSimon Glass Args: 34a10fd93cSSimon Glass pipe_list: List of command lines to execute. Each command line is 35a10fd93cSSimon Glass piped into the next, and is itself a list of strings. For 36a10fd93cSSimon Glass example [ ['ls', '.git'] ['wc'] ] will pipe the output of 37a10fd93cSSimon Glass 'ls .git' into 'wc'. 38a10fd93cSSimon Glass infile: File to provide stdin to the pipeline 39a10fd93cSSimon Glass outfile: File to store stdout 40a10fd93cSSimon Glass capture: True to capture output 41a10fd93cSSimon Glass capture_stderr: True to capture stderr 42a10fd93cSSimon Glass oneline: True to strip newline chars from output 43a10fd93cSSimon Glass kwargs: Additional keyword arguments to cros_subprocess.Popen() 44a10fd93cSSimon Glass Returns: 45a10fd93cSSimon Glass CommandResult object 460d24de9dSSimon Glass """ 47a10fd93cSSimon Glass result = CommandResult() 480d24de9dSSimon Glass last_pipe = None 49a10fd93cSSimon Glass pipeline = list(pipe_list) 50dc191505SSimon Glass user_pipestr = '|'.join([' '.join(pipe) for pipe in pipe_list]) 510d24de9dSSimon Glass while pipeline: 520d24de9dSSimon Glass cmd = pipeline.pop(0) 530d24de9dSSimon Glass if last_pipe is not None: 540d24de9dSSimon Glass kwargs['stdin'] = last_pipe.stdout 550d24de9dSSimon Glass elif infile: 560d24de9dSSimon Glass kwargs['stdin'] = open(infile, 'rb') 570d24de9dSSimon Glass if pipeline or capture: 58a10fd93cSSimon Glass kwargs['stdout'] = cros_subprocess.PIPE 590d24de9dSSimon Glass elif outfile: 600d24de9dSSimon Glass kwargs['stdout'] = open(outfile, 'wb') 61a10fd93cSSimon Glass if capture_stderr: 62a10fd93cSSimon Glass kwargs['stderr'] = cros_subprocess.PIPE 630d24de9dSSimon Glass 64a10fd93cSSimon Glass try: 65a10fd93cSSimon Glass last_pipe = cros_subprocess.Popen(cmd, cwd=cwd, **kwargs) 66a10fd93cSSimon Glass except Exception, err: 67a10fd93cSSimon Glass result.exception = err 68dc191505SSimon Glass if raise_on_error: 69dc191505SSimon Glass raise Exception("Error running '%s': %s" % (user_pipestr, str)) 70dc191505SSimon Glass result.return_code = 255 71dc191505SSimon Glass return result 720d24de9dSSimon Glass 730d24de9dSSimon Glass if capture: 74a10fd93cSSimon Glass result.stdout, result.stderr, result.combined = ( 75a10fd93cSSimon Glass last_pipe.CommunicateFilter(None)) 76a10fd93cSSimon Glass if result.stdout and oneline: 77a10fd93cSSimon Glass result.output = result.stdout.rstrip('\r\n') 78a10fd93cSSimon Glass result.return_code = last_pipe.wait() 790d24de9dSSimon Glass else: 80a10fd93cSSimon Glass result.return_code = os.waitpid(last_pipe.pid, 0)[1] 81dc191505SSimon Glass if raise_on_error and result.return_code: 82dc191505SSimon Glass raise Exception("Error running '%s'" % user_pipestr) 83a10fd93cSSimon Glass return result 840d24de9dSSimon Glass 850d24de9dSSimon Glassdef Output(*cmd): 86dc191505SSimon Glass return RunPipe([cmd], capture=True, raise_on_error=False).stdout 870d24de9dSSimon Glass 88a10fd93cSSimon Glassdef OutputOneLine(*cmd, **kwargs): 89dc191505SSimon Glass raise_on_error = kwargs.pop('raise_on_error', True) 90a10fd93cSSimon Glass return (RunPipe([cmd], capture=True, oneline=True, 91dc191505SSimon Glass raise_on_error=raise_on_error, 92a10fd93cSSimon Glass **kwargs).stdout.strip()) 930d24de9dSSimon Glass 940d24de9dSSimon Glassdef Run(*cmd, **kwargs): 95a10fd93cSSimon Glass return RunPipe([cmd], **kwargs).stdout 960d24de9dSSimon Glass 970d24de9dSSimon Glassdef RunList(cmd): 98a10fd93cSSimon Glass return RunPipe([cmd], capture=True).stdout 99a10fd93cSSimon Glass 100a10fd93cSSimon Glassdef StopAll(): 101a10fd93cSSimon Glass cros_subprocess.stay_alive = False 102