From 0e86d55806cf0ed724b29da30bc8308b682df4f6 Mon Sep 17 00:00:00 2001 From: anikeev-yadro Date: Mon, 24 Oct 2022 21:40:30 +0300 Subject: [PATCH] Add get arbitrary range from file Signed-off-by: anikeev-yadro --- pytest_tests/helpers/file_helper.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pytest_tests/helpers/file_helper.py b/pytest_tests/helpers/file_helper.py index d5a69c3..3da8421 100644 --- a/pytest_tests/helpers/file_helper.py +++ b/pytest_tests/helpers/file_helper.py @@ -60,19 +60,23 @@ def generate_file_with_content( @allure.step("Get File Hash") -def get_file_hash(file_path: str, len: Optional[int] = None) -> str: +def get_file_hash(file_path: str, len: Optional[int] = None, offset: Optional[int] = None) -> str: """Generates hash for the specified file. Args: file_path: Path to the file to generate hash for. len: How many bytes to read. + offset: Which position to start reading Returns: Hash of the file as hex-encoded string. """ file_hash = hashlib.sha256() with open(file_path, "rb") as out: - if len: + if len and offset is None: + file_hash.update(out.read(len)) + elif len and offset: + out.seek(offset, 1) file_hash.update(out.read(len)) else: file_hash.update(out.read())