diff options
author | Matthew Waters <matthew@centricular.com> | 2023-07-12 16:25:02 +1000 |
---|---|---|
committer | Matthew Waters <matthew@centricular.com> | 2023-07-26 23:51:14 +0000 |
commit | 5a8b7625730d8aad90c29996a0918c3a99d87abd (patch) | |
tree | ed86c917365245b4d37b5f03f8bf1adddf75b908 | |
parent | 53cd86ec0b2ad5e886cb86d568d31b82afa6995e (diff) |
shell: use the context manager of the tempfile.TemporaryDirectory
Automatically removes the directory on normal exit
Part-of: <https://gitlab.freedesktop.org/gstreamer/cerbero/-/merge_requests/1231>
-rw-r--r-- | cerbero/utils/shell.py | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/cerbero/utils/shell.py b/cerbero/utils/shell.py index be7ba9ee..b82c626b 100644 --- a/cerbero/utils/shell.py +++ b/cerbero/utils/shell.py @@ -621,21 +621,20 @@ C:\\msys64\\msys2_shell.cmd -ucrt64 -defterm -no-start -here -use-full-path -c ' # We should remove the temporary directory # but there is a race with the bash process else: - tmp = tempfile.TemporaryDirectory() - rc_tmp = open(os.path.join(tmp.name, rc_file), 'w+') - rc_tmp.write(shellrc) - rc_tmp.flush() - if 'zsh' in shell: - env["ZDOTDIR"] = tmp.name - os.execlpe(shell, shell, env) - else: - # Check if the shell supports passing the rcfile - if os.system("%s %s %s -c echo 'test' > /dev/null 2>&1" % (shell, rc_opt, rc_tmp.name)) == 0: - os.execlpe(shell, shell, rc_opt, rc_tmp.name, env) - else: - env["CERBERO_ENV"] = "[cerbero-%s-%s]" % (platform, arch) + with tempfile.TemporaryDirectory() as tmp: + rc_tmp = open(os.path.join(tmp, rc_file), 'w+') + rc_tmp.write(shellrc) + rc_tmp.flush() + if 'zsh' in shell: + env["ZDOTDIR"] = tmp.name os.execlpe(shell, shell, env) - tmp.close() + else: + # Check if the shell supports passing the rcfile + if os.system("%s %s %s -c echo 'test' > /dev/null 2>&1" % (shell, rc_opt, rc_tmp.name)) == 0: + os.execlpe(shell, shell, rc_opt, rc_tmp.name, env) + else: + env["CERBERO_ENV"] = "[cerbero-%s-%s]" % (platform, arch) + os.execlpe(shell, shell, env) def which(pgm, path=None): |