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