cpython/Lib/tempfile.py

131 lines
3.4 KiB
Python
Raw Normal View History

1991-11-12 11:38:08 -04:00
# Temporary file name allocation
#
# 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
import os
1991-11-12 11:38:08 -04:00
# Parameters that the caller may set to override the defaults
1991-11-12 11:38:08 -04:00
tempdir = None
template = None
# Function to calculate the directory to use
def gettempdir():
1996-05-28 20:31:34 -03:00
global tempdir
if tempdir is not None:
return tempdir
try:
pwd = os.getcwd()
except (AttributeError, os.error):
pwd = os.curdir
attempdirs = ['/usr/tmp', '/tmp', pwd]
1996-08-20 17:38:59 -03:00
if os.name == 'nt':
attempdirs.insert(0, 'C:\\TEMP')
attempdirs.insert(0, '\\TEMP')
1997-04-11 16:00:53 -03:00
elif os.name == 'mac':
import macfs, MACFS
try:
refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk,
MACFS.kTemporaryFolderType, 0)
dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname()
attempdirs.insert(0, dirname)
except macfs.error:
pass
for envname in 'TMPDIR', 'TEMP', 'TMP':
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:
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
1996-05-28 20:31:34 -03:00
if tempdir is None:
msg = "Can't find a usable temporary directory amongst " + `attempdirs`
raise IOError, msg
1996-05-28 20:31:34 -03:00
return tempdir
# Function to calculate a prefix of the filename to use
def gettempprefix():
global template
if template == None:
if os.name == 'posix':
template = '@' + `os.getpid()` + '.'
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
1991-11-12 11:38:08 -04:00
1991-12-26 09:10:50 -04:00
# Counter for generating unique names
1991-11-12 11:38:08 -04:00
1991-12-26 09:10:50 -04:00
counter = 0
1991-11-12 11:38:08 -04:00
# User-callable function to return a unique temporary file name
1991-11-12 11:38:08 -04:00
def mktemp(suffix=""):
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
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):
self.file = file
self.path = path
def close(self):
self.file.close()
os.unlink(self.path)
def __del__(self):
try: self.close()
except: pass
def __getattr__(self, name):
file = self.__dict__['file']
a = getattr(file, name)
setattr(self, name, a)
return a
def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
name = mktemp(suffix)
file = open(name, mode, bufsize)
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