From ed544b838cce6888483f18c50d1043bc9d9344a1 Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Wed, 4 Oct 2023 10:40:06 +0530 Subject: [PATCH 1/7] fix: hash data without replacing the CRLF with LF --- aea/helpers/ipfs/base.py | 32 ++++---------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/aea/helpers/ipfs/base.py b/aea/helpers/ipfs/base.py index 27842f5dad..b2f687bbe8 100644 --- a/aea/helpers/ipfs/base.py +++ b/aea/helpers/ipfs/base.py @@ -22,14 +22,12 @@ import hashlib import io import os -import re from pathlib import Path from typing import Any, Dict, Generator, Optional, Sized, Tuple, cast import base58 from aea.helpers.cid import to_v1 -from aea.helpers.io import open_file from aea.helpers.ipfs.utils import _protobuf_python_implementation @@ -43,35 +41,13 @@ from aea.helpers.ipfs.pb.merkledag_pb2 import PBNode # type: ignore -def _dos2unix(file_content: bytes) -> bytes: - """ - Replace occurrences of Windows line terminator CR/LF with only LF. - - :param file_content: the content of the file. - :return: the same content but with the line terminator - """ - return re.sub(b"\r\n", b"\n", file_content, flags=re.M) - - -def _is_text(file_path: str) -> bool: - """Check if a file can be read as text or not.""" - try: - with open_file(file_path, "r") as f: - f.read() - return True - except UnicodeDecodeError: - return False - - def _read(file_path: str) -> bytes: """Read a file, replacing Windows line endings if it is a text file.""" - is_text = _is_text(file_path) with open(file_path, "rb") as file: - file_b = file.read() - if is_text: - file_b = _dos2unix(file_b) - - return file_b + data = file.read() + if len(data) == 0: + raise ValueError(f"File cannot be empty: {file_path}") + return data def chunks(data: Sized, size: int) -> Generator: From f253d379f489589bf02f9eae379ae9aec97f5d0a Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Wed, 4 Oct 2023 10:40:47 +0530 Subject: [PATCH 2/7] fix: linters --- tests/test_helpers/test_ipfs/test_base.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/tests/test_helpers/test_ipfs/test_base.py b/tests/test_helpers/test_ipfs/test_base.py index 59d57c8ed5..06c8056421 100644 --- a/tests/test_helpers/test_ipfs/test_base.py +++ b/tests/test_helpers/test_ipfs/test_base.py @@ -22,23 +22,12 @@ import tempfile from pathlib import Path from tempfile import TemporaryDirectory -from unittest.mock import patch import pytest from aea_cli_ipfs.ipfs_utils import IPFSTool # type: ignore from aea.helpers.cid import to_v1 -from aea.helpers.ipfs.base import IPFSHashOnly, _is_text - - -def test_is_text_negative(): - """Test the helper method 'is_text' negative case.""" - # https://gehrcke.de/2015/12/how-to-raise-unicodedecodeerror-in-python-3/ - with patch( - "aea.helpers.ipfs.base.open_file", - side_effect=UnicodeDecodeError("foo", b"bytes", 1, 2, "Fake reason"), - ): - assert not _is_text("path") +from aea.helpers.ipfs.base import IPFSHashOnly def test_hash_for_big_file(): From 65cd68ba897f6ea6b973bcc88d41f0b90da953f0 Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Wed, 4 Oct 2023 10:40:54 +0530 Subject: [PATCH 3/7] chore: API docs --- docs/api/crypto/helpers.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/docs/api/crypto/helpers.md b/docs/api/crypto/helpers.md index f5bdf47650..17fc77678d 100644 --- a/docs/api/crypto/helpers.md +++ b/docs/api/crypto/helpers.md @@ -172,17 +172,3 @@ def hex_to_bytes_for_key(data: str) -> bytes Convert hex string to bytes with error handling. - - -#### generate`_`multiple`_`keys - -```python -def generate_multiple_keys(n: int, - type_: str, - password: Optional[str] = None, - extra_entropy: Union[str, bytes, int] = "", - file: Optional[str] = None) -> None -``` - -Generate n key pairs. - From dce3cea88eb84c82f2a8b375e05b4e5fb660e088 Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Wed, 4 Oct 2023 10:46:03 +0530 Subject: [PATCH 4/7] chore: copyright --- tests/test_helpers/test_ipfs/test_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_helpers/test_ipfs/test_base.py b/tests/test_helpers/test_ipfs/test_base.py index 06c8056421..d4301d6c49 100644 --- a/tests/test_helpers/test_ipfs/test_base.py +++ b/tests/test_helpers/test_ipfs/test_base.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # ------------------------------------------------------------------------------ # -# Copyright 2022 Valory AG +# Copyright 2022-2023 Valory AG # Copyright 2018-2021 Fetch.AI Limited # # Licensed under the Apache License, Version 2.0 (the "License"); From 310d0d427416718415ceecc2749a88ff5ea6275c Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Wed, 4 Oct 2023 11:42:34 +0530 Subject: [PATCH 5/7] fix: write dummy data for testing instead of keeping the file empty --- tests/test_package_manager/test_v1.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_package_manager/test_v1.py b/tests/test_package_manager/test_v1.py index 6578c2ea3b..f620fb1ff6 100644 --- a/tests/test_package_manager/test_v1.py +++ b/tests/test_package_manager/test_v1.py @@ -352,8 +352,7 @@ def test_update_fingerprints(self, caplog) -> None: dev_packages=OrderedDict({package_id: package_hash}), ) - (temp_package / "__init__.py").write_text("") - + (temp_package / "__init__.py").write_text("hello") with caplog.at_level(logging.ERROR): assert pm.verify() == 1 assert ( From fb89dfea890f1e1a2afa51e83843a001c7610625 Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Wed, 4 Oct 2023 12:40:21 +0530 Subject: [PATCH 6/7] fix: docstring [no ci] --- aea/helpers/ipfs/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aea/helpers/ipfs/base.py b/aea/helpers/ipfs/base.py index b2f687bbe8..a3f0bc4ea0 100644 --- a/aea/helpers/ipfs/base.py +++ b/aea/helpers/ipfs/base.py @@ -42,7 +42,7 @@ def _read(file_path: str) -> bytes: - """Read a file, replacing Windows line endings if it is a text file.""" + """Read and verify the file is not empty.""" with open(file_path, "rb") as file: data = file.read() if len(data) == 0: From d66d8d19f60d1059ba249d30b280b8d892587f8b Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Wed, 4 Oct 2023 13:46:56 +0530 Subject: [PATCH 7/7] fix: package manager tests --- tests/test_package_manager/test_v0.py | 4 ++-- tests/test_package_manager/test_v1.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_package_manager/test_v0.py b/tests/test_package_manager/test_v0.py index 588b4ecab1..ced9fa3b72 100644 --- a/tests/test_package_manager/test_v0.py +++ b/tests/test_package_manager/test_v0.py @@ -147,7 +147,7 @@ def test_update_fingerprints(self, caplog) -> None: path=packages_dir, packages=OrderedDict({package_id: package_hash}) ) - (temp_package / "__init__.py").write_text("") + (temp_package / "__init__.py").write_text("dummy") with caplog.at_level(logging.ERROR): assert pm.verify() == 1 @@ -210,7 +210,7 @@ def test_verify_method(self, caplog) -> None: pm.package_path_from_package_id(package_id=EXAMPLE_PACKAGE_ID) / INIT_FILE_NAME ) - init_file.write_text("") + init_file.write_text("dummy") with caplog.at_level(logging.ERROR), mock.patch( "aea.package_manager.v0.check_fingerprint", diff --git a/tests/test_package_manager/test_v1.py b/tests/test_package_manager/test_v1.py index f620fb1ff6..b5cc5b6cb7 100644 --- a/tests/test_package_manager/test_v1.py +++ b/tests/test_package_manager/test_v1.py @@ -562,7 +562,7 @@ def test_verify_method(self, caplog) -> None: pm.package_path_from_package_id(package_id=EXAMPLE_PACKAGE_ID) / INIT_FILE_NAME ) - init_file.write_text("") + init_file.write_text("dummy") with caplog.at_level(logging.ERROR), mock.patch( "aea.package_manager.v1.check_fingerprint",