|
3 | 3 | abstractmethod,
|
4 | 4 | )
|
5 | 5 | import json
|
| 6 | +from pathlib import ( |
| 7 | + Path, |
| 8 | +) |
6 | 9 | from typing import (
|
7 | 10 | Any,
|
8 | 11 | Dict,
|
@@ -421,6 +424,33 @@ def get_package_from_uri(self, manifest_uri: URI) -> Package:
|
421 | 424 | """
|
422 | 425 | return Package.from_uri(manifest_uri, self.web3)
|
423 | 426 |
|
| 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 | + |
424 | 454 | def set_registry(self, address: Address) -> None:
|
425 | 455 | """
|
426 | 456 | Sets the current registry used in ``web3.pm`` functions that read/write to an on-chain
|
@@ -488,13 +518,13 @@ def release_package(
|
488 | 518 | validate_raw_manifest_format(raw_manifest)
|
489 | 519 | manifest = json.loads(raw_manifest)
|
490 | 520 | validate_manifest_against_schema(manifest)
|
491 |
| - if package_name != manifest['package_name']: |
| 521 | + if package_name != manifest["package_name"]: |
492 | 522 | raise ManifestValidationError(
|
493 | 523 | f"Provided package name: {package_name} does not match the package name "
|
494 | 524 | f"found in the manifest: {manifest['package_name']}."
|
495 | 525 | )
|
496 | 526 |
|
497 |
| - if version != manifest['version']: |
| 527 | + if version != manifest["version"]: |
498 | 528 | raise ManifestValidationError(
|
499 | 529 | f"Provided package version: {version} does not match the package version "
|
500 | 530 | f"found in the manifest: {manifest['version']}."
|
|
0 commit comments