from __future__ import annotations # standard imports import importlib import logging import os from typing import TYPE_CHECKING # local imports from cic import Processor, Proof from cic.attachment import Attachment from cic.meta import Meta, MetadataWriter from cic.network import Network from cic.output import HTTPWriter, KeyedWriterFactory from cic.token import Token # external imports from cic.MetaRequestHandler import MetadataRequestsHandler from cic_types.ext.metadata.signer import Signer as MetadataSigner logg = logging.getLogger(__name__) if TYPE_CHECKING: from cic.cmd.arg import CmdCtrl from cic.actions.types import Options def init_writers_from_config(config): w = { 'meta': None, 'attachment': None, 'proof': None, 'ext': None, } for v in w.keys(): k = 'CIC_CORE_{}_WRITER'.format(v.upper()) (d, c) = config.get(k).rsplit('.', maxsplit=1) m = importlib.import_module(d) o = getattr(m, c) w[v] = o return w def deploy(ctrl: CmdCtrl, target: str, contract_directory: str, keystore_directory: str, options: Options): auth_passphrase=options.auth_passphrase, auth_key_file_path=options.auth_keyfile_path, metadata_endpoint=options.metadata_endpoint, modname = f'cic.ext.{target}' cmd_mod = importlib.import_module(modname) writers = init_writers_from_config(ctrl.config) output_directory = os.path.join(contract_directory, 'out') output_writer_path_meta = output_directory if metadata_endpoint != None: MetadataRequestsHandler.base_url = metadata_endpoint MetadataRequestsHandler.ctrl = ctrl MetadataSigner.gpg_path = '/tmp' MetadataSigner.key_file_path = auth_key_file_path # This is a p2p key for add data to meta MetadataSigner.gpg_passphrase = auth_passphrase writers['proof'] = KeyedWriterFactory(MetadataWriter, None).new writers['attachment'] = KeyedWriterFactory(None, HTTPWriter).new writers['meta'] = MetadataWriter output_writer_path_meta = metadata_endpoint ct = Token(path=contract_directory) cm = Meta(path=contract_directory, writer=writers['meta'](path=output_writer_path_meta)) ca = Attachment(path=contract_directory, writer=writers['attachment'](path=output_writer_path_meta)) cp = Proof(path=contract_directory, attachments=ca, writer=writers['proof'](path=output_writer_path_meta)) cn = Network(path=contract_directory) ca.load() ct.load() cp.load() cm.load() cn.load() chain_spec = None try: chain_spec = ctrl.config.get('CHAIN_SPEC') except KeyError: chain_spec = cn.chain_spec ctrl.config.add(chain_spec, 'CHAIN_SPEC', exists_ok=True) logg.debug(f'CHAIN_SPEC config set to {str(chain_spec)}') (rpc, signer) = cmd_mod.parse_adapter(ctrl.config, keystore_directory) target_network_reference = cn.resource(target) chain_spec = cn.chain_spec(target) logg.debug(f'found reference {target_network_reference["contents"]} chain spec {chain_spec} for target {target}') c = getattr(cmd_mod, 'new')(chain_spec, target_network_reference['contents'], cp, signer_hint=signer, rpc=rpc, outputs_writer=writers['ext'](path=output_directory)) c.apply_token(ct) p = Processor(proof=cp, attachment=ca, metadata=cm, extensions=[c]) p.process()