Guido van Rossum | eee9498 | 1991-11-12 15:38:08 +0000 | [diff] [blame] | 1 | # Temporary file name allocation |
| 2 | |
| 3 | import posix |
| 4 | import path |
| 5 | |
| 6 | |
| 7 | # Changeable parameters (by clients!)... |
| 8 | # XXX Should the environment variable $TMPDIR override tempdir? |
| 9 | |
| 10 | tempdir = '/usr/tmp' |
| 11 | template = '@' |
| 12 | |
| 13 | |
| 14 | # Kludge to hold mutable state |
| 15 | |
| 16 | class Struct(): pass |
| 17 | G = Struct() |
| 18 | G.i = 0 |
| 19 | |
| 20 | |
| 21 | # User-callable function |
| 22 | # XXX Should this have a parameter, like C's mktemp()? |
| 23 | # XXX Should we instead use the model of Standard C's tempnam()? |
| 24 | # XXX By all means, avoid a mess with four different functions like C... |
| 25 | |
| 26 | def mktemp(): |
| 27 | while 1: |
| 28 | G.i = G.i+1 |
| 29 | file = tempdir +'/'+ template + `posix.getpid()` +'.'+ `G.i` |
| 30 | if not path.exists(file): |
| 31 | break |
| 32 | return file |