blob: 8ac707d7e0902eaf1b60ed71d95833c2f813183f [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Temporary files and filenames."""
2
Guido van Rossum41f95031992-03-31 19:02:01 +00003# XXX This tries to be not UNIX specific, but I don't know beans about
4# how to choose a temp directory or filename on MS-DOS or other
5# systems so it may have to be changed...
Guido van Rossumeee94981991-11-12 15:38:08 +00006
Guido van Rossum41f95031992-03-31 19:02:01 +00007import os
Guido van Rossumeee94981991-11-12 15:38:08 +00008
Guido van Rossum41f95031992-03-31 19:02:01 +00009# Parameters that the caller may set to override the defaults
Guido van Rossum41f95031992-03-31 19:02:01 +000010tempdir = None
11template = None
12
Guido van Rossum41f95031992-03-31 19:02:01 +000013def gettempdir():
Guido van Rossume7b146f2000-02-04 15:28:42 +000014 """Function to calculate the directory to use."""
Guido van Rossumf4aaf861996-05-28 23:31:34 +000015 global tempdir
Guido van Rossum4033ad71996-08-08 18:33:56 +000016 if tempdir is not None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000017 return tempdir
Guido van Rossum29e5f5d1998-04-09 14:27:57 +000018 try:
19 pwd = os.getcwd()
20 except (AttributeError, os.error):
21 pwd = os.curdir
Guido van Rossume504c0c2000-08-29 14:55:03 +000022 attempdirs = ['/var/tmp', '/usr/tmp', '/tmp', pwd]
Guido van Rossum3e065ad1996-08-20 20:38:59 +000023 if os.name == 'nt':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000024 attempdirs.insert(0, 'C:\\TEMP')
25 attempdirs.insert(0, '\\TEMP')
Guido van Rossumf4f756c1997-04-11 19:00:53 +000026 elif os.name == 'mac':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000027 import macfs, MACFS
28 try:
29 refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk,
Guido van Rossum57a06611998-04-28 16:03:34 +000030 MACFS.kTemporaryFolderType, 1)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000031 dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname()
32 attempdirs.insert(0, dirname)
33 except macfs.error:
34 pass
Guido van Rossumca549821997-08-12 18:00:12 +000035 for envname in 'TMPDIR', 'TEMP', 'TMP':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000036 if os.environ.has_key(envname):
37 attempdirs.insert(0, os.environ[envname])
Guido van Rossum3e065ad1996-08-20 20:38:59 +000038 testfile = gettempprefix() + 'test'
Guido van Rossumf4aaf861996-05-28 23:31:34 +000039 for dir in attempdirs:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000040 try:
Guido van Rossum00f09b32000-04-24 13:28:02 +000041 filename = os.path.join(dir, testfile)
42 if os.name == 'posix':
43 try:
44 fd = os.open(filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
45 except OSError:
46 pass
47 else:
48 fp = os.fdopen(fd, 'w')
49 fp.write('blat')
50 fp.close()
51 os.unlink(filename)
52 del fp, fd
53 tempdir = dir
54 break
55 else:
56 fp = open(filename, 'w')
57 fp.write('blat')
58 fp.close()
59 os.unlink(filename)
60 tempdir = dir
61 break
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000062 except IOError:
63 pass
Guido van Rossumf4aaf861996-05-28 23:31:34 +000064 if tempdir is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000065 msg = "Can't find a usable temporary directory amongst " + `attempdirs`
66 raise IOError, msg
Guido van Rossumf4aaf861996-05-28 23:31:34 +000067 return tempdir
Guido van Rossum41f95031992-03-31 19:02:01 +000068
69
Tim Peters9fadfb02001-01-13 03:04:02 +000070# template caches the result of gettempprefix, for speed, when possible.
71# XXX unclear why this isn't "_template"; left it "template" for backward
72# compatibility.
73if os.name == "posix":
74 # We don't try to cache the template on posix: the pid may change on us
75 # between calls due to a fork, and on Linux the pid changes even for
76 # another thread in the same process. Since any attempt to keep the
77 # cache in synch would have to call os.getpid() anyway in order to make
78 # sure the pid hasn't changed between calls, a cache wouldn't save any
79 # time. In addition, a cache is difficult to keep correct with the pid
80 # changing willy-nilly, and earlier attempts proved buggy (races).
81 template = None
82
83# Else the pid never changes, so gettempprefix always returns the same
84# string.
85elif os.name == "nt":
86 template = '~' + `os.getpid()` + '-'
87elif os.name == 'mac':
88 template = 'Python-Tmp-'
89else:
90 template = 'tmp' # XXX might choose a better one
Guido van Rossumb0e57181998-10-14 20:27:05 +000091
Guido van Rossum41f95031992-03-31 19:02:01 +000092def gettempprefix():
Tim Peters9fadfb02001-01-13 03:04:02 +000093 """Function to calculate a prefix of the filename to use.
94
95 This incorporates the current process id on systems that support such a
96 notion, so that concurrent processes don't generate the same prefix.
97 """
98
Tim Peters83732182001-01-14 05:12:40 +000099 global template
Guido van Rossumb0e57181998-10-14 20:27:05 +0000100 if template is None:
Tim Peters83732182001-01-14 05:12:40 +0000101 return '@' + `os.getpid()` + '.'
Tim Peters9fadfb02001-01-13 03:04:02 +0000102 else:
103 return template
Guido van Rossumcff34541992-01-14 18:31:56 +0000104
Guido van Rossumeee94981991-11-12 15:38:08 +0000105
Guido van Rossumb8c42c91997-12-19 04:29:50 +0000106def mktemp(suffix=""):
Guido van Rossume7b146f2000-02-04 15:28:42 +0000107 """User-callable function to return a unique temporary file name."""
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000108 dir = gettempdir()
109 pre = gettempprefix()
110 while 1:
Tim Peters1baa22a2001-01-12 10:02:46 +0000111 i = _counter.get_next()
112 file = os.path.join(dir, pre + str(i) + suffix)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000113 if not os.path.exists(file):
114 return file
Guido van Rossumca549821997-08-12 18:00:12 +0000115
116
117class TemporaryFileWrapper:
118 """Temporary file wrapper
119
120 This class provides a wrapper around files opened for temporary use.
121 In particular, it seeks to automatically remove the file when it is
122 no longer needed.
123 """
124 def __init__(self, file, path):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000125 self.file = file
126 self.path = path
Guido van Rossumca549821997-08-12 18:00:12 +0000127
128 def close(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000129 self.file.close()
130 os.unlink(self.path)
Guido van Rossumca549821997-08-12 18:00:12 +0000131
132 def __del__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000133 try: self.close()
134 except: pass
Guido van Rossumca549821997-08-12 18:00:12 +0000135
136 def __getattr__(self, name):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000137 file = self.__dict__['file']
138 a = getattr(file, name)
Guido van Rossum6b708d51999-06-01 18:55:36 +0000139 if type(a) != type(0):
140 setattr(self, name, a)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000141 return a
Guido van Rossumca549821997-08-12 18:00:12 +0000142
143
Guido van Rossumb8c42c91997-12-19 04:29:50 +0000144def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
Guido van Rossume7b146f2000-02-04 15:28:42 +0000145 """Create and return a temporary file (opened read-write by default)."""
Guido van Rossumb8c42c91997-12-19 04:29:50 +0000146 name = mktemp(suffix)
Guido van Rossumdce3d551998-10-24 01:34:45 +0000147 if os.name == 'posix':
148 # Unix -- be very careful
149 fd = os.open(name, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
Guido van Rossum2457fc21998-10-24 15:02:59 +0000150 try:
151 os.unlink(name)
152 return os.fdopen(fd, mode, bufsize)
153 except:
154 os.close(fd)
155 raise
Guido van Rossumca549821997-08-12 18:00:12 +0000156 else:
Guido van Rossumdce3d551998-10-24 01:34:45 +0000157 # Non-unix -- can't unlink file that's still open, use wrapper
158 file = open(name, mode, bufsize)
159 return TemporaryFileWrapper(file, name)
Tim Peters1baa22a2001-01-12 10:02:46 +0000160
161# In order to generate unique names, mktemp() uses _counter.get_next().
162# This returns a unique integer on each call, in a threadsafe way (i.e.,
163# multiple threads will never see the same integer). The integer will
164# usually be a Python int, but if _counter.get_next() is called often
165# enough, it will become a Python long.
166# Note that the only name that survives this next block of code
167# is "_counter".
168
169class _ThreadSafeCounter:
170 def __init__(self, mutex, initialvalue=0):
171 self.mutex = mutex
172 self.i = initialvalue
173
174 def get_next(self):
175 self.mutex.acquire()
176 result = self.i
177 try:
178 newi = result + 1
179 except OverflowError:
180 newi = long(result) + 1
181 self.i = newi
182 self.mutex.release()
183 return result
184
185try:
186 import thread
187
188except ImportError:
189 class _DummyMutex:
190 def acquire(self):
191 pass
192
193 release = acquire
194
195 _counter = _ThreadSafeCounter(_DummyMutex())
196 del _DummyMutex
197
198else:
199 _counter = _ThreadSafeCounter(thread.allocate_lock())
200 del thread
201
202del _ThreadSafeCounter