run shell command @param cmd: command to execute @param timeout: timeout for command execution @return: (return code from command, command output)
(cmd, timeout=None, window=None)
| 61 | |
| 62 | |
| 63 | def runCommand(cmd, timeout=None, window=None): |
| 64 | """ run shell command |
| 65 | |
| 66 | @param cmd: command to execute |
| 67 | @param timeout: timeout for command execution |
| 68 | |
| 69 | @return: (return code from command, command output) |
| 70 | """ |
| 71 | |
| 72 | p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 73 | output = '' |
| 74 | for line in p.stdout: |
| 75 | line = line.decode(errors='replace' if (sys.version_info) < (3, 5) |
| 76 | else 'backslashreplace').rstrip() |
| 77 | output += line |
| 78 | print(line) |
| 79 | if window: |
| 80 | window.Refresh() |
| 81 | |
| 82 | retval = p.wait(timeout) |
| 83 | |
| 84 | return (retval, output) |
| 85 | |
| 86 | |
| 87 | if __name__ == '__main__': |