Revision 70f4497c lib/utils.py
b/lib/utils.py | ||
---|---|---|
991 | 991 |
|
992 | 992 |
def WriteFile(file_name, fn=None, data=None, |
993 | 993 |
mode=None, uid=-1, gid=-1, |
994 |
atime=None, mtime=None): |
|
994 |
atime=None, mtime=None, |
|
995 |
check_abspath=True, dry_run=False, backup=False): |
|
995 | 996 |
"""(Over)write a file atomically. |
996 | 997 |
|
997 | 998 |
The file_name and either fn (a function taking one argument, the |
... | ... | |
1006 | 1007 |
temporary file should be removed. |
1007 | 1008 |
|
1008 | 1009 |
""" |
1009 |
if not os.path.isabs(file_name): |
|
1010 |
if check_abspath and not os.path.isabs(file_name):
|
|
1010 | 1011 |
raise errors.ProgrammerError("Path passed to WriteFile is not" |
1011 | 1012 |
" absolute: '%s'" % file_name) |
1012 | 1013 |
|
... | ... | |
1017 | 1018 |
raise errors.ProgrammerError("Both atime and mtime must be either" |
1018 | 1019 |
" set or None") |
1019 | 1020 |
|
1021 |
if backup and not dry_run and os.path.isfile(file_name): |
|
1022 |
CreateBackup(file_name) |
|
1020 | 1023 |
|
1021 | 1024 |
dir_name, base_name = os.path.split(file_name) |
1022 | 1025 |
fd, new_name = tempfile.mkstemp('.new', base_name, dir_name) |
... | ... | |
1034 | 1037 |
os.fsync(fd) |
1035 | 1038 |
if atime is not None and mtime is not None: |
1036 | 1039 |
os.utime(new_name, (atime, mtime)) |
1037 |
os.rename(new_name, file_name) |
|
1040 |
if not dry_run: |
|
1041 |
os.rename(new_name, file_name) |
|
1038 | 1042 |
finally: |
1039 | 1043 |
os.close(fd) |
1040 | 1044 |
RemoveFile(new_name) |
Also available in: Unified diff