mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-09-10 13:10:37 +02:00
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:
parent
8511c9f756
commit
188fd4ba61
1 changed files with 30 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue