Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. |
| 2 | # |
| 3 | # See file CREDITS for list of people who contributed to this |
| 4 | # project. |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or |
| 7 | # modify it under the terms of the GNU General Public License as |
| 8 | # published by the Free Software Foundation; either version 2 of |
| 9 | # the License, or (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | # MA 02111-1307 USA |
| 20 | # |
| 21 | |
| 22 | import os |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 23 | import cros_subprocess |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 24 | |
| 25 | """Shell command ease-ups for Python.""" |
| 26 | |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 27 | class CommandResult: |
| 28 | """A class which captures the result of executing a command. |
| 29 | |
| 30 | Members: |
| 31 | stdout: stdout obtained from command, as a string |
| 32 | stderr: stderr obtained from command, as a string |
| 33 | return_code: Return code from command |
| 34 | exception: Exception received, or None if all ok |
| 35 | """ |
| 36 | def __init__(self): |
| 37 | self.stdout = None |
| 38 | self.stderr = None |
| 39 | self.return_code = None |
| 40 | self.exception = None |
| 41 | |
| 42 | |
| 43 | def RunPipe(pipe_list, infile=None, outfile=None, |
| 44 | capture=False, capture_stderr=False, oneline=False, |
| 45 | 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 | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 63 | result = CommandResult() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 64 | last_pipe = None |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 65 | pipeline = list(pipe_list) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 66 | while pipeline: |
| 67 | cmd = pipeline.pop(0) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 68 | if last_pipe is not None: |
| 69 | kwargs['stdin'] = last_pipe.stdout |
| 70 | elif infile: |
| 71 | kwargs['stdin'] = open(infile, 'rb') |
| 72 | if pipeline or capture: |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 73 | kwargs['stdout'] = cros_subprocess.PIPE |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 74 | elif outfile: |
| 75 | kwargs['stdout'] = open(outfile, 'wb') |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 76 | if capture_stderr: |
| 77 | kwargs['stderr'] = cros_subprocess.PIPE |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 78 | |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 79 | try: |
| 80 | last_pipe = cros_subprocess.Popen(cmd, cwd=cwd, **kwargs) |
| 81 | except Exception, err: |
| 82 | result.exception = err |
| 83 | print 'exception', pipe_list, err |
| 84 | raise Exception("Error running '%s': %s" % (pipe_list, str)) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 85 | |
| 86 | if capture: |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 87 | result.stdout, result.stderr, result.combined = ( |
| 88 | last_pipe.CommunicateFilter(None)) |
| 89 | if result.stdout and oneline: |
| 90 | result.output = result.stdout.rstrip('\r\n') |
| 91 | result.return_code = last_pipe.wait() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 92 | else: |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 93 | result.return_code = os.waitpid(last_pipe.pid, 0)[1] |
| 94 | if result.return_code: |
| 95 | raise Exception("Error running '%s'" % pipe_list) |
| 96 | return result |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 97 | |
| 98 | def Output(*cmd): |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 99 | return RunPipe([cmd], capture=True).stdout |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 100 | |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 101 | def OutputOneLine(*cmd, **kwargs): |
| 102 | return (RunPipe([cmd], capture=True, oneline=True, |
| 103 | **kwargs).stdout.strip()) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 104 | |
| 105 | def Run(*cmd, **kwargs): |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 106 | return RunPipe([cmd], **kwargs).stdout |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 107 | |
| 108 | def RunList(cmd): |
Simon Glass | 34e5943 | 2012-12-15 10:42:04 +0000 | [diff] [blame^] | 109 | return RunPipe([cmd], capture=True).stdout |
| 110 | |
| 111 | def StopAll(): |
| 112 | cros_subprocess.stay_alive = False |