2000-02-04 11:28:42 -04:00
|
|
|
"""Temporary files and filenames."""
|
|
|
|
|
1992-03-31 15:02:01 -04: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 11:38:08 -04:00
|
|
|
|
1992-03-31 15:02:01 -04:00
|
|
|
import os
|
1991-11-12 11:38:08 -04:00
|
|
|
|
2001-03-01 00:27:19 -04:00
|
|
|
__all__ = ["mktemp", "TemporaryFile", "tempdir", "gettempprefix"]
|
|
|
|
|
1992-03-31 15:02:01 -04:00
|
|
|
# Parameters that the caller may set to override the defaults
|
|
|
|
tempdir = None
|
|
|
|
template = None
|
1992-01-14 14:31:56 -04:00
|
|
|
|
1992-03-31 15:02:01 -04:00
|
|
|
def gettempdir():
|
2002-01-28 19:11:23 -04:00
|
|
|
"""Function to calculate the directory to use."""
|
|
|
|
global tempdir
|
|
|
|
if tempdir is not None:
|
|
|
|
return tempdir
|
|
|
|
|
|
|
|
# _gettempdir_inner deduces whether a candidate temp dir is usable by
|
|
|
|
# trying to create a file in it, and write to it. If that succeeds,
|
|
|
|
# great, it closes the file and unlinks it. There's a race, though:
|
|
|
|
# the *name* of the test file it tries is the same across all threads
|
|
|
|
# under most OSes (Linux is an exception), and letting multiple threads
|
|
|
|
# all try to open, write to, close, and unlink a single file can cause
|
|
|
|
# a variety of bogus errors (e.g., you cannot unlink a file under
|
|
|
|
# Windows if anyone has it open, and two threads cannot create the
|
|
|
|
# same file in O_EXCL mode under Unix). The simplest cure is to serialize
|
|
|
|
# calls to _gettempdir_inner. This isn't a real expense, because the
|
|
|
|
# first thread to succeed sets the global tempdir, and all subsequent
|
|
|
|
# calls to gettempdir() reuse that without trying _gettempdir_inner.
|
|
|
|
_tempdir_lock.acquire()
|
|
|
|
try:
|
|
|
|
return _gettempdir_inner()
|
|
|
|
finally:
|
|
|
|
_tempdir_lock.release()
|
|
|
|
|
|
|
|
def _gettempdir_inner():
|
2000-02-04 11:28:42 -04:00
|
|
|
"""Function to calculate the directory to use."""
|
1996-05-28 20:31:34 -03:00
|
|
|
global tempdir
|
1996-08-08 15:33:56 -03:00
|
|
|
if tempdir is not None:
|
1998-03-26 17:13:24 -04:00
|
|
|
return tempdir
|
1998-04-09 11:27:57 -03:00
|
|
|
try:
|
|
|
|
pwd = os.getcwd()
|
|
|
|
except (AttributeError, os.error):
|
|
|
|
pwd = os.curdir
|
2001-03-02 01:51:16 -04:00
|
|
|
attempdirs = ['/tmp', '/var/tmp', '/usr/tmp', pwd]
|
1996-08-20 17:38:59 -03:00
|
|
|
if os.name == 'nt':
|
1998-03-26 17:13:24 -04:00
|
|
|
attempdirs.insert(0, 'C:\\TEMP')
|
|
|
|
attempdirs.insert(0, '\\TEMP')
|
1997-04-11 16:00:53 -03:00
|
|
|
elif os.name == 'mac':
|
1998-03-26 17:13:24 -04:00
|
|
|
import macfs, MACFS
|
|
|
|
try:
|
2001-01-14 23:26:36 -04:00
|
|
|
refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk,
|
|
|
|
MACFS.kTemporaryFolderType, 1)
|
|
|
|
dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname()
|
|
|
|
attempdirs.insert(0, dirname)
|
1998-03-26 17:13:24 -04:00
|
|
|
except macfs.error:
|
|
|
|
pass
|
2001-10-24 17:42:55 -03:00
|
|
|
elif os.name == 'riscos':
|
|
|
|
scrapdir = os.getenv('Wimp$ScrapDir')
|
|
|
|
if scrapdir:
|
|
|
|
attempdirs.insert(0, scrapdir)
|
1997-08-12 15:00:12 -03:00
|
|
|
for envname in 'TMPDIR', 'TEMP', 'TMP':
|
1998-03-26 17:13:24 -04:00
|
|
|
if os.environ.has_key(envname):
|
|
|
|
attempdirs.insert(0, os.environ[envname])
|
1996-08-20 17:38:59 -03:00
|
|
|
testfile = gettempprefix() + 'test'
|
1996-05-28 20:31:34 -03:00
|
|
|
for dir in attempdirs:
|
1998-03-26 17:13:24 -04:00
|
|
|
try:
|
2001-01-14 23:26:36 -04:00
|
|
|
filename = os.path.join(dir, testfile)
|
|
|
|
if os.name == 'posix':
|
|
|
|
try:
|
2001-02-19 11:34:10 -04:00
|
|
|
fd = os.open(filename,
|
|
|
|
os.O_RDWR | os.O_CREAT | os.O_EXCL, 0700)
|
2001-01-14 23:26:36 -04:00
|
|
|
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 17:13:24 -04:00
|
|
|
except IOError:
|
|
|
|
pass
|
1996-05-28 20:31:34 -03:00
|
|
|
if tempdir is None:
|
1998-03-26 17:13:24 -04:00
|
|
|
msg = "Can't find a usable temporary directory amongst " + `attempdirs`
|
|
|
|
raise IOError, msg
|
1996-05-28 20:31:34 -03:00
|
|
|
return tempdir
|
1992-03-31 15:02:01 -04:00
|
|
|
|
|
|
|
|
2001-01-12 23:04:02 -04:00
|
|
|
# template caches the result of gettempprefix, for speed, when possible.
|
|
|
|
# XXX unclear why this isn't "_template"; left it "template" for backward
|
|
|
|
# compatibility.
|
|
|
|
if os.name == "posix":
|
|
|
|
# We don't try to cache the template on posix: the pid may change on us
|
|
|
|
# between calls due to a fork, and on Linux the pid changes even for
|
|
|
|
# another thread in the same process. Since any attempt to keep the
|
|
|
|
# cache in synch would have to call os.getpid() anyway in order to make
|
|
|
|
# sure the pid hasn't changed between calls, a cache wouldn't save any
|
|
|
|
# time. In addition, a cache is difficult to keep correct with the pid
|
|
|
|
# changing willy-nilly, and earlier attempts proved buggy (races).
|
|
|
|
template = None
|
|
|
|
|
|
|
|
# Else the pid never changes, so gettempprefix always returns the same
|
|
|
|
# string.
|
|
|
|
elif os.name == "nt":
|
|
|
|
template = '~' + `os.getpid()` + '-'
|
2001-10-24 17:42:55 -03:00
|
|
|
elif os.name in ('mac', 'riscos'):
|
2001-01-12 23:04:02 -04:00
|
|
|
template = 'Python-Tmp-'
|
|
|
|
else:
|
|
|
|
template = 'tmp' # XXX might choose a better one
|
1998-10-14 17:27:05 -03:00
|
|
|
|
1992-03-31 15:02:01 -04:00
|
|
|
def gettempprefix():
|
2001-01-12 23:04:02 -04:00
|
|
|
"""Function to calculate a prefix of the filename to use.
|
|
|
|
|
|
|
|
This incorporates the current process id on systems that support such a
|
|
|
|
notion, so that concurrent processes don't generate the same prefix.
|
|
|
|
"""
|
|
|
|
|
2001-01-14 01:12:40 -04:00
|
|
|
global template
|
1998-10-14 17:27:05 -03:00
|
|
|
if template is None:
|
2001-01-14 01:12:40 -04:00
|
|
|
return '@' + `os.getpid()` + '.'
|
2001-01-12 23:04:02 -04:00
|
|
|
else:
|
|
|
|
return template
|
1992-01-14 14:31:56 -04:00
|
|
|
|
1991-11-12 11:38:08 -04:00
|
|
|
|
1997-12-19 00:29:50 -04:00
|
|
|
def mktemp(suffix=""):
|
2000-02-04 11:28:42 -04:00
|
|
|
"""User-callable function to return a unique temporary file name."""
|
1998-03-26 17:13:24 -04:00
|
|
|
dir = gettempdir()
|
|
|
|
pre = gettempprefix()
|
|
|
|
while 1:
|
2001-01-12 06:02:46 -04:00
|
|
|
i = _counter.get_next()
|
|
|
|
file = os.path.join(dir, pre + str(i) + suffix)
|
1998-03-26 17:13:24 -04:00
|
|
|
if not os.path.exists(file):
|
|
|
|
return file
|
1997-08-12 15:00:12 -03: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.
|
|
|
|
"""
|
2001-12-18 18:32:40 -04:00
|
|
|
|
|
|
|
# Cache the unlinker so we don't get spurious errors at shutdown
|
2001-12-18 19:22:01 -04:00
|
|
|
# when the module-level "os" is None'd out. Note that this must
|
2001-12-18 18:32:40 -04:00
|
|
|
# be referenced as self.unlink, because the name TemporaryFileWrapper
|
|
|
|
# may also get None'd out before __del__ is called.
|
|
|
|
unlink = os.unlink
|
|
|
|
|
1997-08-12 15:00:12 -03:00
|
|
|
def __init__(self, file, path):
|
1998-03-26 17:13:24 -04:00
|
|
|
self.file = file
|
|
|
|
self.path = path
|
2001-10-29 17:46:08 -04:00
|
|
|
self.close_called = 0
|
1997-08-12 15:00:12 -03:00
|
|
|
|
|
|
|
def close(self):
|
2001-10-29 17:46:08 -04:00
|
|
|
if not self.close_called:
|
|
|
|
self.close_called = 1
|
|
|
|
self.file.close()
|
2001-12-18 18:32:40 -04:00
|
|
|
self.unlink(self.path)
|
1997-08-12 15:00:12 -03:00
|
|
|
|
|
|
|
def __del__(self):
|
2001-10-29 17:46:08 -04:00
|
|
|
self.close()
|
1997-08-12 15:00:12 -03:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
1998-03-26 17:13:24 -04:00
|
|
|
file = self.__dict__['file']
|
|
|
|
a = getattr(file, name)
|
1999-06-01 15:55:36 -03:00
|
|
|
if type(a) != type(0):
|
|
|
|
setattr(self, name, a)
|
1998-03-26 17:13:24 -04:00
|
|
|
return a
|
1997-08-12 15:00:12 -03:00
|
|
|
|
|
|
|
|
1997-12-19 00:29:50 -04:00
|
|
|
def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
|
2000-02-04 11:28:42 -04:00
|
|
|
"""Create and return a temporary file (opened read-write by default)."""
|
1997-12-19 00:29:50 -04:00
|
|
|
name = mktemp(suffix)
|
1998-10-23 22:34:45 -03: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 12:02:59 -03:00
|
|
|
try:
|
|
|
|
os.unlink(name)
|
|
|
|
return os.fdopen(fd, mode, bufsize)
|
|
|
|
except:
|
|
|
|
os.close(fd)
|
|
|
|
raise
|
2002-01-30 03:47:51 -04:00
|
|
|
elif os.name == 'nt':
|
|
|
|
# Windows -- can't unlink an open file, but O_TEMPORARY creates a
|
|
|
|
# file that "deletes itself" when the last handle is closed.
|
|
|
|
# O_NOINHERIT ensures processes created via spawn() don't get a
|
|
|
|
# handle to this too. That would be a security hole, and, on my
|
|
|
|
# Win98SE box, when an O_TEMPORARY file is inherited by a spawned
|
|
|
|
# process, the fd in the spawned process seems to lack the
|
|
|
|
# O_TEMPORARY flag, so the file doesn't go away by magic then if the
|
|
|
|
# spawning process closes it first.
|
|
|
|
flags = (os.O_RDWR | os.O_CREAT | os.O_EXCL |
|
|
|
|
os.O_TEMPORARY | os.O_NOINHERIT)
|
|
|
|
if 'b' in mode:
|
|
|
|
flags |= os.O_BINARY
|
|
|
|
fd = os.open(name, flags, 0700)
|
|
|
|
return os.fdopen(fd, mode, bufsize)
|
1998-10-23 22:34:45 -03:00
|
|
|
else:
|
2002-01-30 03:47:51 -04:00
|
|
|
# Assume we can't unlink a file that's still open, or arrange for
|
|
|
|
# an automagically self-deleting file -- use wrapper.
|
1998-10-23 22:34:45 -03:00
|
|
|
file = open(name, mode, bufsize)
|
1998-03-26 17:13:24 -04:00
|
|
|
return TemporaryFileWrapper(file, name)
|
2001-01-12 06:02:46 -04:00
|
|
|
|
|
|
|
# In order to generate unique names, mktemp() uses _counter.get_next().
|
|
|
|
# This returns a unique integer on each call, in a threadsafe way (i.e.,
|
|
|
|
# multiple threads will never see the same integer). The integer will
|
|
|
|
# usually be a Python int, but if _counter.get_next() is called often
|
|
|
|
# enough, it will become a Python long.
|
2002-01-28 19:11:23 -04:00
|
|
|
# Note that the only names that survive this next block of code
|
|
|
|
# are "_counter" and "_tempdir_lock".
|
2001-01-12 06:02:46 -04:00
|
|
|
|
|
|
|
class _ThreadSafeCounter:
|
|
|
|
def __init__(self, mutex, initialvalue=0):
|
|
|
|
self.mutex = mutex
|
|
|
|
self.i = initialvalue
|
|
|
|
|
|
|
|
def get_next(self):
|
|
|
|
self.mutex.acquire()
|
|
|
|
result = self.i
|
|
|
|
try:
|
|
|
|
newi = result + 1
|
|
|
|
except OverflowError:
|
|
|
|
newi = long(result) + 1
|
|
|
|
self.i = newi
|
|
|
|
self.mutex.release()
|
|
|
|
return result
|
|
|
|
|
|
|
|
try:
|
|
|
|
import thread
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
class _DummyMutex:
|
|
|
|
def acquire(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
release = acquire
|
|
|
|
|
|
|
|
_counter = _ThreadSafeCounter(_DummyMutex())
|
2002-01-30 05:11:42 -04:00
|
|
|
_tempdir_lock = _DummyMutex()
|
2001-01-12 06:02:46 -04:00
|
|
|
del _DummyMutex
|
|
|
|
|
|
|
|
else:
|
|
|
|
_counter = _ThreadSafeCounter(thread.allocate_lock())
|
2002-01-28 19:11:23 -04:00
|
|
|
_tempdir_lock = thread.allocate_lock()
|
2001-01-12 06:02:46 -04:00
|
|
|
del thread
|
|
|
|
|
|
|
|
del _ThreadSafeCounter
|