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