def run_command_print(command, print_on_success=False): """ Simliar to run_command but prints each line of output on the fly. """ output = [] env = os.environ.copy() env['LC_ALL'] = 'C' p = None try: p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env, universal_newlines=True, shell=True) except OSError as e: status = e.errno output = e.strerror msgs = [ "Error to run command: %s\n" % command, "Error code: %s\n" % e.errno, "Error description: %s\n" % e.strerror, ] error_out(msgs, die=False) raise RunCommandException(command, status, "\n".join(output)) for line in run_subprocess(p): line = line.rstrip('\n') print(line) output.append(line) print("\n") status = p.poll() if status > 0: msgs = [ "Error running command: %s\n" % command, "Status code: %s\n" % status, "Command output: %s\n" % output, ] error_out(msgs, die=False) raise RunCommandException(command, status, "\n".join(output)) elif print_on_success: print("Command: %s\n" % command) print("Status code: %s\n" % status) print("Command output: %s\n" % output) else: debug("Command: %s\n" % command) debug("Status code: %s\n" % status) debug("Command output: %s\n" % output) return '\n'.join(output)
def run_command(command): debug(command) (status, output) = commands.getstatusoutput(command) if status > 0: sys.stderr.write("\n########## ERROR ############\n") sys.stderr.write("Error running command: %s\n" % command) sys.stderr.write("Status code: %s\n" % status) sys.stderr.write("Command output: %s\n" % output) raise RunCommandException("Error running command", command, status, output) return output
def run_command_print(command): """ Simliar to run_command but prints each line of output on the fly. TODO: make this work in both python2 and python3. """ output = [] env = os.environ.copy() env['LC_ALL'] = 'C' p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) for line in run_subprocess(p): print(line), output.append(line.rstrip('\n')) print("\n"), if p.poll() > 0: raise RunCommandException(command, p.poll(), "\n".join(output)) return '\n'.join(output)
def run_command(command, print_on_success=False): """ Run command. If command fails, print status code and command output. """ (status, output) = getstatusoutput(command) if status > 0: sys.stderr.write("\n########## ERROR ############\n") sys.stderr.write("Error running command: %s\n" % command) sys.stderr.write("Status code: %s\n" % status) sys.stderr.write("Command output: %s\n" % output) raise RunCommandException(command, status, output) elif print_on_success: print("Command: %s\n" % command) print("Status code: %s\n" % status) print("Command output: %s\n" % output) return output
def run_command_print(command): """ Simliar to run_command but prints each line of output on the fly. """ output = [] env = os.environ.copy() env['LC_ALL'] = 'C' p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env, universal_newlines=True) for line in run_subprocess(p): line = line.rstrip('\n') print(line) output.append(line) print("\n"), if p.poll() > 0: raise RunCommandException(command, p.poll(), "\n".join(output)) return '\n'.join(output)
def run_command(command, print_on_success=False): """ Run command. If command fails, print status code and command output. """ (status, output) = getstatusoutput(command) if status > 0: msgs = [ "Error running command: %s\n" % command, "Status code: %s\n" % status, "Command output: %s\n" % output, ] error_out(msgs, die=False) raise RunCommandException(command, status, output) elif print_on_success: print("Command: %s\n" % command) print("Status code: %s\n" % status) print("Command output: %s\n" % output) return output