Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/util.py @ 8327:aa25be1c2889
atomictempfile: delegate to posixfile instead of inheriting from it
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Thu, 26 Mar 2009 13:12:11 -0700 |
parents | b87a50b7125c |
children | f55869abb5c3 |
comparison
equal
deleted
inserted
replaced
8326:b7017097a4ec | 8327:aa25be1c2889 |
---|---|
962 try: os.unlink(temp) | 962 try: os.unlink(temp) |
963 except: pass | 963 except: pass |
964 raise | 964 raise |
965 return temp | 965 return temp |
966 | 966 |
967 class atomictempfile(posixfile): | 967 class atomictempfile: |
968 """file-like object that atomically updates a file | 968 """file-like object that atomically updates a file |
969 | 969 |
970 All writes will be redirected to a temporary copy of the original | 970 All writes will be redirected to a temporary copy of the original |
971 file. When rename is called, the copy is renamed to the original | 971 file. When rename is called, the copy is renamed to the original |
972 name, making the changes visible. | 972 name, making the changes visible. |
973 """ | 973 """ |
974 def __init__(self, name, mode, createmode): | 974 def __init__(self, name, mode, createmode): |
975 self.__name = name | 975 self.__name = name |
976 self.temp = mktempcopy(name, emptyok=('w' in mode), | 976 self.temp = mktempcopy(name, emptyok=('w' in mode), |
977 createmode=createmode) | 977 createmode=createmode) |
978 posixfile.__init__(self, self.temp, mode) | 978 self._fp = posixfile(self.temp, mode) |
979 | |
980 def __getattr__(self, name): | |
981 return getattr(self._fp, name) | |
979 | 982 |
980 def rename(self): | 983 def rename(self): |
981 if not self.closed: | 984 if not self.closed: |
982 posixfile.close(self) | 985 self._fp.close() |
983 rename(self.temp, localpath(self.__name)) | 986 rename(self.temp, localpath(self.__name)) |
984 | 987 |
985 def __del__(self): | 988 def __del__(self): |
986 if not self.closed: | 989 if not self.closed: |
987 try: | 990 try: |
988 os.unlink(self.temp) | 991 os.unlink(self.temp) |
989 except: pass | 992 except: pass |
990 posixfile.close(self) | 993 self._fp.close() |
991 | 994 |
992 def makedirs(name, mode=None): | 995 def makedirs(name, mode=None): |
993 """recursive directory creation with parent mode inheritance""" | 996 """recursive directory creation with parent mode inheritance""" |
994 try: | 997 try: |
995 os.mkdir(name) | 998 os.mkdir(name) |