def pluck_ipfs_uris_from_manifest(uri: URI) -> Iterable[List[Any]]: manifest_contents = json.loads(resolve_uri_contents(uri)) yield pluck_ipfs_uris(manifest_contents) if "build_dependencies" in manifest_contents: for dependency_uri in manifest_contents["build_dependencies"].values(): yield pluck_ipfs_uris_from_manifest(dependency_uri)
def write_ipfs_uris_to_disk(ethpm_dir: Path, manifests: Dict[Address, Dict[str, str]]) -> None: all_manifest_uris = [ version_release_data["manifestURI"] for version_release_data in manifests.values() if is_supported_content_addressed_uri(version_release_data["manifestURI"]) ] base_ipfs_uris = [uri for uri in all_manifest_uris if is_ipfs_uri(uri)] nested_ipfs_uris = [ pluck_ipfs_uris_from_manifest(uri) for uri in all_manifest_uris ] all_ipfs_uris = set(flatten(nested_ipfs_uris) + base_ipfs_uris) # ex. # ipfs uri: QmdvZEW3AaUntDfFkcbdnYzeLAAeD4YFeixQsdmHF88T6Q # dir store: ethpmcli/Qm/dv/ZE/QmdvZEW3AaUntDfFkcbdnYzeLAAeD4YFeixQsdmHF88T6Q for uri in all_ipfs_uris: ipfs_hash = extract_ipfs_path_from_uri(uri) first_two_bytes_dir = ethpm_dir / ipfs_hash[0:2] second_two_bytes_dir = first_two_bytes_dir / ipfs_hash[2:4] third_two_bytes_dir = second_two_bytes_dir / ipfs_hash[4:6] asset_dest_path = third_two_bytes_dir / ipfs_hash if not asset_dest_path.is_file(): if not first_two_bytes_dir.is_dir(): first_two_bytes_dir.mkdir() if not second_two_bytes_dir.is_dir(): second_two_bytes_dir.mkdir() if not third_two_bytes_dir.is_dir(): third_two_bytes_dir.mkdir() asset_dest_path.touch() asset_dest_path.write_bytes(resolve_uri_contents(uri)) logger.info("%s written to\n %s.\n", uri, asset_dest_path)
def release_package(self, package_name: str, version: str, manifest_uri: str) -> bytes: """ Returns the release id generated by releasing a package on the current registry. Requires ``web3.PM`` to have a registry set. Requires ``web3.eth.defaultAccount`` to be the registry owner. * Parameters: * ``package_name``: Must be a valid package name, matching the given manifest. * ``version``: Must be a valid package version, matching the given manifest. * ``manifest_uri``: Must be a valid content-addressed URI. Currently, only IPFS and Github content-addressed URIs are supported. """ validate_is_supported_manifest_uri(manifest_uri) raw_manifest = to_text(resolve_uri_contents(manifest_uri)) validate_raw_manifest_format(raw_manifest) manifest = json.loads(raw_manifest) validate_manifest_against_schema(manifest) 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"]: raise ManifestValidationError( f"Provided package version: {version} does not match the package version " f"found in the manifest: {manifest['version']}.") self._validate_set_registry() return self.registry._release(package_name, version, manifest_uri)
def from_uri(cls, uri: URI, w3: Web3) -> "Package": """ Returns a Package object instantiated by a manifest located at a content-addressed URI. A valid ``Web3`` instance is also required. URI schemes supported: - IPFS `ipfs://Qm...` - HTTP `https://api.github.com/repos/:owner/:repo/git/blobs/:file_sha` - Registry `erc1319://registry.eth:1/greeter?version=1.0.0` .. code:: python OwnedPackage = Package.from_uri('ipfs://QmbeVyFLSuEUxiXKwSsEjef7icpdTdA4kGG9BcrJXKNKUW', w3) # noqa: E501 """ contents = to_text(resolve_uri_contents(uri)) validate_raw_manifest_format(contents) manifest = json.loads(contents) return cls(manifest, w3, uri)
def test_resolve_uri_contents_raises_exception_for_unsupported_schemes(uri): with pytest.raises(CannotHandleURI): resolve_uri_contents(uri)