Revision 1d466a4f lib/utils.py
b/lib/utils.py | ||
---|---|---|
1119 | 1119 |
RemoveEtcHostsEntry(constants.ETC_HOSTS, hi.ShortName()) |
1120 | 1120 |
|
1121 | 1121 |
|
1122 |
def TimestampForFilename(): |
|
1123 |
"""Returns the current time formatted for filenames. |
|
1124 |
|
|
1125 |
The format doesn't contain colons as some shells and applications them as |
|
1126 |
separators. |
|
1127 |
|
|
1128 |
""" |
|
1129 |
return time.strftime("%Y-%m-%d_%H_%M_%S") |
|
1130 |
|
|
1131 |
|
|
1122 | 1132 |
def CreateBackup(file_name): |
1123 | 1133 |
"""Creates a backup of a file. |
1124 | 1134 |
|
... | ... | |
1133 | 1143 |
raise errors.ProgrammerError("Can't make a backup of a non-file '%s'" % |
1134 | 1144 |
file_name) |
1135 | 1145 |
|
1136 |
prefix = '%s.backup-%d.' % (os.path.basename(file_name), int(time.time())) |
|
1146 |
prefix = ("%s.backup-%s." % |
|
1147 |
(os.path.basename(file_name), TimestampForFilename())) |
|
1137 | 1148 |
dir_name = os.path.dirname(file_name) |
1138 | 1149 |
|
1139 | 1150 |
fsrc = open(file_name, 'rb') |
... | ... | |
1141 | 1152 |
(fd, backup_name) = tempfile.mkstemp(prefix=prefix, dir=dir_name) |
1142 | 1153 |
fdst = os.fdopen(fd, 'wb') |
1143 | 1154 |
try: |
1155 |
logging.debug("Backing up %s at %s", file_name, backup_name) |
|
1144 | 1156 |
shutil.copyfileobj(fsrc, fdst) |
1145 | 1157 |
finally: |
1146 | 1158 |
fdst.close() |
Also available in: Unified diff