Skip to content

Commit

Permalink
fix: checksum util
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Nov 2, 2023
1 parent 78d3a20 commit aaddee5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
6 changes: 3 additions & 3 deletions ethpm_types/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ def get_contract_compiler(self, contract_type_name: str) -> Optional[Compiler]:
Returns:
Optional[`~ethpm_types.source.Compiler`]
"""
for compiler in (self.compilers or []):
if contract_type_name in compiler.contractTypes:
for compiler in self.compilers or []:
if contract_type_name in (compiler.contractTypes or []):
return compiler

return None

def update_compilers(self, compilers: List[Compiler]):
updated_compilers = [*compilers]
for prior_compiler in (self.compilers or []):
for prior_compiler in self.compilers or []:
if prior_compiler not in updated_compilers:
updated_compilers.append(prior_compiler)

Expand Down
9 changes: 9 additions & 0 deletions ethpm_types/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ class Checksum(BaseModel):
The hash of a source files contents generated with the corresponding algorithm.
"""

@classmethod
def from_file(cls, file: Union[Path, str], algorithm: Algorithm = Algorithm.MD5) -> "Checksum":
source_path = file if isinstance(file, Path) else Path(file)
return cls.from_bytes(source_path.read_bytes(), algorithm=algorithm)

@classmethod
def from_bytes(cls, data: bytes, algorithm: Algorithm = Algorithm.MD5) -> "Checksum":
return cls(algorithm=algorithm, hash=compute_checksum(data, algorithm=algorithm))


class Content(BaseModel):
"""
Expand Down
3 changes: 2 additions & 1 deletion tests/test_package_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def test_adding_compiler_with_missing_contract():
compiler = Compiler(name="vyper", version="0.3.7", settings={}, contractTypes=["foobar"])

with pytest.raises(
ValidationError, match=r".*Invalid compiler\. Missing compiled contract type 'foobar'*"):
ValidationError, match=r".*Invalid compiler\. Missing compiled contract type 'foobar'*"
):
PackageManifest(compilers=[compiler])


Expand Down
17 changes: 16 additions & 1 deletion tests/test_source.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import tempfile
from pathlib import Path

import pytest

from ethpm_types._pydantic_v1 import FileUrl
from ethpm_types.source import Compiler, Content, ContractSource, Source
from ethpm_types.source import Checksum, Compiler, Content, ContractSource, Source
from ethpm_types.utils import Algorithm, compute_checksum

SOURCE_LOCATION = (
"https://github.com/OpenZeppelin/openzeppelin-contracts"
Expand Down Expand Up @@ -168,3 +172,14 @@ def test_compiler_equality():
compiler_1.settings["test"] = "123"
assert compiler_1 != compiler_2
compiler_1.settings = compiler_2.settings


def test_checksum_from_file():
file = Path(tempfile.mktemp())
file.write_text("foobartest123")
actual = Checksum.from_file(file)
expected = Checksum(
algorithm=Algorithm.MD5,
hash=compute_checksum(file.read_bytes()),
)
assert actual == expected

0 comments on commit aaddee5

Please sign in to comment.