Skip to content

Commit

Permalink
Merging pull request 108
Browse files Browse the repository at this point in the history
Signed-off-by: Lukáš Doktor <[email protected]>

* github.com:autotest/aexpect:
  Allow to calculate hashes on parts of files
  • Loading branch information
ldoktor committed Nov 9, 2022
2 parents c68ea10 + 8dce38f commit 38f3aa5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions aexpect/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,23 +424,29 @@ def grep(session, expr, path, check=False, flags="-a"):
###############################################################################


def hash_file(session, filename, method='md5'):
def hash_file(session, filename, size="", method="md5"):
"""
Calculate hash of given file from a session.
:param session: session to run the command on
:type session: ShellSession
:param str filename: full path of file that should be hashed
:param str size: calculate hash from initial data chunk of some size,
e.g. 1M, 1G, etc.
:param str method: method for hashing
:returns: hash of file, a hex number
:rtype: str
:raises: :py:class:`RuntimeError` if hash command failed (e.g. file not
found) or resulting output does not have expected format
"""
cmd = f"{method}sum"
if size:
cmd = f"head -c {size} {quote(filename)} | {cmd}"
else:
cmd += " " + quote(filename)

# run cmd on shell
status, output = session.cmd_status_output(cmd + ' ' + quote(filename))
status, output = session.cmd_status_output(cmd)
if output:
output = output.strip()
if status != 0 or not output:
Expand Down

0 comments on commit 38f3aa5

Please sign in to comment.