folder: LocalStatus: avoid to redefine 'file'

Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Nicolas Sebrecht 2015-01-18 22:09:41 +01:00
parent 461554b7b1
commit 95eb8697f9

View File

@ -114,8 +114,8 @@ class LocalStatusFolder(BaseFolder):
# Loop as many times as version, and update format. # Loop as many times as version, and update format.
for i in range(1, self.cur_version + 1): for i in range(1, self.cur_version + 1):
self.messagelist = {} self.messagelist = {}
cache = open(self.filename, "rt") cachefd = open(self.filename, "rt")
line = cache.readline().strip() line = cachefd.readline().strip()
# Format is up to date. break. # Format is up to date. break.
if line == (self.magicline % self.cur_version): if line == (self.magicline % self.cur_version):
@ -125,8 +125,8 @@ class LocalStatusFolder(BaseFolder):
elif line == (self.magicline % 1): elif line == (self.magicline % 1):
self.ui._msg('Upgrading LocalStatus cache from version 1' self.ui._msg('Upgrading LocalStatus cache from version 1'
'to version 2 for %s:%s'% (self.repository, self)) 'to version 2 for %s:%s'% (self.repository, self))
self.readstatus_v1(cache) self.readstatus_v1(cachefd)
cache.close() cachefd.close()
self.save() self.save()
# NOTE: Add other format transitions here in the future. # NOTE: Add other format transitions here in the future.
@ -148,12 +148,12 @@ class LocalStatusFolder(BaseFolder):
# but somehow did. # but somehow did.
errstr = "Cache file '%s' is empty."% self.filename errstr = "Cache file '%s' is empty."% self.filename
self.ui.warn(errstr) self.ui.warn(errstr)
cache.close() cachefd.close()
return return
assert(line == (self.magicline % self.cur_version)) assert(line == (self.magicline % self.cur_version))
self.readstatus(cache) self.readstatus(cachefd)
cache.close() cachefd.close()
def dropmessagelistcache(self): def dropmessagelistcache(self):
self.messagelist = None self.messagelist = None
@ -167,16 +167,16 @@ class LocalStatusFolder(BaseFolder):
"""Saves the entire messagelist to disk.""" """Saves the entire messagelist to disk."""
with self.savelock: with self.savelock:
file = open(self.filename + ".tmp", "wt") cachefd = open(self.filename + ".tmp", "wt")
file.write((self.magicline % self.cur_version) + "\n") cachefd.write((self.magicline % self.cur_version) + "\n")
for msg in self.messagelist.values(): for msg in self.messagelist.values():
flags = ''.join(sorted(msg['flags'])) flags = ''.join(sorted(msg['flags']))
labels = ', '.join(sorted(msg['labels'])) labels = ', '.join(sorted(msg['labels']))
file.write("%s|%s|%d|%s\n" % (msg['uid'], flags, msg['mtime'], labels)) cachefd.write("%s|%s|%d|%s\n" % (msg['uid'], flags, msg['mtime'], labels))
file.flush() cachefd.flush()
if self.doautosave: if self.doautosave:
os.fsync(file.fileno()) os.fsync(cachefd.fileno())
file.close() cachefd.close()
os.rename(self.filename + ".tmp", self.filename) os.rename(self.filename + ".tmp", self.filename)
if self.doautosave: if self.doautosave: