Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. |
| 2 | # |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | # |
| 5 | |
| 6 | import os |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 7 | import cros_subprocess |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 8 | |
| 9 | """Shell command ease-ups for Python.""" |
| 10 | |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 11 | class CommandResult: |
| 12 | """A class which captures the result of executing a command. |
| 13 | |
| 14 | Members: |
| 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 |
| 19 | """ |
| 20 | def __init__(self): |
| 21 | self.stdout = None |
| 22 | self.stderr = None |
| 23 | self.return_code = None |
| 24 | self.exception = None |
| 25 | |
| 26 | |
| 27 | def RunPipe(pipe_list, infile=None, outfile=None, |
| 28 | capture=False, capture_stderr=False, oneline=False, |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 29 | raise_on_error=True, cwd=None, **kwargs): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 30 | """ |
| 31 | Perform a command pipeline, with optional input/output filenames. |
| 32 | |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 33 | Args: |
| 34 | pipe_list: List of command lines to execute. Each command line is |
| 35 | piped into the next, and is itself a list of strings. For |
| 36 | example [ ['ls', '.git'] ['wc'] ] will pipe the output of |
| 37 | 'ls .git' into 'wc'. |
| 38 | infile: File to provide stdin to the pipeline |
| 39 | outfile: File to store stdout |
| 40 | capture: True to capture output |
| 41 | capture_stderr: True to capture stderr |
| 42 | oneline: True to strip newline chars from output |
| 43 | kwargs: Additional keyword arguments to cros_subprocess.Popen() |
| 44 | Returns: |
| 45 | CommandResult object |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 46 | """ |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 47 | result = CommandResult() |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 48 | last_pipe = None |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 49 | pipeline = list(pipe_list) |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 50 | user_pipestr = '|'.join([' '.join(pipe) for pipe in pipe_list]) |
Simon Glass | ddaf5c8 | 2014-09-05 19:00:09 -0600 | [diff] [blame] | 51 | kwargs['stdout'] = None |
| 52 | kwargs['stderr'] = None |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 53 | while pipeline: |
| 54 | cmd = pipeline.pop(0) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 55 | if last_pipe is not None: |
| 56 | kwargs['stdin'] = last_pipe.stdout |
| 57 | elif infile: |
| 58 | kwargs['stdin'] = open(infile, 'rb') |
| 59 | if pipeline or capture: |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 60 | kwargs['stdout'] = cros_subprocess.PIPE |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 61 | elif outfile: |
| 62 | kwargs['stdout'] = open(outfile, 'wb') |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 63 | if capture_stderr: |
| 64 | kwargs['stderr'] = cros_subprocess.PIPE |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 65 | |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 66 | try: |
| 67 | last_pipe = cros_subprocess.Popen(cmd, cwd=cwd, **kwargs) |
| 68 | except Exception, err: |
| 69 | result.exception = err |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 70 | if raise_on_error: |
| 71 | raise Exception("Error running '%s': %s" % (user_pipestr, str)) |
| 72 | result.return_code = 255 |
| 73 | return result |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 74 | |
| 75 | if capture: |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 76 | result.stdout, result.stderr, result.combined = ( |
| 77 | last_pipe.CommunicateFilter(None)) |
| 78 | if result.stdout and oneline: |
| 79 | result.output = result.stdout.rstrip('\r\n') |
| 80 | result.return_code = last_pipe.wait() |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 81 | else: |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 82 | result.return_code = os.waitpid(last_pipe.pid, 0)[1] |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 83 | if raise_on_error and result.return_code: |
| 84 | raise Exception("Error running '%s'" % user_pipestr) |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 85 | return result |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 86 | |
| 87 | def Output(*cmd): |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 88 | return RunPipe([cmd], capture=True, raise_on_error=False).stdout |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 89 | |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 90 | def OutputOneLine(*cmd, **kwargs): |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 91 | raise_on_error = kwargs.pop('raise_on_error', True) |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 92 | return (RunPipe([cmd], capture=True, oneline=True, |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 93 | raise_on_error=raise_on_error, |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 94 | **kwargs).stdout.strip()) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 95 | |
| 96 | def Run(*cmd, **kwargs): |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 97 | return RunPipe([cmd], **kwargs).stdout |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 98 | |
| 99 | def RunList(cmd): |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 100 | return RunPipe([cmd], capture=True).stdout |
| 101 | |
| 102 | def StopAll(): |
| 103 | cros_subprocess.stay_alive = False |