Skip to content

Commit

Permalink
fix: hashing bug introduced in #674
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybayblade committed Oct 10, 2023
1 parent 267a652 commit ee2f998
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions aea/helpers/ipfs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
import hashlib
import io
import os
import platform
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


Expand All @@ -41,12 +44,34 @@
from aea.helpers.ipfs.pb.merkledag_pb2 import PBNode # type: ignore


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.readline()
return True
except UnicodeDecodeError:
return False


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 _read(file_path: str) -> bytes:
"""Read and verify the file is not empty."""
with open(file_path, "rb") as file:
data = file.read()
if len(data) == 0:
raise ValueError(f"File cannot be empty: {file_path}")
if platform.system() == "Windows" and _is_text(file_path=file_path):
data = _dos2unix(data)
return data


Expand Down

0 comments on commit ee2f998

Please sign in to comment.