Skip to content

Commit

Permalink
feat: method to help get compiler by name and version [APE-1618] (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Dec 14, 2023
1 parent 610254c commit 8d6ac28
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ethpm_types/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@ def unpack_sources(self, destination: Path):

source_path.write_text(content)

def get_compiler(self, name: str, version: str) -> Optional[Compiler]:
"""
Get a compiler by name and version.
Args:
name (str): The name of the compiler, such as ``"vyper"``.
version (str): The version of the compiler.
Returns:
Optional[`~ethpm_types.source.Compiler`]
"""
for compiler in self.compilers or []:
if compiler.name == name.lower() and compiler.version == version:
return compiler

return None

def get_contract_compiler(self, contract_type_name: str) -> Optional[Compiler]:
"""
Get the compiler used to compile the contract type, if it exists.
Expand Down
3 changes: 3 additions & 0 deletions ethpm_types/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def __eq__(self, other) -> bool:
and self.settings == other.settings
)

def __hash__(self) -> int:
return hash(f"{self.name}=={self.version}")


class Checksum(BaseModel):
"""Checksum information about the contents of a source file."""
Expand Down
9 changes: 9 additions & 0 deletions tests/test_package_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ def test_package_name_using_all_valid_characters():
assert manifest.name == name


def test_get_compiler():
compiler = Compiler(name="vyper", version="0.3.7", settings={}, contractTypes=["foobar"])
manifest = PackageManifest(
compilers=[compiler], contractTypes={"foobar": ContractType(contractNam="foobar")}
)
actual = manifest.get_compiler("vyper", "0.3.7")
assert actual == compiler


def test_get_contract_compiler():
compiler = Compiler(name="vyper", version="0.3.7", settings={}, contractTypes=["foobar"])
manifest = PackageManifest(
Expand Down
17 changes: 17 additions & 0 deletions tests/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,23 @@ def test_compiler_equality():
compiler_1.settings = compiler_2.settings


def test_compiler_hash():
compiler_1 = Compiler(
name="yo", version="0.2.0", settings={"foo": "bar"}, contractType=["test1"]
)
compiler_2 = Compiler(
name="foo", version="0.1.0", settings={"foo": "bar"}, contractType=["test1", "test2"]
)
compiler_3 = Compiler(
name="yo", version="0.2.0", settings={"foo": "bar"}, contractType=["test1"]
)
a_set = {compiler_1, compiler_2, compiler_3}
assert len(a_set) == 2
assert compiler_1 in a_set
assert compiler_2 in a_set
assert compiler_3 in a_set


def test_checksum_from_file():
file = Path(tempfile.mktemp())
file.write_text("foobartest123")
Expand Down

0 comments on commit 8d6ac28

Please sign in to comment.