0
0
mirror of https://github.com/python/cpython.git synced 2024-11-28 16:45:42 +01:00
cpython/Tools/freeze/bkfile.py
Guido van Rossum baf0603493 New version, with contributions from Sjoerd Mullender and Mark Hammond.
Sjoerd writes:

This version of freeze creates one file per Python module, instead of
one humongous file for all Python modules.
bkfile: new module to used to write files with backups.  No new file
is produced if the new contents is identical to the old.
New option "-x excluded-module" for modulefinder test program.
New option "-i filename" for freeze main program to include a list of
options in place of the -i option.
1998-08-25 14:06:55 +00:00

51 lines
1.4 KiB
Python

_orig_open = open
class _BkFile:
def __init__(self, file, mode, bufsize):
import os
self.__filename = file
self.__backup = file + '~'
try:
os.unlink(self.__backup)
except os.error:
pass
try:
os.rename(file, self.__backup)
except os.error:
self.__backup = None
self.__file = _orig_open(file, mode, bufsize)
self.closed = self.__file.closed
self.fileno = self.__file.fileno
self.flush = self.__file.flush
self.isatty = self.__file.isatty
self.mode = self.__file.mode
self.name = self.__file.name
self.read = self.__file.read
self.readinto = self.__file.readinto
self.readline = self.__file.readline
self.readlines = self.__file.readlines
self.seek = self.__file.seek
self.softspace = self.__file.softspace
self.tell = self.__file.tell
self.truncate = self.__file.truncate
self.write = self.__file.write
self.writelines = self.__file.writelines
def close(self):
self.__file.close()
if self.__backup is None:
return
import cmp
# don't use cmp.cmp because of NFS bugs :-( and
# anyway, the stat mtime values differ so do_cmp will
# most likely be called anyway
if cmp.do_cmp(self.__backup, self.__filename):
import os
os.unlink(self.__filename)
os.rename(self.__backup, self.__filename)
def open(file, mode = 'r', bufsize = -1):
if 'w' not in mode:
return _orig_open(file, mode, bufsize)
return _BkFile(file, mode, bufsize)