Index: trunk/backup/WikiDump.py |
— | — | @@ -47,10 +47,18 @@ |
48 | 48 | return "-".join((key[0:4], key[4:6], key[6:8])) |
49 | 49 | |
50 | 50 | def dumpFile(filename, text): |
51 | | - """Dump a string to a file.""" |
52 | | - file = open(filename, "wt") |
| 51 | + """Dump a string to a file, as atomically as possible, via a temporary file in the same directory.""" |
| 52 | + |
| 53 | + # I'd use os.tempnam() here but it whines about symlinks attacks. |
| 54 | + tempFilename = filename + ".tmp" |
| 55 | + |
| 56 | + file = open(tempFilename, "wt") |
53 | 57 | file.write(text) |
54 | 58 | file.close() |
| 59 | + |
| 60 | + # This may fail across filesystems or on Windows. |
| 61 | + # Of course nothing else will work on Windows. ;) |
| 62 | + os.rename(tempFilename, filename) |
55 | 63 | |
56 | 64 | def readFile(filename): |
57 | 65 | file = open(filename, "r") |
Index: trunk/backup/monitor.py |
— | — | @@ -30,9 +30,7 @@ |
31 | 31 | |
32 | 32 | def updateIndex(): |
33 | 33 | outputFileName = os.path.join(config.publicDir, config.index) |
34 | | - outputFile = open(outputFileName, "w") |
35 | | - outputFile.write(generateIndex()) |
36 | | - outputFile.close() |
| 34 | + WikiDump.dumpFile(outputFileName, generateIndex()) |
37 | 35 | |
38 | 36 | if __name__ == "__main__": |
39 | 37 | updateIndex() |