Clean up AnsibleModule.digest_from_file

pull/85068/head
Matt Martz 7 months ago committed by Abhijeet Kasurde
parent ce84d3157d
commit 6bd5afde73

@ -0,0 +1,2 @@
minor_changes:
- AnsibleModule.digest_from_file - Cleanup function to use context manager and improved blocksize calculation

@ -55,7 +55,7 @@ from collections.abc import (
Set,
MutableSet,
)
from functools import reduce
from functools import reduce, partial
try:
import syslog
@ -1620,13 +1620,11 @@ class AnsibleModule(object):
self.fail_json(msg="Could not hash file '%s' with algorithm '%s'. Available algorithms: %s" %
(filename, algorithm, ', '.join(AVAILABLE_HASH_ALGORITHMS)))
blocksize = 64 * 1024
infile = open(os.path.realpath(b_filename), 'rb')
block = infile.read(blocksize)
while block:
digest_method.update(block)
block = infile.read(blocksize)
infile.close()
blocksize = (64 * 1024) // digest_method.block_size * digest_method.block_size
digest_update = digest_method.update
with open(os.path.realpath(b_filename), 'rb') as f:
for b_block in iter(partial(f.read, blocksize), b''):
digest_update(b_block)
return digest_method.hexdigest()
def md5(self, filename):

Loading…
Cancel
Save