mirror of https://github.com/python/cpython
Mostly rewritten to be more flexible and more portable
./
This commit is contained in:
parent
1a76ef260d
commit
41f9503c11
|
@ -1,19 +1,44 @@
|
||||||
# Temporary file name allocation
|
# Temporary file name allocation
|
||||||
|
#
|
||||||
import posix
|
# XXX This tries to be not UNIX specific, but I don't know beans about
|
||||||
import path
|
# how to choose a temp directory or filename on MS-DOS or other
|
||||||
|
# systems so it may have to be changed...
|
||||||
|
|
||||||
|
|
||||||
# Changeable parameters (by clients!)...
|
import os
|
||||||
|
|
||||||
tempdir = '/usr/tmp'
|
|
||||||
template = '@'
|
|
||||||
|
|
||||||
# Use environment variable $TMPDIR to override default tempdir.
|
# Parameters that the caller may set to override the defaults
|
||||||
|
|
||||||
if posix.environ.has_key('TMPDIR'):
|
tempdir = None
|
||||||
# XXX Could check that it's a writable directory...
|
template = None
|
||||||
tempdir = posix.environ['TMPDIR']
|
|
||||||
|
|
||||||
|
# Function to calculate the directory to use
|
||||||
|
|
||||||
|
def gettempdir():
|
||||||
|
global tempdir
|
||||||
|
if tempdir == None:
|
||||||
|
try:
|
||||||
|
tempdir = os.environ['TMPDIR']
|
||||||
|
except (KeyError, AttributeError):
|
||||||
|
if os.name == 'posix':
|
||||||
|
tempdir = '/usr/tmp' # XXX Why not /tmp?
|
||||||
|
else:
|
||||||
|
tempdir = os.getcwd() # XXX Is this OK?
|
||||||
|
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 for generating unique names
|
||||||
|
@ -21,16 +46,14 @@ if posix.environ.has_key('TMPDIR'):
|
||||||
counter = 0
|
counter = 0
|
||||||
|
|
||||||
|
|
||||||
# User-callable function
|
# User-callable function to return a unique temporary file name
|
||||||
# XXX Should this have a parameter, like C's mktemp()?
|
|
||||||
# XXX Should we instead use the model of Standard C's tempnam()?
|
|
||||||
# XXX By all means, avoid a mess with four different functions like C...
|
|
||||||
|
|
||||||
def mktemp():
|
def mktemp():
|
||||||
global counter
|
global counter
|
||||||
|
dir = gettempdir()
|
||||||
|
pre = gettempprefix()
|
||||||
while 1:
|
while 1:
|
||||||
counter = counter+1
|
counter = counter + 1
|
||||||
file = tempdir+'/'+template+`posix.getpid()`+'.'+`counter`
|
file = os.path.join(dir, pre + `counter`)
|
||||||
if not path.exists(file):
|
if not os.path.exists(file):
|
||||||
break
|
return file
|
||||||
return file
|
|
||||||
|
|
Loading…
Reference in New Issue