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