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