Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 1 | # Temporary file name allocation |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 2 | # |
| 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 | |
| 7 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 8 | import os |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 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 | cff3454 | 1992-01-14 18:31:56 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 13 | tempdir = None |
| 14 | template = None |
| 15 | |
| 16 | |
| 17 | # Function to calculate the directory to use |
| 18 | |
| 19 | def gettempdir(): |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 20 | global tempdir |
Guido van Rossum | 4033ad7 | 1996-08-08 18:33:56 +0000 | [diff] [blame] | 21 | if tempdir is not None: |
| 22 | return tempdir |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 23 | attempdirs = ['/usr/tmp', '/tmp', os.getcwd(), os.curdir] |
Guido van Rossum | 3e065ad | 1996-08-20 20:38:59 +0000 | [diff] [blame] | 24 | if os.name == 'nt': |
| 25 | attempdirs.insert(0, 'C:\\TEMP') |
| 26 | attempdirs.insert(0, '\\TEMP') |
Guido van Rossum | f4f756c | 1997-04-11 19:00:53 +0000 | [diff] [blame] | 27 | elif os.name == 'mac': |
| 28 | import macfs, MACFS |
| 29 | try: |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 30 | refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk, |
| 31 | MACFS.kTemporaryFolderType, 0) |
Guido van Rossum | f4f756c | 1997-04-11 19:00:53 +0000 | [diff] [blame] | 32 | dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname() |
| 33 | attempdirs.insert(0, dirname) |
| 34 | except macfs.error: |
| 35 | pass |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 36 | for envname in 'TMPDIR', 'TEMP', 'TMP': |
| 37 | if os.environ.has_key(envname): |
| 38 | attempdirs.insert(0, os.environ[envname]) |
Guido van Rossum | 3e065ad | 1996-08-20 20:38:59 +0000 | [diff] [blame] | 39 | testfile = gettempprefix() + 'test' |
Guido van Rossum | f4aaf86 | 1996-05-28 23:31:34 +0000 | [diff] [blame] | 40 | for dir in attempdirs: |
| 41 | try: |
| 42 | filename = os.path.join(dir, testfile) |
| 43 | fp = open(filename, 'w') |
| 44 | fp.write('blat') |
| 45 | fp.close() |
| 46 | os.unlink(filename) |
| 47 | tempdir = dir |
| 48 | break |
| 49 | except IOError: |
| 50 | pass |
| 51 | if tempdir is None: |
| 52 | msg = "Can't find a usable temporary directory amongst " + `attempdirs` |
| 53 | raise IOError, msg |
| 54 | return tempdir |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | # Function to calculate a prefix of the filename to use |
| 58 | |
| 59 | def gettempprefix(): |
| 60 | global template |
| 61 | if template == None: |
| 62 | if os.name == 'posix': |
| 63 | template = '@' + `os.getpid()` + '.' |
Guido van Rossum | 80fb344 | 1997-12-15 19:11:53 +0000 | [diff] [blame] | 64 | elif os.name == 'nt': |
| 65 | template = '~' + `os.getpid()` + '-' |
Guido van Rossum | f4f756c | 1997-04-11 19:00:53 +0000 | [diff] [blame] | 66 | elif os.name == 'mac': |
| 67 | template = 'Python-Tmp-' |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 68 | else: |
| 69 | template = 'tmp' # XXX might choose a better one |
| 70 | return template |
Guido van Rossum | cff3454 | 1992-01-14 18:31:56 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 4a3a41f | 1991-12-26 13:10:50 +0000 | [diff] [blame] | 73 | # Counter for generating unique names |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 4a3a41f | 1991-12-26 13:10:50 +0000 | [diff] [blame] | 75 | counter = 0 |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 76 | |
| 77 | |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 78 | # User-callable function to return a unique temporary file name |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 80 | def mktemp(suffix=""): |
Guido van Rossum | 4a3a41f | 1991-12-26 13:10:50 +0000 | [diff] [blame] | 81 | global counter |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 82 | dir = gettempdir() |
| 83 | pre = gettempprefix() |
Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 84 | while 1: |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 85 | counter = counter + 1 |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 86 | file = os.path.join(dir, pre + `counter` + suffix) |
Guido van Rossum | 41f9503 | 1992-03-31 19:02:01 +0000 | [diff] [blame] | 87 | if not os.path.exists(file): |
| 88 | return file |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | class TemporaryFileWrapper: |
| 92 | """Temporary file wrapper |
| 93 | |
| 94 | This class provides a wrapper around files opened for temporary use. |
| 95 | In particular, it seeks to automatically remove the file when it is |
| 96 | no longer needed. |
| 97 | """ |
| 98 | def __init__(self, file, path): |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 99 | self.file = file |
| 100 | self.path = path |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 101 | |
| 102 | def close(self): |
| 103 | self.file.close() |
| 104 | os.unlink(self.path) |
| 105 | |
| 106 | def __del__(self): |
| 107 | try: self.close() |
| 108 | except: pass |
| 109 | |
| 110 | def __getattr__(self, name): |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 111 | file = self.__dict__['file'] |
| 112 | a = getattr(file, name) |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 113 | setattr(self, name, a) |
| 114 | return a |
| 115 | |
| 116 | |
Guido van Rossum | b8c42c9 | 1997-12-19 04:29:50 +0000 | [diff] [blame] | 117 | def TemporaryFile(mode='w+b', bufsize=-1, suffix=""): |
| 118 | name = mktemp(suffix) |
| 119 | file = open(name, mode, bufsize) |
Guido van Rossum | ca54982 | 1997-08-12 18:00:12 +0000 | [diff] [blame] | 120 | try: |
| 121 | os.unlink(name) |
| 122 | except os.error: |
| 123 | # Non-unix -- can't unlink file that's still open, use wrapper |
| 124 | return TemporaryFileWrapper(file, name) |
| 125 | else: |
| 126 | return file |