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