|
96 | 96 | def run(command):
|
97 | 97 | """Return (return-code, stdout, stderr)."""
|
98 | 98 | shell = True if type(command) is str else False
|
99 |
| - p = subprocess.Popen(command, |
100 |
| - stdout=subprocess.PIPE, |
101 |
| - stderr=subprocess.PIPE, |
102 |
| - shell=shell) |
103 |
| - raw_output, raw_err = p.communicate() |
104 |
| - rc = p.returncode |
105 |
| - if get_platform() == 'win32': |
106 |
| - enc = 'oem' |
107 |
| - else: |
108 |
| - enc = locale.getpreferredencoding() |
109 |
| - output = raw_output.decode(enc) |
110 |
| - if command == 'nvidia-smi topo -m': |
111 |
| - # don't remove the leading whitespace of `nvidia-smi topo -m` |
112 |
| - # because they are meaningful |
113 |
| - output = output.rstrip() |
114 |
| - else: |
115 |
| - output = output.strip() |
116 |
| - err = raw_err.decode(enc) |
117 |
| - return rc, output, err.strip() |
| 99 | + try: |
| 100 | + p = subprocess.Popen(command, |
| 101 | + stdout=subprocess.PIPE, |
| 102 | + stderr=subprocess.PIPE, |
| 103 | + shell=shell) |
| 104 | + raw_output, raw_err = p.communicate() |
| 105 | + rc = p.returncode |
| 106 | + if get_platform() == 'win32': |
| 107 | + enc = 'oem' |
| 108 | + else: |
| 109 | + enc = locale.getpreferredencoding() |
| 110 | + output = raw_output.decode(enc) |
| 111 | + if command == 'nvidia-smi topo -m': |
| 112 | + # don't remove the leading whitespace of `nvidia-smi topo -m` |
| 113 | + # because they are meaningful |
| 114 | + output = output.rstrip() |
| 115 | + else: |
| 116 | + output = output.strip() |
| 117 | + err = raw_err.decode(enc) |
| 118 | + return rc, output, err.strip() |
| 119 | + |
| 120 | + except FileNotFoundError: |
| 121 | + cmd_str = command if isinstance(command, str) else command[0] |
| 122 | + return 127, '', f"Command not found: {cmd_str}" |
118 | 123 |
|
119 | 124 |
|
120 | 125 | def run_and_read_all(run_lambda, command):
|
@@ -148,7 +153,7 @@ def get_conda_packages(run_lambda, patterns=None):
|
148 | 153 | if patterns is None:
|
149 | 154 | patterns = DEFAULT_CONDA_PATTERNS
|
150 | 155 | conda = os.environ.get('CONDA_EXE', 'conda')
|
151 |
| - out = run_and_read_all(run_lambda, "{} list".format(conda)) |
| 156 | + out = run_and_read_all(run_lambda, [conda, 'list']) |
152 | 157 | if out is None:
|
153 | 158 | return out
|
154 | 159 |
|
|
0 commit comments