def successful_completed_process(p: subprocess.CompletedProcess): try: p.check_returncode() return True except subprocess.CalledProcessError: # print(f"{p.stderr}") return False
def run(self, input=None, stdin=None, stdout=None, stderr=None, timeout=None, check=False, encoding=None): """!Runs this command via subprocess.Pipe. Returns a CompletedProcess. Arguments have the same meaning as subprocess.run. """ for name, f in self.files.items(): mode = f.get('mode', 'wt') logger.info(f'{f["name"]}: write mode {mode}') with open(f['name'], mode) as fd: fd.write(str(f['content'])) env = None if self.env: env = dict(os.environ) env.update(self.env) logger.info(f'Popen {repr(self.command)}') pipe = Popen(args=self.command, stdin=stdin, stdout=stdout, stderr=stderr, encoding=encoding, cwd=self.cwd, env=env) (stdout, stderr) = pipe.communicate(input=input, timeout=timeout) cp = CompletedProcess(self.command, pipe.returncode, stdout, stderr) if check: cp.check_returncode() return cp
def _check_return_code(process: subprocess.CompletedProcess) -> None: """ Ensures process had 0 exit code, and fails """ try: process.check_returncode() except subprocess.CalledProcessError: raise SystemExit(f"\n\nCommand failed.")
def run(args, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs): """ Run the command described by `args`. Wait for command to complete and then return a CompletedProcess instance. The arguments are the same as the `Popen` constructor with ``capture_output``, ``timeout``, and ``check`` added. """ # Ensure we capture standard output and standard error if capture_output: stdout = PIPE stderr = PIPE # Execute the process proc = Popen(args=args, stdin=stdin, input=input, stdout=stdout, stderr=stderr, shell=shell, cwd=cwd, encoding=encoding, errors=errors, text=text, env=env, universal_newlines=universal_newlines, **other_popen_kwargs) # Send input/receive output stdout_data, stderr_data = proc.communicate(input, timeout) # Build the completed process object completed_proc = CompletedProcess(args, proc.returncode, stdout_data, stderr_data) # Check the result if check: completed_proc.check_returncode() return completed_proc
def check_completedprocess_for_acceptable_exit_codes( completed_process_obj:subprocess.CompletedProcess, acceptable_exit_codes:typing.Sequence[int]): ''' sees if a `subprocess.CompletedProcess` object has an acceptable exit code if not, we call subprocess.CompletedProcess.check_returncode() which will throw an exception @param completed_process_obj - the subprocess.CompletedProcess object to check @param acceptable_exit_codes - the list of integers of acceptable exit codes @throws `subprocess.CalledProcessError` if the exit code is not in the acceptable list ''' logger.debug("checking return code: acceptable: `%s`, CompletedProcess return code: `%s`", acceptable_exit_codes, completed_process_obj.returncode) # throw an exception if the exit code is not in our acceptble list # this assumes that 0 is always a valid exit code, since `check_returncode()` won't # throw an exception if the exit code is 0 if not completed_process_obj.returncode in acceptable_exit_codes: completed_process_obj.check_returncode()
def _check_git_error(cls, response: subprocess.CompletedProcess): try: response.check_returncode() except subprocess.CalledProcessError as error: raise GitError(error.stderr.decode('utf8'))