예제 #1
0
    def generate_client_library(self):

        if os.path.isabs(self.args.protodir):
            client_libraries_base_dir_path = PurePath(self.args.protodir)
        else:
            cur_dir_path = PurePath(os.getcwd())
            client_libraries_base_dir_path = cur_dir_path.joinpath(self.args.protodir)

        os.makedirs(client_libraries_base_dir_path, exist_ok = True)

        # Create service client libraries path
        library_language = self.args.language
        library_org_id = self.args.org_id
        library_service_id = self.args.service_id

        library_dir_path = client_libraries_base_dir_path.joinpath(library_org_id, library_service_id, library_language)

        metadata = self._get_service_metadata_from_registry()
        model_ipfs_hash = metadata["model_ipfs_hash"]

        with TemporaryDirectory() as temp_dir: 
            temp_dir_path = PurePath(temp_dir)
            proto_temp_dir_path = temp_dir_path.joinpath(library_org_id, library_service_id, library_language)
            safe_extract_proto_from_ipfs(self._get_ipfs_client(), model_ipfs_hash, proto_temp_dir_path)

        # Compile proto files
            compile_proto(Path(proto_temp_dir_path), library_dir_path, target_language=self.args.language)

        self._printout('client libraries for service with id "{}" in org with id "{}" generated at {}'.format(library_service_id, library_org_id, library_dir_path))
예제 #2
0
def install_and_compile_proto():
    import snet.snet_cli
    from snet.snet_cli.utils import compile_proto as compile_proto
    from pathlib import Path
    proto_dir = Path(__file__).absolute().parent.joinpath("snet", "snet_cli", "resources", "proto")
    dest_dir = Path(snet.snet_cli.__file__).absolute().parent.joinpath("resources", "proto")
    print(proto_dir, "->", dest_dir)
    for fn in proto_dir.glob('*.proto'):
        print("Compiling protobuf", fn)
        compile_proto(proto_dir, dest_dir, proto_file=fn)
예제 #3
0
    def _get_stub_and_request_classes(self, service_name):
        """ import protobuf and return stub and request class """
        # Compile protobuf if needed
        codegen_dir = Path.home().joinpath(".snet", "mpe_client", "control_service")
        proto_dir = RESOURCES_PATH.joinpath("proto")
        if (not codegen_dir.joinpath("control_service_pb2.py").is_file()):
            compile_proto(proto_dir, codegen_dir,
                          proto_file="control_service.proto")

        stub_class, request_class, _ = import_protobuf_from_dir(
            codegen_dir, service_name)
        return stub_class, request_class
예제 #4
0
    def _init_or_update_service_if_needed(self, metadata,
                                          service_registration):
        # if service was already initialized and metadataURI hasn't changed we do nothing
        if self.is_service_initialized():
            if self.is_service_metadataURI_has_changed(service_registration):
                self._printerr(
                    "# Service with org_id=%s and service_id=%s was updated" %
                    (self.args.org_id, self.args.service_id))
                self._printerr(
                    "# ATTENTION!!! price or other paramaters might have been changed!\n"
                )
            else:
                return  # we do nothing
        self._printerr("# Initilize service with org_id=%s and service_id=%s" %
                       (self.args.org_id, self.args.service_id))
        self._check_mpe_address_metadata(metadata)
        service_dir = self.get_service_spec_dir(self.args.org_id,
                                                self.args.service_id)

        # remove old service_dir
        # it is relatevely safe to remove service_dir because we know that service_dir = self.get_service_spec_dir() so it is not a normal dir
        if (os.path.exists(service_dir)):
            shutil.rmtree(service_dir)

        os.makedirs(service_dir, mode=0o700)
        try:
            spec_dir = os.path.join(service_dir, "service_spec")
            os.makedirs(spec_dir, mode=0o700)
            safe_extract_proto_from_ipfs(self._get_ipfs_client(),
                                         metadata["model_ipfs_hash"], spec_dir)

            # compile .proto files
            if (not compile_proto(Path(spec_dir), service_dir)):
                raise Exception("Fail to compile %s/*.proto" % spec_dir)

            # save service_metadata.json in channel_dir
            metadata.save_pretty(
                os.path.join(service_dir, "service_metadata.json"))
        except Exception as e:
            # it is secure to remove channel_dir, because we've created it
            print(e)
            shutil.rmtree(service_dir)
            raise
        self._save_service_info(self.args.org_id, self.args.service_id,
                                service_registration)
예제 #5
0
from concurrent import futures
import time
import web3

from snet_cli.config import Config

from snet.snet_cli.mpe_channel_command import MPEChannelCommand
from snet.snet_cli.utils import compile_proto, DefaultAttributeObject


compile_proto("../service_spec1", ".", proto_file = "ExampleService.proto")
compile_proto("../../../snet/snet_cli/resources/proto/", ".", proto_file = "state_service.proto")
compile_proto("../../../snet/snet_cli/resources/proto/", ".", proto_file = "control_service.proto")
PRICE = 10000

import grpc

import ExampleService_pb2
import ExampleService_pb2_grpc

import state_service_pb2
import state_service_pb2_grpc

import control_service_pb2
import control_service_pb2_grpc

payments_unclaimed   = dict()
payments_in_progress = dict()

# we use MPEChannelCommand._get_channel_state_from_blockchain to get channel state from blockchain
# we need it to remove already claimed payments from payments_in_progress