1# Copyright (c) 2011 The Chromium OS Authors. 2# 3# SPDX-License-Identifier: GPL-2.0+ 4# 5 6import os 7import cros_subprocess 8 9"""Shell command ease-ups for Python.""" 10 11class 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 23 self.combined = None 24 self.return_code = None 25 self.exception = None 26 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. 41test_result = None 42 43def RunPipe(pipe_list, infile=None, outfile=None, 44 capture=False, capture_stderr=False, oneline=False, 45 raise_on_error=True, cwd=None, **kwargs): 46 """ 47 Perform a command pipeline, with optional input/output filenames. 48 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 62 """ 63 if test_result: 64 if hasattr(test_result, '__call__'): 65 return test_result(pipe_list=pipe_list) 66 return test_result 67 result = CommandResult() 68 last_pipe = None 69 pipeline = list(pipe_list) 70 user_pipestr = '|'.join([' '.join(pipe) for pipe in pipe_list]) 71 kwargs['stdout'] = None 72 kwargs['stderr'] = None 73 while pipeline: 74 cmd = pipeline.pop(0) 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: 80 kwargs['stdout'] = cros_subprocess.PIPE 81 elif outfile: 82 kwargs['stdout'] = open(outfile, 'wb') 83 if capture_stderr: 84 kwargs['stderr'] = cros_subprocess.PIPE 85 86 try: 87 last_pipe = cros_subprocess.Popen(cmd, cwd=cwd, **kwargs) 88 except Exception as err: 89 result.exception = err 90 if raise_on_error: 91 raise Exception("Error running '%s': %s" % (user_pipestr, str)) 92 result.return_code = 255 93 return result 94 95 if capture: 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() 101 else: 102 result.return_code = os.waitpid(last_pipe.pid, 0)[1] 103 if raise_on_error and result.return_code: 104 raise Exception("Error running '%s'" % user_pipestr) 105 return result 106 107def Output(*cmd, **kwargs): 108 raise_on_error = kwargs.get('raise_on_error', True) 109 return RunPipe([cmd], capture=True, raise_on_error=raise_on_error).stdout 110 111def OutputOneLine(*cmd, **kwargs): 112 raise_on_error = kwargs.pop('raise_on_error', True) 113 return (RunPipe([cmd], capture=True, oneline=True, 114 raise_on_error=raise_on_error, 115 **kwargs).stdout.strip()) 116 117def Run(*cmd, **kwargs): 118 return RunPipe([cmd], **kwargs).stdout 119 120def RunList(cmd): 121 return RunPipe([cmd], capture=True).stdout 122 123def StopAll(): 124 cros_subprocess.stay_alive = False 125