Staging
v0.5.1
v0.5.1
https://github.com/python/cpython
Revision 88dc264b0eed2bf18dc56c594ee50a6864c6c3cf authored by Guido van Rossum on 01 July 1996, 18:34:03 UTC, committed by Guido van Rossum on 01 July 1996, 18:34:03 UTC
1 parent 22b6db6
Tip revision: 88dc264b0eed2bf18dc56c594ee50a6864c6c3cf authored by Guido van Rossum on 01 July 1996, 18:34:03 UTC
New versions of VC project which has no source files in PC/
New versions of VC project which has no source files in PC/
Tip revision: 88dc264
tempfile.py
# 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...
import os
# Parameters that the caller may set to override the defaults
tempdir = None
template = None
# Function to calculate the directory to use
def gettempdir():
global tempdir
attempdirs = ['/usr/tmp', '/tmp', os.getcwd(), os.curdir]
if os.environ.has_key('TMPDIR'):
attempdirs.insert(0, os.environ['TMPDIR'])
testfile = gettempprefix() + '-*-writetest-*-'
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
# Function to calculate a prefix of the filename to use
def gettempprefix():
global template
if template == None:
if os.name == 'posix':
template = '@' + `os.getpid()` + '.'
else:
template = 'tmp' # XXX might choose a better one
return template
# Counter for generating unique names
counter = 0
# User-callable function to return a unique temporary file name
def mktemp():
global counter
dir = gettempdir()
pre = gettempprefix()
while 1:
counter = counter + 1
file = os.path.join(dir, pre + `counter`)
if not os.path.exists(file):
return file
Computing file changes ...