1991-11-12 15:38:08 +00:00
|
|
|
# Temporary file name allocation
|
1992-03-31 19:02:01 +00:00
|
|
|
#
|
|
|
|
# XXX This tries to be not UNIX specific, but I don't know beans about
|
|
|
|
# how to choose a temp directory or filename on MS-DOS or other
|
|
|
|
# systems so it may have to be changed...
|
1991-11-12 15:38:08 +00:00
|
|
|
|
|
|
|
|
1992-03-31 19:02:01 +00:00
|
|
|
import os
|
1991-11-12 15:38:08 +00:00
|
|
|
|
|
|
|
|
1992-03-31 19:02:01 +00:00
|
|
|
# Parameters that the caller may set to override the defaults
|
1991-11-12 15:38:08 +00:00
|
|
|
|
1992-03-31 19:02:01 +00:00
|
|
|
tempdir = None
|
|
|
|
template = None
|
1992-01-14 18:31:56 +00:00
|
|
|
|
1992-03-31 19:02:01 +00:00
|
|
|
|
|
|
|
# Function to calculate the directory to use
|
|
|
|
|
|
|
|
def gettempdir():
|
1996-05-28 23:31:34 +00:00
|
|
|
global tempdir
|
1996-08-08 18:33:56 +00:00
|
|
|
if tempdir is not None:
|
|
|
|
return tempdir
|
1996-05-28 23:31:34 +00:00
|
|
|
attempdirs = ['/usr/tmp', '/tmp', os.getcwd(), os.curdir]
|
1996-08-20 20:38:59 +00:00
|
|
|
if os.name == 'nt':
|
|
|
|
attempdirs.insert(0, 'C:\\TEMP')
|
|
|
|
attempdirs.insert(0, '\\TEMP')
|
1997-04-11 19:00:53 +00:00
|
|
|
elif os.name == 'mac':
|
|
|
|
import macfs, MACFS
|
|
|
|
try:
|
1997-12-19 04:29:50 +00:00
|
|
|
refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk,
|
|
|
|
MACFS.kTemporaryFolderType, 0)
|
1997-04-11 19:00:53 +00:00
|
|
|
dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname()
|
|
|
|
attempdirs.insert(0, dirname)
|
|
|
|
except macfs.error:
|
|
|
|
pass
|
1997-08-12 18:00:12 +00:00
|
|
|
for envname in 'TMPDIR', 'TEMP', 'TMP':
|
|
|
|
if os.environ.has_key(envname):
|
|
|
|
attempdirs.insert(0, os.environ[envname])
|
1996-08-20 20:38:59 +00:00
|
|
|
testfile = gettempprefix() + 'test'
|
1996-05-28 23:31:34 +00:00
|
|
|
for dir in attempdirs:
|
|
|
|
try:
|
|
|
|
filename = os.path.join(dir, testfile)
|
|
|
|
fp = open(filename, 'w')
|
|
|
|
fp.write('blat')
|
|
|
|
fp.close()
|
|
|
|
os.unlink(filename)
|
|
|
|
tempdir = dir
|
|
|
|
break
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
if tempdir is None:
|
|
|
|
msg = "Can't find a usable temporary directory amongst " + `attempdirs`
|
|
|
|
raise IOError, msg
|
|
|
|
return tempdir
|
1992-03-31 19:02:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Function to calculate a prefix of the filename to use
|
|
|
|
|
|
|
|
def gettempprefix():
|
|
|
|
global template
|
|
|
|
if template == None:
|
|
|
|
if os.name == 'posix':
|
|
|
|
template = '@' + `os.getpid()` + '.'
|
1997-12-15 19:11:53 +00:00
|
|
|
elif os.name == 'nt':
|
|
|
|
template = '~' + `os.getpid()` + '-'
|
1997-04-11 19:00:53 +00:00
|
|
|
elif os.name == 'mac':
|
|
|
|
template = 'Python-Tmp-'
|
1992-03-31 19:02:01 +00:00
|
|
|
else:
|
|
|
|
template = 'tmp' # XXX might choose a better one
|
|
|
|
return template
|
1992-01-14 18:31:56 +00:00
|
|
|
|
1991-11-12 15:38:08 +00:00
|
|
|
|
1991-12-26 13:10:50 +00:00
|
|
|
# Counter for generating unique names
|
1991-11-12 15:38:08 +00:00
|
|
|
|
1991-12-26 13:10:50 +00:00
|
|
|
counter = 0
|
1991-11-12 15:38:08 +00:00
|
|
|
|
|
|
|
|
1992-03-31 19:02:01 +00:00
|
|
|
# User-callable function to return a unique temporary file name
|
1991-11-12 15:38:08 +00:00
|
|
|
|
1997-12-19 04:29:50 +00:00
|
|
|
def mktemp(suffix=""):
|
1991-12-26 13:10:50 +00:00
|
|
|
global counter
|
1992-03-31 19:02:01 +00:00
|
|
|
dir = gettempdir()
|
|
|
|
pre = gettempprefix()
|
1991-11-12 15:38:08 +00:00
|
|
|
while 1:
|
1992-03-31 19:02:01 +00:00
|
|
|
counter = counter + 1
|
1997-12-19 04:29:50 +00:00
|
|
|
file = os.path.join(dir, pre + `counter` + suffix)
|
1992-03-31 19:02:01 +00:00
|
|
|
if not os.path.exists(file):
|
|
|
|
return file
|
1997-08-12 18:00:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TemporaryFileWrapper:
|
|
|
|
"""Temporary file wrapper
|
|
|
|
|
|
|
|
This class provides a wrapper around files opened for temporary use.
|
|
|
|
In particular, it seeks to automatically remove the file when it is
|
|
|
|
no longer needed.
|
|
|
|
"""
|
|
|
|
def __init__(self, file, path):
|
1997-12-19 04:29:50 +00:00
|
|
|
self.file = file
|
|
|
|
self.path = path
|
1997-08-12 18:00:12 +00:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.file.close()
|
|
|
|
os.unlink(self.path)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
try: self.close()
|
|
|
|
except: pass
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
1997-12-19 04:29:50 +00:00
|
|
|
file = self.__dict__['file']
|
|
|
|
a = getattr(file, name)
|
1997-08-12 18:00:12 +00:00
|
|
|
setattr(self, name, a)
|
|
|
|
return a
|
|
|
|
|
|
|
|
|
1997-12-19 04:29:50 +00:00
|
|
|
def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
|
|
|
|
name = mktemp(suffix)
|
|
|
|
file = open(name, mode, bufsize)
|
1997-08-12 18:00:12 +00:00
|
|
|
try:
|
|
|
|
os.unlink(name)
|
|
|
|
except os.error:
|
|
|
|
# Non-unix -- can't unlink file that's still open, use wrapper
|
|
|
|
return TemporaryFileWrapper(file, name)
|
|
|
|
else:
|
|
|
|
return file
|