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

Loading…
Cancel
Save