Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Temporary files and filenames.""" |
| 2 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 3 | # 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 Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 7 | import os |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 9 | # Parameters that the caller may set to override the defaults |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 10 | tempdir = None |
| 11 | template = None |
| 12 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 13 | def gettempdir(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 14 | """Function to calculate the directory to use.""" |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 15 | global tempdir |
Guido van Rossum | 4033ad7 | 1996-08-08 18:33:56 +0000 | [diff] [blame] | 16 | if tempdir is not None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 17 | return tempdir |
Guido van Rossum | 29e5f5d | 1998-04-09 14:27:57 +0000 | [diff] [blame] | 18 | try: |
| 19 | pwd = os.getcwd() |
| 20 | except (AttributeError, os.error): |
| 21 | pwd = os.curdir |
Guido van Rossum | e504c0c | 2000-08-29 14:55:03 +0000 | [diff] [blame] | 22 | attempdirs = ['/var/tmp', '/usr/tmp', '/tmp', pwd] |
Guido van Rossum | 3e065ad | 1996-08-20 20:38:59 +0000 | [diff] [blame] | 23 | if os.name == 'nt': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 24 | attempdirs.insert(0, 'C:\\TEMP') |
| 25 | attempdirs.insert(0, '\\TEMP') |
Guido van Rossum | f4f756c | 1997-04-11 19:00:53 +0000 | [diff] [blame] | 26 | elif os.name == 'mac': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 27 | import macfs, MACFS |
| 28 | try: |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 29 | refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk, |
| 30 | MACFS.kTemporaryFolderType, 1) |
| 31 | dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname() |
| 32 | attempdirs.insert(0, dirname) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 33 | except macfs.error: |
| 34 | pass |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 35 | for envname in 'TMPDIR', 'TEMP', 'TMP': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 36 | if os.environ.has_key(envname): |
| 37 | attempdirs.insert(0, os.environ[envname]) |
Guido van Rossum | 3e065ad | 1996-08-20 20:38:59 +0000 | [diff] [blame] | 38 | testfile = gettempprefix() + 'test' |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 39 | for dir in attempdirs: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 40 | try: |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 41 | 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 Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 62 | except IOError: |
| 63 | pass |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 64 | if tempdir is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 65 | msg = "Can't find a usable temporary directory amongst " + `attempdirs` |
| 66 | raise IOError, msg |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 67 | return tempdir |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 68 | |
| 69 | |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 70 | # 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. |
| 73 | if 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. |
| 85 | elif os.name == "nt": |
| 86 | template = '~' + `os.getpid()` + '-' |
| 87 | elif os.name == 'mac': |
| 88 | template = 'Python-Tmp-' |
| 89 | else: |
| 90 | template = 'tmp' # XXX might choose a better one |
Guido van Rossum | b0e5718 | 1998-10-14 20:27:05 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 92 | def gettempprefix(): |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 93 | """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 Peters | 8373218 | 2001-01-14 05:12:40 +0000 | [diff] [blame] | 99 | global template |
Guido van Rossum | b0e5718 | 1998-10-14 20:27:05 +0000 | [diff] [blame] | 100 | if template is None: |
Tim Peters | 8373218 | 2001-01-14 05:12:40 +0000 | [diff] [blame] | 101 | return '@' + `os.getpid()` + '.' |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 102 | else: |
| 103 | return template |
Guido van Rossum | cff3454 | 1992-01-14 18:31:56 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 106 | def mktemp(suffix=""): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 107 | """User-callable function to return a unique temporary file name.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 108 | dir = gettempdir() |
| 109 | pre = gettempprefix() |
| 110 | while 1: |
Tim Peters | 1baa22a | 2001-01-12 10:02:46 +0000 | [diff] [blame] | 111 | i = _counter.get_next() |
| 112 | file = os.path.join(dir, pre + str(i) + suffix) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 113 | if not os.path.exists(file): |
| 114 | return file |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | class 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 Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 125 | self.file = file |
| 126 | self.path = path |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 127 | |
| 128 | def close(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 129 | self.file.close() |
| 130 | os.unlink(self.path) |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 131 | |
| 132 | def __del__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 133 | try: self.close() |
| 134 | except: pass |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 135 | |
| 136 | def __getattr__(self, name): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 137 | file = self.__dict__['file'] |
| 138 | a = getattr(file, name) |
Guido van Rossum | 6b708d5 | 1999-06-01 18:55:36 +0000 | [diff] [blame] | 139 | if type(a) != type(0): |
| 140 | setattr(self, name, a) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 141 | return a |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 142 | |
| 143 | |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 144 | def TemporaryFile(mode='w+b', bufsize=-1, suffix=""): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 145 | """Create and return a temporary file (opened read-write by default).""" |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 146 | name = mktemp(suffix) |
Guido van Rossum | dce3d55 | 1998-10-24 01:34:45 +0000 | [diff] [blame] | 147 | 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 Rossum | 2457fc2 | 1998-10-24 15:02:59 +0000 | [diff] [blame] | 150 | try: |
| 151 | os.unlink(name) |
| 152 | return os.fdopen(fd, mode, bufsize) |
| 153 | except: |
| 154 | os.close(fd) |
| 155 | raise |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 156 | else: |
Guido van Rossum | dce3d55 | 1998-10-24 01:34:45 +0000 | [diff] [blame] | 157 | # Non-unix -- can't unlink file that's still open, use wrapper |
| 158 | file = open(name, mode, bufsize) |
| 159 | return TemporaryFileWrapper(file, name) |
Tim Peters | 1baa22a | 2001-01-12 10:02:46 +0000 | [diff] [blame] | 160 | |
| 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 | |
| 169 | class _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 | |
| 185 | try: |
| 186 | import thread |
| 187 | |
| 188 | except ImportError: |
| 189 | class _DummyMutex: |
| 190 | def acquire(self): |
| 191 | pass |
| 192 | |
| 193 | release = acquire |
| 194 | |
| 195 | _counter = _ThreadSafeCounter(_DummyMutex()) |
| 196 | del _DummyMutex |
| 197 | |
| 198 | else: |
| 199 | _counter = _ThreadSafeCounter(thread.allocate_lock()) |
| 200 | del thread |
| 201 | |
| 202 | del _ThreadSafeCounter |