filemanip: use with statements to open files.
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
This commit is contained in:
parent
d66aa22c12
commit
3e5ef8d7ec
1 changed files with 6 additions and 9 deletions
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Reference in a new issue