replace mktemp usages with mkstemp (#16260)
Summary of the issue:
From CodeQL: "Functions that create temporary file names (such as tempfile.mktemp and os.tempnam) are fundamentally insecure, as they do not ensure exclusive access to a file with the temporary name they return. The file name returned by these functions is guaranteed to be unique on creation but the file must be opened in a separate operation. There is no guarantee that the creation and open operations will happen atomically. This provides an opportunity for an attacker to interfere with the file before it is opened.
Note that mktemp has been deprecated since Python 2.3."
Given our usage of tempfile.mktemp is to safely delete files, this is not a security concern, as exclusive access to these files is not required. However, given that the function is deprecated and strongly discouraged, we should modernise our code to use what is recommended by python.
Description of user facing changes
None
Description of development approach
Replace usages of tempfile.mktemp with the secure alternative tempfile.mkstemp.
https://docs.python.org/3/library/tempfile.html#tempfile.mktemp
https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp
os.rename does not work if the file already exists, so instances have been replaced by os.replace:
https://docs.python.org/3/library/os.html#os.replace
https://docs.python.org/3/library/os.html#os.rename