Write the code to a temporary file.
(cmd, tmpdir)
| 23 | |
| 24 | |
| 25 | def _run_shell_command(cmd, tmpdir): |
| 26 | """Write the code to a temporary file.""" |
| 27 | cmdsuf = "" |
| 28 | if platform.system() == "Windows": |
| 29 | # suffix required to run command on windows |
| 30 | cmdsuf = ".bat" |
| 31 | # turn echo off |
| 32 | cmd = "@echo off\r\n" + cmd |
| 33 | handle, path = tempfile.mkstemp(text=True, dir=tmpdir, suffix=cmdsuf) |
| 34 | os.write(handle, cmd.encode("utf-8")) |
| 35 | os.close(handle) |
| 36 | os.chmod(path, stat.S_IRWXU) |
| 37 | |
| 38 | # Execute the file and read stdout |
| 39 | proc = Popen(path, shell=True, stdout=PIPE, stderr=PIPE) |
| 40 | proc.wait() |
| 41 | stdout, _ = proc.communicate() |
| 42 | os.unlink(path) |
| 43 | return _chomp(stdout.decode("utf-8")) |
| 44 | |
| 45 | |
| 46 | def _get_tmp(): |