Return (return-code, stdout, stderr).
(command)
| 112 | |
| 113 | |
| 114 | def run(command): |
| 115 | """Return (return-code, stdout, stderr).""" |
| 116 | shell = True if type(command) is str else False |
| 117 | try: |
| 118 | p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell) |
| 119 | raw_output, raw_err = p.communicate() |
| 120 | rc = p.returncode |
| 121 | if get_platform() == "win32": |
| 122 | enc = "oem" |
| 123 | else: |
| 124 | enc = locale.getpreferredencoding() |
| 125 | output = raw_output.decode(enc) |
| 126 | if command == "nvidia-smi topo -m": |
| 127 | # don't remove the leading whitespace of `nvidia-smi topo -m` |
| 128 | # because they are meaningful |
| 129 | output = output.rstrip() |
| 130 | else: |
| 131 | output = output.strip() |
| 132 | err = raw_err.decode(enc) |
| 133 | return rc, output, err.strip() |
| 134 | |
| 135 | except FileNotFoundError: |
| 136 | cmd_str = command if isinstance(command, str) else command[0] |
| 137 | return 127, "", f"Command not found: {cmd_str}" |
| 138 | |
| 139 | |
| 140 | def run_and_read_all(run_lambda, command): |
nothing calls this directly
no test coverage detected