Tests: Accessory functions to create temporary files

These use tempfile.NamedTemporaryFile but allow execution without
raising "Text file busy".
The file is removed at exit (of the process) so no cleanup is necessary.
This commit is contained in:
Renato Alves 2015-06-05 14:09:47 +01:00
parent 8511c9f756
commit 188fd4ba61

View file

@ -5,6 +5,8 @@ import sys
import socket
import signal
import functools
import atexit
import tempfile
from subprocess import Popen, PIPE, STDOUT
from threading import Thread
from Queue import Queue, Empty
@ -444,4 +446,32 @@ def parse_datafile(file):
return data
def mkstemp(data):
"""
Create a temporary file that is removed at process exit
"""
def rmtemp(name):
try:
os.remove(name)
except OSError:
pass
f = tempfile.NamedTemporaryFile(delete=False)
f.write(data)
f.close()
# Ensure removal at end of python session
atexit.register(rmtemp, f.name)
return f.name
def mkstemp_exec(data):
"""Create a temporary executable file that is removed at process exit
"""
name = mkstemp(data)
os.chmod(name, 0755)
return name
# vim: ai sts=4 et sw=4