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 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 9 | __all__ = ["mktemp", "TemporaryFile", "tempdir", "gettempprefix"] |
| 10 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 11 | # Parameters that the caller may set to override the defaults |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 12 | tempdir = None |
| 13 | template = None |
| 14 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 15 | def gettempdir(): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 16 | """Function to calculate the directory to use.""" |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 17 | global tempdir |
Guido van Rossum | 4033ad7 | 1996-08-08 18:33:56 +0000 | [diff] [blame] | 18 | if tempdir is not None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 19 | return tempdir |
Guido van Rossum | 29e5f5d | 1998-04-09 14:27:57 +0000 | [diff] [blame] | 20 | try: |
| 21 | pwd = os.getcwd() |
| 22 | except (AttributeError, os.error): |
| 23 | pwd = os.curdir |
Guido van Rossum | 7dcf84f | 2001-03-02 05:51:16 +0000 | [diff] [blame] | 24 | attempdirs = ['/tmp', '/var/tmp', '/usr/tmp', pwd] |
Guido van Rossum | 3e065ad | 1996-08-20 20:38:59 +0000 | [diff] [blame] | 25 | if os.name == 'nt': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 26 | attempdirs.insert(0, 'C:\\TEMP') |
| 27 | attempdirs.insert(0, '\\TEMP') |
Guido van Rossum | f4f756c | 1997-04-11 19:00:53 +0000 | [diff] [blame] | 28 | elif os.name == 'mac': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 29 | import macfs, MACFS |
| 30 | try: |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 31 | refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk, |
| 32 | MACFS.kTemporaryFolderType, 1) |
| 33 | dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname() |
| 34 | attempdirs.insert(0, dirname) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 35 | except macfs.error: |
| 36 | pass |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 37 | for envname in 'TMPDIR', 'TEMP', 'TMP': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 38 | if os.environ.has_key(envname): |
| 39 | attempdirs.insert(0, os.environ[envname]) |
Guido van Rossum | 3e065ad | 1996-08-20 20:38:59 +0000 | [diff] [blame] | 40 | testfile = gettempprefix() + 'test' |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 41 | for dir in attempdirs: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 42 | try: |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 43 | filename = os.path.join(dir, testfile) |
| 44 | if os.name == 'posix': |
| 45 | try: |
Jeremy Hylton | c348cd7 | 2001-02-19 15:34:10 +0000 | [diff] [blame] | 46 | fd = os.open(filename, |
| 47 | os.O_RDWR | os.O_CREAT | os.O_EXCL, 0700) |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 48 | except OSError: |
| 49 | pass |
| 50 | else: |
| 51 | fp = os.fdopen(fd, 'w') |
| 52 | fp.write('blat') |
| 53 | fp.close() |
| 54 | os.unlink(filename) |
| 55 | del fp, fd |
| 56 | tempdir = dir |
| 57 | break |
| 58 | else: |
| 59 | fp = open(filename, 'w') |
| 60 | fp.write('blat') |
| 61 | fp.close() |
| 62 | os.unlink(filename) |
| 63 | tempdir = dir |
| 64 | break |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 65 | except IOError: |
| 66 | pass |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 67 | if tempdir is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 68 | msg = "Can't find a usable temporary directory amongst " + `attempdirs` |
| 69 | raise IOError, msg |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 70 | return tempdir |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 71 | |
| 72 | |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 73 | # template caches the result of gettempprefix, for speed, when possible. |
| 74 | # XXX unclear why this isn't "_template"; left it "template" for backward |
| 75 | # compatibility. |
| 76 | if os.name == "posix": |
| 77 | # We don't try to cache the template on posix: the pid may change on us |
| 78 | # between calls due to a fork, and on Linux the pid changes even for |
| 79 | # another thread in the same process. Since any attempt to keep the |
| 80 | # cache in synch would have to call os.getpid() anyway in order to make |
| 81 | # sure the pid hasn't changed between calls, a cache wouldn't save any |
| 82 | # time. In addition, a cache is difficult to keep correct with the pid |
| 83 | # changing willy-nilly, and earlier attempts proved buggy (races). |
| 84 | template = None |
| 85 | |
| 86 | # Else the pid never changes, so gettempprefix always returns the same |
| 87 | # string. |
| 88 | elif os.name == "nt": |
| 89 | template = '~' + `os.getpid()` + '-' |
| 90 | elif os.name == 'mac': |
| 91 | template = 'Python-Tmp-' |
| 92 | else: |
| 93 | template = 'tmp' # XXX might choose a better one |
Guido van Rossum | b0e5718 | 1998-10-14 20:27:05 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 95 | def gettempprefix(): |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 96 | """Function to calculate a prefix of the filename to use. |
| 97 | |
| 98 | This incorporates the current process id on systems that support such a |
| 99 | notion, so that concurrent processes don't generate the same prefix. |
| 100 | """ |
| 101 | |
Tim Peters | 8373218 | 2001-01-14 05:12:40 +0000 | [diff] [blame] | 102 | global template |
Guido van Rossum | b0e5718 | 1998-10-14 20:27:05 +0000 | [diff] [blame] | 103 | if template is None: |
Tim Peters | 8373218 | 2001-01-14 05:12:40 +0000 | [diff] [blame] | 104 | return '@' + `os.getpid()` + '.' |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 105 | else: |
| 106 | return template |
Guido van Rossum | cff3454 | 1992-01-14 18:31:56 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 109 | def mktemp(suffix=""): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 110 | """User-callable function to return a unique temporary file name.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 111 | dir = gettempdir() |
| 112 | pre = gettempprefix() |
| 113 | while 1: |
Tim Peters | 1baa22a | 2001-01-12 10:02:46 +0000 | [diff] [blame] | 114 | i = _counter.get_next() |
| 115 | file = os.path.join(dir, pre + str(i) + suffix) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 116 | if not os.path.exists(file): |
| 117 | return file |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | class TemporaryFileWrapper: |
| 121 | """Temporary file wrapper |
| 122 | |
| 123 | This class provides a wrapper around files opened for temporary use. |
| 124 | In particular, it seeks to automatically remove the file when it is |
| 125 | no longer needed. |
| 126 | """ |
| 127 | def __init__(self, file, path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 128 | self.file = file |
| 129 | self.path = path |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 130 | |
| 131 | def close(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 132 | self.file.close() |
| 133 | os.unlink(self.path) |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 134 | |
| 135 | def __del__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 136 | try: self.close() |
| 137 | except: pass |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 138 | |
| 139 | def __getattr__(self, name): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 140 | file = self.__dict__['file'] |
| 141 | a = getattr(file, name) |
Guido van Rossum | 6b708d5 | 1999-06-01 18:55:36 +0000 | [diff] [blame] | 142 | if type(a) != type(0): |
| 143 | setattr(self, name, a) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 144 | return a |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 145 | |
| 146 | |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 147 | def TemporaryFile(mode='w+b', bufsize=-1, suffix=""): |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 148 | """Create and return a temporary file (opened read-write by default).""" |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 149 | name = mktemp(suffix) |
Guido van Rossum | dce3d55 | 1998-10-24 01:34:45 +0000 | [diff] [blame] | 150 | if os.name == 'posix': |
| 151 | # Unix -- be very careful |
| 152 | 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] | 153 | try: |
| 154 | os.unlink(name) |
| 155 | return os.fdopen(fd, mode, bufsize) |
| 156 | except: |
| 157 | os.close(fd) |
| 158 | raise |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 159 | else: |
Guido van Rossum | dce3d55 | 1998-10-24 01:34:45 +0000 | [diff] [blame] | 160 | # Non-unix -- can't unlink file that's still open, use wrapper |
| 161 | file = open(name, mode, bufsize) |
| 162 | return TemporaryFileWrapper(file, name) |
Tim Peters | 1baa22a | 2001-01-12 10:02:46 +0000 | [diff] [blame] | 163 | |
| 164 | # In order to generate unique names, mktemp() uses _counter.get_next(). |
| 165 | # This returns a unique integer on each call, in a threadsafe way (i.e., |
| 166 | # multiple threads will never see the same integer). The integer will |
| 167 | # usually be a Python int, but if _counter.get_next() is called often |
| 168 | # enough, it will become a Python long. |
| 169 | # Note that the only name that survives this next block of code |
| 170 | # is "_counter". |
| 171 | |
| 172 | class _ThreadSafeCounter: |
| 173 | def __init__(self, mutex, initialvalue=0): |
| 174 | self.mutex = mutex |
| 175 | self.i = initialvalue |
| 176 | |
| 177 | def get_next(self): |
| 178 | self.mutex.acquire() |
| 179 | result = self.i |
| 180 | try: |
| 181 | newi = result + 1 |
| 182 | except OverflowError: |
| 183 | newi = long(result) + 1 |
| 184 | self.i = newi |
| 185 | self.mutex.release() |
| 186 | return result |
| 187 | |
| 188 | try: |
| 189 | import thread |
| 190 | |
| 191 | except ImportError: |
| 192 | class _DummyMutex: |
| 193 | def acquire(self): |
| 194 | pass |
| 195 | |
| 196 | release = acquire |
| 197 | |
| 198 | _counter = _ThreadSafeCounter(_DummyMutex()) |
| 199 | del _DummyMutex |
| 200 | |
| 201 | else: |
| 202 | _counter = _ThreadSafeCounter(thread.allocate_lock()) |
| 203 | del thread |
| 204 | |
| 205 | del _ThreadSafeCounter |