filemanip: use with statements to open files.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
This commit is contained in:
brian m. carlson 2014-07-18 23:10:48 +00:00
parent d66aa22c12
commit 3e5ef8d7ec
No known key found for this signature in database
GPG key ID: BF535D811F52F68B

View file

@ -218,11 +218,10 @@ class HashTransactionStore(TransactionStore):
@staticmethod
def _hash_file(hashname, filename):
h = hashlib.__dict__[hashname]()
fp = open(filename, "rb")
data = fp.read(-1)
with open(filename, "rb") as fp:
data = fp.read(-1)
totallen = len(data)
h.update(data)
fp.close()
return "%s:%s:%s" % (hashname, str(totallen), h.hexdigest())
@staticmethod
@ -238,9 +237,8 @@ class HashTransactionStore(TransactionStore):
self._mode = mode
if "r" in mode:
try:
fp = open(filename + ".checksum", "r")
line = fp.readline()[:-1]
fp.close()
with open(filename + ".checksum", "r") as fp:
line = fp.readline()[:-1]
hashtype = line.split(":")[0]
result = self._hash_file(self._get_hashtype(self._hashname,
hashtype),
@ -258,9 +256,8 @@ class HashTransactionStore(TransactionStore):
if "w" in self._mode:
result = self._hash_file(self._get_hashtype(self._hashname, None),
self._filename)
fp = open(self._filename + ".checksum", "w")
fp.write(result + "\n")
fp.close()
with open(self._filename + ".checksum", "w") as fp:
fp.write(result + "\n")
class FileFormat: