Skip to content

Commit 80a22d8

Browse files
committed
Write web3.pm.get_local_package()
1 parent 609b465 commit 80a22d8

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

tests/conftest.py

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
get_open_port,
1212
)
1313

14-
pytest_plugins = ["pytest_ethereum.plugins"]
15-
1614

1715
@pytest.fixture(scope="module", params=[lambda x: to_bytes(hexstr=x), identity])
1816
def address_conversion_func(request):

tests/core/pm-module/test_pm_init.py

+36
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import pytest
23

34
from eth_utils import (
@@ -13,6 +14,10 @@
1314
get_manifest as get_ethpm_manifest,
1415
)
1516

17+
from web3.exceptions import (
18+
PMError,
19+
)
20+
1621

1722
def test_pm_init_with_minimal_manifest(w3):
1823
owned_manifest = get_ethpm_manifest('owned', '1.0.1.json')
@@ -89,3 +94,34 @@ def test_pm_init_with_manifest_uri(w3, monkeypatch):
8994
pkg = w3.pm.get_package_from_uri(dummy_standard_token_uri)
9095
assert isinstance(pkg, Package)
9196
assert pkg.name == "standard-token"
97+
98+
99+
@pytest.fixture
100+
def tmp_ethpmdir(tmp_path):
101+
owned_manifest = get_ethpm_manifest("owned", "1.0.0.json")
102+
ethpmdir = tmp_path / '_ethpm_packages'
103+
ethpmdir.mkdir()
104+
owned_dir = ethpmdir / 'owned'
105+
owned_dir.mkdir()
106+
manifest = owned_dir / 'manifest.json'
107+
manifest.touch()
108+
manifest.write_text(json.dumps(owned_manifest, sort_keys=True, separators=(",", ":")))
109+
return ethpmdir
110+
111+
112+
def test_get_local_package(w3, tmp_ethpmdir):
113+
pkg = w3.pm.get_local_package("owned", tmp_ethpmdir)
114+
assert isinstance(pkg, Package)
115+
assert pkg.name == "owned"
116+
117+
118+
def test_get_local_package_with_invalid_ethpmdir(w3, tmp_path):
119+
invalid_ethpmdir = tmp_path / 'invalid'
120+
invalid_ethpmdir.mkdir()
121+
with pytest.raises(PMError, match="not a valid ethPM packages directory."):
122+
w3.pm.get_local_package("owned", invalid_ethpmdir)
123+
124+
125+
def test_get_local_package_with_uninstalled_package(w3, tmp_ethpmdir):
126+
with pytest.raises(PMError, match="Package: safe-math not found in "):
127+
w3.pm.get_local_package("safe-math", tmp_ethpmdir)

web3/pm.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
abstractmethod,
44
)
55
import json
6+
from pathlib import (
7+
Path,
8+
)
69
from typing import (
710
Any,
811
Dict,
@@ -421,6 +424,33 @@ def get_package_from_uri(self, manifest_uri: URI) -> Package:
421424
"""
422425
return Package.from_uri(manifest_uri, self.web3)
423426

427+
def get_local_package(self, package_name: str, ethpm_dir: Path = None) -> Package:
428+
"""
429+
Returns a `Package <https://github.com/ethpm/py-ethpm/blob/master/ethpm/package.py>`__
430+
instance built with the Manifest found at the package name in your local ethpm_dir.
431+
432+
* Parameters:
433+
* ``package_name``: Must be the name of a package installed locally.
434+
* ``ethpm_dir``: Path pointing to the target ethpm directory (optional).
435+
"""
436+
if not ethpm_dir:
437+
ethpm_dir = Path.cwd() / '_ethpm_packages'
438+
439+
if not ethpm_dir.name == "_ethpm_packages" or not ethpm_dir.is_dir():
440+
raise PMError(f"{ethpm_dir} is not a valid ethPM packages directory.")
441+
442+
local_packages = [pkg.name for pkg in ethpm_dir.iterdir() if pkg.is_dir()]
443+
if package_name not in local_packages:
444+
raise PMError(
445+
f"Package: {package_name} not found in {ethpm_dir}. "
446+
f"Available packages include: {local_packages}."
447+
)
448+
449+
target_manifest = json.loads(
450+
(ethpm_dir / package_name / "manifest.json").read_text()
451+
)
452+
return self.get_package_from_manifest(target_manifest)
453+
424454
def set_registry(self, address: Address) -> None:
425455
"""
426456
Sets the current registry used in ``web3.pm`` functions that read/write to an on-chain
@@ -488,13 +518,13 @@ def release_package(
488518
validate_raw_manifest_format(raw_manifest)
489519
manifest = json.loads(raw_manifest)
490520
validate_manifest_against_schema(manifest)
491-
if package_name != manifest['package_name']:
521+
if package_name != manifest["package_name"]:
492522
raise ManifestValidationError(
493523
f"Provided package name: {package_name} does not match the package name "
494524
f"found in the manifest: {manifest['package_name']}."
495525
)
496526

497-
if version != manifest['version']:
527+
if version != manifest["version"]:
498528
raise ManifestValidationError(
499529
f"Provided package version: {version} does not match the package version "
500530
f"found in the manifest: {manifest['version']}."

0 commit comments

Comments
 (0)