Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. |
| 3 | # |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | |
| 5 | import os |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 6 | import cros_subprocess |
Simon Glass | 3b3e3c0 | 2019-10-31 07:42:50 -0600 | [diff] [blame^] | 7 | import tools |
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 |
Simon Glass | 82012dd | 2014-09-05 19:00:12 -0600 | [diff] [blame] | 23 | self.combined = None |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 24 | self.return_code = None |
| 25 | self.exception = None |
| 26 | |
Simon Glass | 82012dd | 2014-09-05 19:00:12 -0600 | [diff] [blame] | 27 | def __init__(self, stdout='', stderr='', combined='', return_code=0, |
| 28 | exception=None): |
| 29 | self.stdout = stdout |
| 30 | self.stderr = stderr |
| 31 | self.combined = combined |
| 32 | self.return_code = return_code |
| 33 | self.exception = exception |
| 34 | |
Simon Glass | 3b3e3c0 | 2019-10-31 07:42:50 -0600 | [diff] [blame^] | 35 | def ToOutput(self, binary): |
| 36 | if not binary: |
| 37 | self.stdout = tools.ToString(self.stdout) |
| 38 | self.stderr = tools.ToString(self.stderr) |
| 39 | self.combined = tools.ToString(self.combined) |
| 40 | return self |
| 41 | |
Simon Glass | 82012dd | 2014-09-05 19:00:12 -0600 | [diff] [blame] | 42 | |
| 43 | # This permits interception of RunPipe for test purposes. If it is set to |
| 44 | # a function, then that function is called with the pipe list being |
| 45 | # executed. Otherwise, it is assumed to be a CommandResult object, and is |
| 46 | # returned as the result for every RunPipe() call. |
| 47 | # When this value is None, commands are executed as normal. |
| 48 | test_result = None |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 49 | |
| 50 | def RunPipe(pipe_list, infile=None, outfile=None, |
| 51 | capture=False, capture_stderr=False, oneline=False, |
Simon Glass | 3b3e3c0 | 2019-10-31 07:42:50 -0600 | [diff] [blame^] | 52 | raise_on_error=True, cwd=None, binary=False, **kwargs): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 53 | """ |
| 54 | Perform a command pipeline, with optional input/output filenames. |
| 55 | |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 56 | Args: |
| 57 | pipe_list: List of command lines to execute. Each command line is |
| 58 | piped into the next, and is itself a list of strings. For |
| 59 | example [ ['ls', '.git'] ['wc'] ] will pipe the output of |
| 60 | 'ls .git' into 'wc'. |
| 61 | infile: File to provide stdin to the pipeline |
| 62 | outfile: File to store stdout |
| 63 | capture: True to capture output |
| 64 | capture_stderr: True to capture stderr |
| 65 | oneline: True to strip newline chars from output |
| 66 | kwargs: Additional keyword arguments to cros_subprocess.Popen() |
| 67 | Returns: |
| 68 | CommandResult object |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 69 | """ |
Simon Glass | 82012dd | 2014-09-05 19:00:12 -0600 | [diff] [blame] | 70 | if test_result: |
| 71 | if hasattr(test_result, '__call__'): |
Simon Glass | 2b19321e | 2018-07-17 13:25:42 -0600 | [diff] [blame] | 72 | result = test_result(pipe_list=pipe_list) |
| 73 | if result: |
| 74 | return result |
| 75 | else: |
| 76 | return test_result |
| 77 | # No result: fall through to normal processing |
Simon Glass | 3b3e3c0 | 2019-10-31 07:42:50 -0600 | [diff] [blame^] | 78 | result = CommandResult(b'', b'', b'') |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 79 | last_pipe = None |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 80 | pipeline = list(pipe_list) |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 81 | user_pipestr = '|'.join([' '.join(pipe) for pipe in pipe_list]) |
Simon Glass | ddaf5c8 | 2014-09-05 19:00:09 -0600 | [diff] [blame] | 82 | kwargs['stdout'] = None |
| 83 | kwargs['stderr'] = None |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 84 | while pipeline: |
| 85 | cmd = pipeline.pop(0) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 86 | if last_pipe is not None: |
| 87 | kwargs['stdin'] = last_pipe.stdout |
| 88 | elif infile: |
| 89 | kwargs['stdin'] = open(infile, 'rb') |
| 90 | if pipeline or capture: |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 91 | kwargs['stdout'] = cros_subprocess.PIPE |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 92 | elif outfile: |
| 93 | kwargs['stdout'] = open(outfile, 'wb') |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 94 | if capture_stderr: |
| 95 | kwargs['stderr'] = cros_subprocess.PIPE |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 96 | |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 97 | try: |
| 98 | last_pipe = cros_subprocess.Popen(cmd, cwd=cwd, **kwargs) |
Paul Burton | ac3fde9 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 99 | except Exception as err: |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 100 | result.exception = err |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 101 | if raise_on_error: |
| 102 | raise Exception("Error running '%s': %s" % (user_pipestr, str)) |
| 103 | result.return_code = 255 |
Simon Glass | 3b3e3c0 | 2019-10-31 07:42:50 -0600 | [diff] [blame^] | 104 | return result.ToOutput(binary) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 105 | |
| 106 | if capture: |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 107 | result.stdout, result.stderr, result.combined = ( |
| 108 | last_pipe.CommunicateFilter(None)) |
| 109 | if result.stdout and oneline: |
Simon Glass | 3b3e3c0 | 2019-10-31 07:42:50 -0600 | [diff] [blame^] | 110 | result.output = result.stdout.rstrip(b'\r\n') |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 111 | result.return_code = last_pipe.wait() |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 112 | else: |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 113 | result.return_code = os.waitpid(last_pipe.pid, 0)[1] |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 114 | if raise_on_error and result.return_code: |
| 115 | raise Exception("Error running '%s'" % user_pipestr) |
Simon Glass | 3b3e3c0 | 2019-10-31 07:42:50 -0600 | [diff] [blame^] | 116 | return result.ToOutput(binary) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 117 | |
Simon Glass | 785f154 | 2016-07-25 18:59:00 -0600 | [diff] [blame] | 118 | def Output(*cmd, **kwargs): |
Simon Glass | 512f455 | 2019-07-08 13:18:23 -0600 | [diff] [blame] | 119 | kwargs['raise_on_error'] = kwargs.get('raise_on_error', True) |
| 120 | return RunPipe([cmd], capture=True, **kwargs).stdout |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 121 | |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 122 | def OutputOneLine(*cmd, **kwargs): |
Simon Glass | 3b3e3c0 | 2019-10-31 07:42:50 -0600 | [diff] [blame^] | 123 | """Run a command and output it as a single-line string |
| 124 | |
| 125 | The command us expected to produce a single line of output |
| 126 | |
| 127 | Returns: |
| 128 | String containing output of command |
| 129 | """ |
Simon Glass | dc19150 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 130 | raise_on_error = kwargs.pop('raise_on_error', True) |
Simon Glass | 3b3e3c0 | 2019-10-31 07:42:50 -0600 | [diff] [blame^] | 131 | result = RunPipe([cmd], capture=True, oneline=True, |
| 132 | raise_on_error=raise_on_error, **kwargs).stdout.strip() |
| 133 | return result |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 134 | |
| 135 | def Run(*cmd, **kwargs): |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 136 | return RunPipe([cmd], **kwargs).stdout |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 137 | |
| 138 | def RunList(cmd): |
Simon Glass | a10fd93 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 139 | return RunPipe([cmd], capture=True).stdout |
| 140 | |
| 141 | def StopAll(): |
| 142 | cros_subprocess.stay_alive = False |