Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. |
| 3 | # |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | |
| 5 | import os |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 6 | import cros_subprocess |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 7 | |
| 8 | """Shell command ease-ups for Python.""" |
| 9 | |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 10 | class CommandResult: |
| 11 | """A class which captures the result of executing a command. |
| 12 | |
| 13 | Members: |
| 14 | stdout: stdout obtained from command, as a string |
| 15 | stderr: stderr obtained from command, as a string |
| 16 | return_code: Return code from command |
| 17 | exception: Exception received, or None if all ok |
| 18 | """ |
| 19 | def __init__(self): |
| 20 | self.stdout = None |
| 21 | self.stderr = None |
Simon Glass | de38497 | 2014-09-05 19:00:12 -0600 | [diff] [blame] | 22 | self.combined = None |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 23 | self.return_code = None |
| 24 | self.exception = None |
| 25 | |
Simon Glass | de38497 | 2014-09-05 19:00:12 -0600 | [diff] [blame] | 26 | def __init__(self, stdout='', stderr='', combined='', return_code=0, |
| 27 | exception=None): |
| 28 | self.stdout = stdout |
| 29 | self.stderr = stderr |
| 30 | self.combined = combined |
| 31 | self.return_code = return_code |
| 32 | self.exception = exception |
| 33 | |
| 34 | |
| 35 | # This permits interception of RunPipe for test purposes. If it is set to |
| 36 | # a function, then that function is called with the pipe list being |
| 37 | # executed. Otherwise, it is assumed to be a CommandResult object, and is |
| 38 | # returned as the result for every RunPipe() call. |
| 39 | # When this value is None, commands are executed as normal. |
| 40 | test_result = None |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 41 | |
| 42 | def RunPipe(pipe_list, infile=None, outfile=None, |
| 43 | capture=False, capture_stderr=False, oneline=False, |
Simon Glass | 519fad2 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 44 | raise_on_error=True, cwd=None, **kwargs): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 45 | """ |
| 46 | Perform a command pipeline, with optional input/output filenames. |
| 47 | |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 48 | Args: |
| 49 | pipe_list: List of command lines to execute. Each command line is |
| 50 | piped into the next, and is itself a list of strings. For |
| 51 | example [ ['ls', '.git'] ['wc'] ] will pipe the output of |
| 52 | 'ls .git' into 'wc'. |
| 53 | infile: File to provide stdin to the pipeline |
| 54 | outfile: File to store stdout |
| 55 | capture: True to capture output |
| 56 | capture_stderr: True to capture stderr |
| 57 | oneline: True to strip newline chars from output |
| 58 | kwargs: Additional keyword arguments to cros_subprocess.Popen() |
| 59 | Returns: |
| 60 | CommandResult object |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 61 | """ |
Simon Glass | de38497 | 2014-09-05 19:00:12 -0600 | [diff] [blame] | 62 | if test_result: |
| 63 | if hasattr(test_result, '__call__'): |
| 64 | return test_result(pipe_list=pipe_list) |
| 65 | return test_result |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 66 | result = CommandResult() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 67 | last_pipe = None |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 68 | pipeline = list(pipe_list) |
Simon Glass | 519fad2 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 69 | user_pipestr = '|'.join([' '.join(pipe) for pipe in pipe_list]) |
Simon Glass | f1bf686 | 2014-09-05 19:00:09 -0600 | [diff] [blame] | 70 | kwargs['stdout'] = None |
| 71 | kwargs['stderr'] = None |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 72 | while pipeline: |
| 73 | cmd = pipeline.pop(0) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 74 | if last_pipe is not None: |
| 75 | kwargs['stdin'] = last_pipe.stdout |
| 76 | elif infile: |
| 77 | kwargs['stdin'] = open(infile, 'rb') |
| 78 | if pipeline or capture: |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 79 | kwargs['stdout'] = cros_subprocess.PIPE |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 80 | elif outfile: |
| 81 | kwargs['stdout'] = open(outfile, 'wb') |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 82 | if capture_stderr: |
| 83 | kwargs['stderr'] = cros_subprocess.PIPE |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 84 | |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 85 | try: |
| 86 | last_pipe = cros_subprocess.Popen(cmd, cwd=cwd, **kwargs) |
Paul Burton | f14a131 | 2016-09-27 16:03:51 +0100 | [diff] [blame] | 87 | except Exception as err: |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 88 | result.exception = err |
Simon Glass | 519fad2 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 89 | if raise_on_error: |
| 90 | raise Exception("Error running '%s': %s" % (user_pipestr, str)) |
| 91 | result.return_code = 255 |
| 92 | return result |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 93 | |
| 94 | if capture: |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 95 | result.stdout, result.stderr, result.combined = ( |
| 96 | last_pipe.CommunicateFilter(None)) |
| 97 | if result.stdout and oneline: |
| 98 | result.output = result.stdout.rstrip('\r\n') |
| 99 | result.return_code = last_pipe.wait() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 100 | else: |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 101 | result.return_code = os.waitpid(last_pipe.pid, 0)[1] |
Simon Glass | 519fad2 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 102 | if raise_on_error and result.return_code: |
| 103 | raise Exception("Error running '%s'" % user_pipestr) |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 104 | return result |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 105 | |
Simon Glass | c55e056 | 2016-07-25 18:59:00 -0600 | [diff] [blame] | 106 | def Output(*cmd, **kwargs): |
| 107 | raise_on_error = kwargs.get('raise_on_error', True) |
| 108 | return RunPipe([cmd], capture=True, raise_on_error=raise_on_error).stdout |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 109 | |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 110 | def OutputOneLine(*cmd, **kwargs): |
Simon Glass | 519fad2 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 111 | raise_on_error = kwargs.pop('raise_on_error', True) |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 112 | return (RunPipe([cmd], capture=True, oneline=True, |
Simon Glass | 519fad2 | 2012-12-15 10:42:05 +0000 | [diff] [blame] | 113 | raise_on_error=raise_on_error, |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 114 | **kwargs).stdout.strip()) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 115 | |
| 116 | def Run(*cmd, **kwargs): |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 117 | return RunPipe([cmd], **kwargs).stdout |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 118 | |
| 119 | def RunList(cmd): |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame] | 120 | return RunPipe([cmd], capture=True).stdout |
| 121 | |
| 122 | def StopAll(): |
| 123 | cros_subprocess.stay_alive = False |