3 This module provides generic, low- and high-level interfaces for
4 creating temporary files and directories. The interfaces listed
5 as "safe" just below can be used without fear of race conditions.
6 Those listed as "unsafe" cannot, and are provided for backward
9 This module also provides some data items to the user:
11 TMP_MAX - maximum number of names that will be tried before
13 template - the default prefix for all temporary names.
14 You may change this to control the default prefix.
15 tempdir - If this is set to a string before the first use of
16 any routine from this module, it will be considered as
17 another candidate location to store temporary files.
21 "NamedTemporaryFile", "TemporaryFile", # high level safe interfaces
22 "SpooledTemporaryFile",
23 "mkstemp", "mkdtemp", # low level safe interfaces
24 "mktemp", # deprecated unsafe interface
25 "TMP_MAX", "gettempprefix", # constants
26 "tempdir", "gettempdir"
33 import errno
as _errno
34 from random
import Random
as _Random
37 import Carbon
.Folder
as _Folder
38 import Carbon
.Folders
as _Folders
41 from cStringIO
import StringIO
as _StringIO
43 from StringIO
import StringIO
as _StringIO
46 import fcntl
as _fcntl
53 flags
= _fcntl
.fcntl(fd
, _fcntl
.F_GETFD
, 0)
57 # flags read successfully, modify
58 flags |
= _fcntl
.FD_CLOEXEC
59 _fcntl
.fcntl(fd
, _fcntl
.F_SETFD
, flags
)
63 import thread
as _thread
65 import dummy_thread
as _thread
66 _allocate_lock
= _thread
.allocate_lock
68 _text_openflags
= _os
.O_RDWR | _os
.O_CREAT | _os
.O_EXCL
69 if hasattr(_os
, 'O_NOINHERIT'):
70 _text_openflags |
= _os
.O_NOINHERIT
71 if hasattr(_os
, 'O_NOFOLLOW'):
72 _text_openflags |
= _os
.O_NOFOLLOW
74 _bin_openflags
= _text_openflags
75 if hasattr(_os
, 'O_BINARY'):
76 _bin_openflags |
= _os
.O_BINARY
78 if hasattr(_os
, 'TMP_MAX'):
89 _once_lock
= _allocate_lock()
91 if hasattr(_os
, "lstat"):
93 elif hasattr(_os
, "stat"):
96 # Fallback. All we need is something that raises os.error if the
113 class _RandomNameSequence
:
114 """An instance of _RandomNameSequence generates an endless
115 sequence of unpredictable strings which can safely be incorporated
116 into file names. Each string is six characters long. Multiple
117 threads can safely use the same instance at the same time.
119 _RandomNameSequence is an iterator."""
121 characters
= ("abcdefghijklmnopqrstuvwxyz" +
122 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
126 self
.mutex
= _allocate_lock()
128 self
.normcase
= _os
.path
.normcase
136 choose
= self
.rng
.choice
140 letters
= [choose(c
) for dummy
in "123456"]
144 return self
.normcase(''.join(letters
))
146 def _candidate_tempdir_list():
147 """Generate a list of candidate temporary directories which
148 _get_default_tempdir will try."""
152 # First, try the environment.
153 for envname
in 'TMPDIR', 'TEMP', 'TMP':
154 dirname
= _os
.getenv(envname
)
155 if dirname
: dirlist
.append(dirname
)
157 # Failing that, try OS-specific locations.
158 if _os
.name
== 'mac':
160 fsr
= _Folder
.FSFindFolder(_Folders
.kOnSystemDisk
,
161 _Folders
.kTemporaryFolderType
, 1)
162 dirname
= fsr
.as_pathname()
163 dirlist
.append(dirname
)
164 except _Folder
.error
:
166 elif _os
.name
== 'riscos':
167 dirname
= _os
.getenv('Wimp$ScrapDir')
168 if dirname
: dirlist
.append(dirname
)
169 elif _os
.name
== 'nt':
170 dirlist
.extend([ r
'c:\temp', r
'c:\tmp', r
'\temp', r
'\tmp' ])
172 dirlist
.extend([ '/tmp', '/var/tmp', '/usr/tmp' ])
174 # As a last resort, the current directory.
176 dirlist
.append(_os
.getcwd())
177 except (AttributeError, _os
.error
):
178 dirlist
.append(_os
.curdir
)
182 def _get_default_tempdir():
183 """Calculate the default directory to use for temporary files.
184 This routine should be called exactly once.
186 We determine whether or not a candidate temp dir is usable by
187 trying to create and write to a file in that directory. If this
188 is successful, the test file is deleted. To prevent denial of
189 service, the name of the test file must be randomized."""
191 namer
= _RandomNameSequence()
192 dirlist
= _candidate_tempdir_list()
193 flags
= _text_openflags
196 if dir != _os
.curdir
:
197 dir = _os
.path
.normcase(_os
.path
.abspath(dir))
198 # Try only a few names per directory.
199 for seq
in xrange(100):
201 filename
= _os
.path
.join(dir, name
)
203 fd
= _os
.open(filename
, flags
, 0600)
204 fp
= _os
.fdopen(fd
, 'w')
210 except (OSError, IOError), e
:
211 if e
[0] != _errno
.EEXIST
:
212 break # no point trying more names in this directory
214 raise IOError, (_errno
.ENOENT
,
215 ("No usable temporary directory found in %s" % dirlist
))
217 _name_sequence
= None
219 def _get_candidate_names():
220 """Common setup sequence for all user-callable interfaces."""
222 global _name_sequence
223 if _name_sequence
is None:
226 if _name_sequence
is None:
227 _name_sequence
= _RandomNameSequence()
230 return _name_sequence
233 def _mkstemp_inner(dir, pre
, suf
, flags
):
234 """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
236 names
= _get_candidate_names()
238 for seq
in xrange(TMP_MAX
):
240 file = _os
.path
.join(dir, pre
+ name
+ suf
)
242 fd
= _os
.open(file, flags
, 0600)
244 return (fd
, _os
.path
.abspath(file))
246 if e
.errno
== _errno
.EEXIST
:
250 raise IOError, (_errno
.EEXIST
, "No usable temporary file name found")
253 # User visible interfaces.
256 """Accessor for tempdir.template."""
262 """Accessor for tempdir.tempdir."""
268 tempdir
= _get_default_tempdir()
273 def mkstemp(suffix
="", prefix
=template
, dir=None, text
=False):
274 """mkstemp([suffix, [prefix, [dir, [text]]]])
275 User-callable function to create and return a unique temporary
276 file. The return value is a pair (fd, name) where fd is the
277 file descriptor returned by os.open, and name is the filename.
279 If 'suffix' is specified, the file name will end with that suffix,
280 otherwise there will be no suffix.
282 If 'prefix' is specified, the file name will begin with that prefix,
283 otherwise a default prefix is used.
285 If 'dir' is specified, the file will be created in that directory,
286 otherwise a default directory is used.
288 If 'text' is specified and true, the file is opened in text
289 mode. Else (the default) the file is opened in binary mode. On
290 some operating systems, this makes no difference.
292 The file is readable and writable only by the creating user ID.
293 If the operating system uses permission bits to indicate whether a
294 file is executable, the file is executable by no one. The file
295 descriptor is not inherited by children of this process.
297 Caller is responsible for deleting the file when done with it.
304 flags
= _text_openflags
306 flags
= _bin_openflags
308 return _mkstemp_inner(dir, prefix
, suffix
, flags
)
311 def mkdtemp(suffix
="", prefix
=template
, dir=None):
312 """mkdtemp([suffix, [prefix, [dir]]])
313 User-callable function to create and return a unique temporary
314 directory. The return value is the pathname of the directory.
316 Arguments are as for mkstemp, except that the 'text' argument is
319 The directory is readable, writable, and searchable only by the
322 Caller is responsible for deleting the directory when done with it.
328 names
= _get_candidate_names()
330 for seq
in xrange(TMP_MAX
):
332 file = _os
.path
.join(dir, prefix
+ name
+ suffix
)
334 _os
.mkdir(file, 0700)
337 if e
.errno
== _errno
.EEXIST
:
341 raise IOError, (_errno
.EEXIST
, "No usable temporary directory name found")
343 def mktemp(suffix
="", prefix
=template
, dir=None):
344 """mktemp([suffix, [prefix, [dir]]])
345 User-callable function to return a unique temporary file name. The
348 Arguments are as for mkstemp, except that the 'text' argument is
351 This function is unsafe and should not be used. The file name
352 refers to a file that did not exist at some point, but by the time
353 you get around to creating it, someone else may have beaten you to
357 ## from warnings import warn as _warn
358 ## _warn("mktemp is a potential security risk to your program",
359 ## RuntimeWarning, stacklevel=2)
364 names
= _get_candidate_names()
365 for seq
in xrange(TMP_MAX
):
367 file = _os
.path
.join(dir, prefix
+ name
+ suffix
)
368 if not _exists(file):
371 raise IOError, (_errno
.EEXIST
, "No usable temporary filename found")
373 class _TemporaryFileWrapper
:
374 """Temporary file wrapper
376 This class provides a wrapper around files opened for
377 temporary use. In particular, it seeks to automatically
378 remove the file when it is no longer needed.
381 def __init__(self
, file, name
, delete
=True):
384 self
.close_called
= False
387 def __getattr__(self
, name
):
388 file = self
.__dict
__['file']
389 a
= getattr(file, name
)
390 if type(a
) != type(0):
391 setattr(self
, name
, a
)
394 # NT provides delete-on-close as a primitive, so we don't need
395 # the wrapper to do anything special. We still use it so that
396 # file.name is useful (i.e. not "(fdopen)") with NamedTemporaryFile.
399 # Cache the unlinker so we don't get spurious errors at
400 # shutdown when the module-level "os" is None'd out. Note
401 # that this must be referenced as self.unlink, because the
402 # name TemporaryFileWrapper may also get None'd out before
407 if not self
.close_called
:
408 self
.close_called
= True
411 self
.unlink(self
.name
)
416 def NamedTemporaryFile(mode
='w+b', bufsize
=-1, suffix
="",
417 prefix
=template
, dir=None, delete
=True):
418 """Create and return a temporary file.
420 'prefix', 'suffix', 'dir' -- as for mkstemp.
421 'mode' -- the mode argument to os.fdopen (default "w+b").
422 'bufsize' -- the buffer size argument to os.fdopen (default -1).
423 'delete' -- whether the file is deleted on close (default True).
424 The file is created as mkstemp() would do it.
426 Returns an object with a file-like interface; the name of the file
427 is accessible as file.name. The file will be automatically deleted
428 when it is closed unless the 'delete' argument is set to False.
435 flags
= _bin_openflags
437 flags
= _text_openflags
439 # Setting O_TEMPORARY in the flags causes the OS to delete
440 # the file when it is closed. This is only supported by Windows.
441 if _os
.name
== 'nt' and delete
:
442 flags |
= _os
.O_TEMPORARY
444 (fd
, name
) = _mkstemp_inner(dir, prefix
, suffix
, flags
)
445 file = _os
.fdopen(fd
, mode
, bufsize
)
446 return _TemporaryFileWrapper(file, name
, delete
)
448 if _os
.name
!= 'posix' or _os
.sys
.platform
== 'cygwin':
449 # On non-POSIX and Cygwin systems, assume that we cannot unlink a file
451 TemporaryFile
= NamedTemporaryFile
454 def TemporaryFile(mode
='w+b', bufsize
=-1, suffix
="",
455 prefix
=template
, dir=None):
456 """Create and return a temporary file.
458 'prefix', 'suffix', 'dir' -- as for mkstemp.
459 'mode' -- the mode argument to os.fdopen (default "w+b").
460 'bufsize' -- the buffer size argument to os.fdopen (default -1).
461 The file is created as mkstemp() would do it.
463 Returns an object with a file-like interface. The file has no
464 name, and will cease to exist when it is closed.
471 flags
= _bin_openflags
473 flags
= _text_openflags
475 (fd
, name
) = _mkstemp_inner(dir, prefix
, suffix
, flags
)
478 return _os
.fdopen(fd
, mode
, bufsize
)
483 class SpooledTemporaryFile
:
484 """Temporary file wrapper, specialized to switch from
485 StringIO to a real file when it exceeds a certain size or
486 when a fileno is needed.
490 def __init__(self
, max_size
=0, mode
='w+b', bufsize
=-1,
491 suffix
="", prefix
=template
, dir=None):
492 self
._file
= _StringIO()
493 self
._max
_size
= max_size
495 self
._TemporaryFileArgs
= (mode
, bufsize
, suffix
, prefix
, dir)
497 def _check(self
, file):
498 if self
._rolled
: return
499 max_size
= self
._max
_size
500 if max_size
and file.tell() > max_size
:
504 if self
._rolled
: return
506 newfile
= self
._file
= TemporaryFile(*self
._TemporaryFileArgs
)
507 del self
._TemporaryFileArgs
509 newfile
.write(file.getvalue())
510 newfile
.seek(file.tell(), 0)
516 return self
._file
.__iter
__()
523 return self
._file
.closed
527 return self
._file
.encoding
531 return self
._file
.fileno()
537 return self
._file
.isatty()
541 return self
._file
.mode
545 return self
._file
.name
549 return self
._file
.newlines
552 return self
._file
.next
554 def read(self
, *args
):
555 return self
._file
.read(*args
)
557 def readline(self
, *args
):
558 return self
._file
.readline(*args
)
560 def readlines(self
, *args
):
561 return self
._file
.readlines(*args
)
563 def seek(self
, *args
):
564 self
._file
.seek(*args
)
568 return self
._file
.softspace
571 return self
._file
.tell()
574 self
._file
.truncate()
582 def writelines(self
, iterable
):
584 rv
= file.writelines(iterable
)
588 def xreadlines(self
, *args
):
589 return self
._file
.xreadlines(*args
)