2000-02-04 16:28:42 +01:00
|
|
|
"""Temporary files and filenames."""
|
|
|
|
|
1992-03-31 21:02:01 +02: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 16:38:08 +01:00
|
|
|
|
|
|
|
|
1992-03-31 21:02:01 +02:00
|
|
|
import os
|
1991-11-12 16:38:08 +01:00
|
|
|
|
|
|
|
|
1992-03-31 21:02:01 +02:00
|
|
|
# Parameters that the caller may set to override the defaults
|
1991-11-12 16:38:08 +01:00
|
|
|
|
1992-03-31 21:02:01 +02:00
|
|
|
tempdir = None
|
|
|
|
template = None
|
1992-01-14 19:31:56 +01:00
|
|
|
|
1992-03-31 21:02:01 +02:00
|
|
|
|
|
|
|
def gettempdir():
|
2000-02-04 16:28:42 +01:00
|
|
|
"""Function to calculate the directory to use."""
|
1996-05-29 01:31:34 +02:00
|
|
|
global tempdir
|
1996-08-08 20:33:56 +02:00
|
|
|
if tempdir is not None:
|
1998-03-26 22:13:24 +01:00
|
|
|
return tempdir
|
1998-04-09 16:27:57 +02:00
|
|
|
try:
|
|
|
|
pwd = os.getcwd()
|
|
|
|
except (AttributeError, os.error):
|
|
|
|
pwd = os.curdir
|
|
|
|
attempdirs = ['/usr/tmp', '/tmp', pwd]
|
1996-08-20 22:38:59 +02:00
|
|
|
if os.name == 'nt':
|
1998-03-26 22:13:24 +01:00
|
|
|
attempdirs.insert(0, 'C:\\TEMP')
|
|
|
|
attempdirs.insert(0, '\\TEMP')
|
1997-04-11 21:00:53 +02:00
|
|
|
elif os.name == 'mac':
|
1998-03-26 22:13:24 +01:00
|
|
|
import macfs, MACFS
|
|
|
|
try:
|
|
|
|
refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk,
|
1998-04-28 18:03:34 +02:00
|
|
|
MACFS.kTemporaryFolderType, 1)
|
1998-03-26 22:13:24 +01:00
|
|
|
dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname()
|
|
|
|
attempdirs.insert(0, dirname)
|
|
|
|
except macfs.error:
|
|
|
|
pass
|
1997-08-12 20:00:12 +02:00
|
|
|
for envname in 'TMPDIR', 'TEMP', 'TMP':
|
1998-03-26 22:13:24 +01:00
|
|
|
if os.environ.has_key(envname):
|
|
|
|
attempdirs.insert(0, os.environ[envname])
|
1996-08-20 22:38:59 +02:00
|
|
|
testfile = gettempprefix() + 'test'
|
1996-05-29 01:31:34 +02:00
|
|
|
for dir in attempdirs:
|
1998-03-26 22:13:24 +01:00
|
|
|
try:
|
2000-04-24 15:28:02 +02:00
|
|
|
filename = os.path.join(dir, testfile)
|
|
|
|
if os.name == 'posix':
|
|
|
|
try:
|
|
|
|
fd = os.open(filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
fp = os.fdopen(fd, 'w')
|
|
|
|
fp.write('blat')
|
|
|
|
fp.close()
|
|
|
|
os.unlink(filename)
|
|
|
|
del fp, fd
|
|
|
|
tempdir = dir
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
fp = open(filename, 'w')
|
|
|
|
fp.write('blat')
|
|
|
|
fp.close()
|
|
|
|
os.unlink(filename)
|
|
|
|
tempdir = dir
|
|
|
|
break
|
1998-03-26 22:13:24 +01:00
|
|
|
except IOError:
|
|
|
|
pass
|
1996-05-29 01:31:34 +02:00
|
|
|
if tempdir is None:
|
1998-03-26 22:13:24 +01:00
|
|
|
msg = "Can't find a usable temporary directory amongst " + `attempdirs`
|
|
|
|
raise IOError, msg
|
1996-05-29 01:31:34 +02:00
|
|
|
return tempdir
|
1992-03-31 21:02:01 +02:00
|
|
|
|
|
|
|
|
1998-10-14 22:27:05 +02:00
|
|
|
_pid = None
|
|
|
|
|
1992-03-31 21:02:01 +02:00
|
|
|
def gettempprefix():
|
2000-02-04 16:28:42 +01:00
|
|
|
"""Function to calculate a prefix of the filename to use."""
|
1998-10-14 22:27:05 +02:00
|
|
|
global template, _pid
|
|
|
|
if os.name == 'posix' and _pid and _pid != os.getpid():
|
|
|
|
# Our pid changed; we must have forked -- zap the template
|
|
|
|
template = None
|
|
|
|
if template is None:
|
1998-03-26 22:13:24 +01:00
|
|
|
if os.name == 'posix':
|
1998-10-14 22:27:05 +02:00
|
|
|
_pid = os.getpid()
|
|
|
|
template = '@' + `_pid` + '.'
|
1998-03-26 22:13:24 +01:00
|
|
|
elif os.name == 'nt':
|
|
|
|
template = '~' + `os.getpid()` + '-'
|
|
|
|
elif os.name == 'mac':
|
|
|
|
template = 'Python-Tmp-'
|
|
|
|
else:
|
|
|
|
template = 'tmp' # XXX might choose a better one
|
|
|
|
return template
|
1992-01-14 19:31:56 +01:00
|
|
|
|
1991-11-12 16:38:08 +01:00
|
|
|
|
1991-12-26 14:10:50 +01:00
|
|
|
# Counter for generating unique names
|
1991-11-12 16:38:08 +01:00
|
|
|
|
1991-12-26 14:10:50 +01:00
|
|
|
counter = 0
|
1991-11-12 16:38:08 +01:00
|
|
|
|
|
|
|
|
1997-12-19 05:29:50 +01:00
|
|
|
def mktemp(suffix=""):
|
2000-02-04 16:28:42 +01:00
|
|
|
"""User-callable function to return a unique temporary file name."""
|
1998-03-26 22:13:24 +01:00
|
|
|
global counter
|
|
|
|
dir = gettempdir()
|
|
|
|
pre = gettempprefix()
|
|
|
|
while 1:
|
|
|
|
counter = counter + 1
|
|
|
|
file = os.path.join(dir, pre + `counter` + suffix)
|
|
|
|
if not os.path.exists(file):
|
|
|
|
return file
|
1997-08-12 20:00:12 +02: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):
|
1998-03-26 22:13:24 +01:00
|
|
|
self.file = file
|
|
|
|
self.path = path
|
1997-08-12 20:00:12 +02:00
|
|
|
|
|
|
|
def close(self):
|
1998-03-26 22:13:24 +01:00
|
|
|
self.file.close()
|
|
|
|
os.unlink(self.path)
|
1997-08-12 20:00:12 +02:00
|
|
|
|
|
|
|
def __del__(self):
|
1998-03-26 22:13:24 +01:00
|
|
|
try: self.close()
|
|
|
|
except: pass
|
1997-08-12 20:00:12 +02:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
1998-03-26 22:13:24 +01:00
|
|
|
file = self.__dict__['file']
|
|
|
|
a = getattr(file, name)
|
1999-06-01 20:55:36 +02:00
|
|
|
if type(a) != type(0):
|
|
|
|
setattr(self, name, a)
|
1998-03-26 22:13:24 +01:00
|
|
|
return a
|
1997-08-12 20:00:12 +02:00
|
|
|
|
|
|
|
|
1997-12-19 05:29:50 +01:00
|
|
|
def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
|
2000-02-04 16:28:42 +01:00
|
|
|
"""Create and return a temporary file (opened read-write by default)."""
|
1997-12-19 05:29:50 +01:00
|
|
|
name = mktemp(suffix)
|
1998-10-24 03:34:45 +02:00
|
|
|
if os.name == 'posix':
|
|
|
|
# Unix -- be very careful
|
|
|
|
fd = os.open(name, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
|
1998-10-24 17:02:59 +02:00
|
|
|
try:
|
|
|
|
os.unlink(name)
|
|
|
|
return os.fdopen(fd, mode, bufsize)
|
|
|
|
except:
|
|
|
|
os.close(fd)
|
|
|
|
raise
|
1998-10-24 03:34:45 +02:00
|
|
|
else:
|
1998-03-26 22:13:24 +01:00
|
|
|
# Non-unix -- can't unlink file that's still open, use wrapper
|
1998-10-24 03:34:45 +02:00
|
|
|
file = open(name, mode, bufsize)
|
1998-03-26 22:13:24 +01:00
|
|
|
return TemporaryFileWrapper(file, name)
|