Skip to content

Commit

Permalink
Merge pull request #1372 from njgheorghita/etherscan-dev
Browse files Browse the repository at this point in the history
Write web3.pm.get_local_package()
  • Loading branch information
njgheorghita authored Jun 25, 2019
2 parents 609b465 + 80a22d8 commit c79acee
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 0 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
get_open_port,
)

pytest_plugins = ["pytest_ethereum.plugins"]


@pytest.fixture(scope="module", params=[lambda x: to_bytes(hexstr=x), identity])
def address_conversion_func(request):
Expand Down
36 changes: 36 additions & 0 deletions tests/core/pm-module/test_pm_init.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import pytest

from eth_utils import (
Expand All @@ -13,6 +14,10 @@
get_manifest as get_ethpm_manifest,
)

from web3.exceptions import (
PMError,
)


def test_pm_init_with_minimal_manifest(w3):
owned_manifest = get_ethpm_manifest('owned', '1.0.1.json')
Expand Down Expand Up @@ -89,3 +94,34 @@ def test_pm_init_with_manifest_uri(w3, monkeypatch):
pkg = w3.pm.get_package_from_uri(dummy_standard_token_uri)
assert isinstance(pkg, Package)
assert pkg.name == "standard-token"


@pytest.fixture
def tmp_ethpmdir(tmp_path):
owned_manifest = get_ethpm_manifest("owned", "1.0.0.json")
ethpmdir = tmp_path / '_ethpm_packages'
ethpmdir.mkdir()
owned_dir = ethpmdir / 'owned'
owned_dir.mkdir()
manifest = owned_dir / 'manifest.json'
manifest.touch()
manifest.write_text(json.dumps(owned_manifest, sort_keys=True, separators=(",", ":")))
return ethpmdir


def test_get_local_package(w3, tmp_ethpmdir):
pkg = w3.pm.get_local_package("owned", tmp_ethpmdir)
assert isinstance(pkg, Package)
assert pkg.name == "owned"


def test_get_local_package_with_invalid_ethpmdir(w3, tmp_path):
invalid_ethpmdir = tmp_path / 'invalid'
invalid_ethpmdir.mkdir()
with pytest.raises(PMError, match="not a valid ethPM packages directory."):
w3.pm.get_local_package("owned", invalid_ethpmdir)


def test_get_local_package_with_uninstalled_package(w3, tmp_ethpmdir):
with pytest.raises(PMError, match="Package: safe-math not found in "):
w3.pm.get_local_package("safe-math", tmp_ethpmdir)
34 changes: 32 additions & 2 deletions web3/pm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
abstractmethod,
)
import json
from pathlib import (
Path,
)
from typing import (
Any,
Dict,
Expand Down Expand Up @@ -421,6 +424,33 @@ def get_package_from_uri(self, manifest_uri: URI) -> Package:
"""
return Package.from_uri(manifest_uri, self.web3)

def get_local_package(self, package_name: str, ethpm_dir: Path = None) -> Package:
"""
Returns a `Package <https://github.com/ethpm/py-ethpm/blob/master/ethpm/package.py>`__
instance built with the Manifest found at the package name in your local ethpm_dir.
* Parameters:
* ``package_name``: Must be the name of a package installed locally.
* ``ethpm_dir``: Path pointing to the target ethpm directory (optional).
"""
if not ethpm_dir:
ethpm_dir = Path.cwd() / '_ethpm_packages'

if not ethpm_dir.name == "_ethpm_packages" or not ethpm_dir.is_dir():
raise PMError(f"{ethpm_dir} is not a valid ethPM packages directory.")

local_packages = [pkg.name for pkg in ethpm_dir.iterdir() if pkg.is_dir()]
if package_name not in local_packages:
raise PMError(
f"Package: {package_name} not found in {ethpm_dir}. "
f"Available packages include: {local_packages}."
)

target_manifest = json.loads(
(ethpm_dir / package_name / "manifest.json").read_text()
)
return self.get_package_from_manifest(target_manifest)

def set_registry(self, address: Address) -> None:
"""
Sets the current registry used in ``web3.pm`` functions that read/write to an on-chain
Expand Down Expand Up @@ -488,13 +518,13 @@ def release_package(
validate_raw_manifest_format(raw_manifest)
manifest = json.loads(raw_manifest)
validate_manifest_against_schema(manifest)
if package_name != manifest['package_name']:
if package_name != manifest["package_name"]:
raise ManifestValidationError(
f"Provided package name: {package_name} does not match the package name "
f"found in the manifest: {manifest['package_name']}."
)

if version != manifest['version']:
if version != manifest["version"]:
raise ManifestValidationError(
f"Provided package version: {version} does not match the package version "
f"found in the manifest: {manifest['version']}."
Expand Down

0 comments on commit c79acee

Please sign in to comment.