2021-10-09 21:04:11 +02:00
|
|
|
|
# standard imports
|
|
|
|
|
import logging
|
|
|
|
|
import importlib
|
2021-10-11 20:08:18 +02:00
|
|
|
|
import os
|
2021-10-09 21:04:11 +02:00
|
|
|
|
|
2021-10-14 15:43:23 +02:00
|
|
|
|
# external imports
|
|
|
|
|
from cic_types.ext.metadata import MetadataRequestsHandler
|
|
|
|
|
from cic_types.ext.metadata.signer import Signer as MetadataSigner
|
|
|
|
|
|
2021-10-09 21:04:11 +02:00
|
|
|
|
# local imports
|
2021-10-11 20:08:18 +02:00
|
|
|
|
from cic import (
|
|
|
|
|
Proof,
|
|
|
|
|
Processor,
|
|
|
|
|
)
|
2021-10-22 16:42:38 +02:00
|
|
|
|
from cic.output import (
|
|
|
|
|
HTTPWriter,
|
|
|
|
|
KeyedWriterFactory,
|
|
|
|
|
)
|
2021-10-14 15:43:23 +02:00
|
|
|
|
from cic.meta import (
|
|
|
|
|
Meta,
|
|
|
|
|
MetadataWriter,
|
|
|
|
|
)
|
2021-10-09 21:04:11 +02:00
|
|
|
|
from cic.attachment import Attachment
|
|
|
|
|
from cic.network import Network
|
2021-10-10 14:49:22 +02:00
|
|
|
|
from cic.token import Token
|
2022-02-10 09:22:21 +01:00
|
|
|
|
from typing import Optional
|
2021-10-09 21:04:11 +02:00
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_args(argparser):
|
|
|
|
|
argparser.add_argument('-d', '--directory', type=str, dest='directory', default='.', help='directory')
|
2021-10-12 08:39:20 +02:00
|
|
|
|
argparser.add_argument('-o', '--output-directory', type=str, dest='output_directory', help='output directory')
|
2021-10-19 11:18:22 +02:00
|
|
|
|
argparser.add_argument('--metadata-endpoint', dest='metadata_endpoint', type=str, help='metadata endpoint to interact with')
|
2021-10-11 20:08:18 +02:00
|
|
|
|
argparser.add_argument('-y', '--signer', type=str, dest='y', help='target-specific signer to use for export')
|
2021-10-11 21:18:51 +02:00
|
|
|
|
argparser.add_argument('-p', type=str, help='RPC endpoint')
|
2021-10-09 21:04:11 +02:00
|
|
|
|
argparser.add_argument('target', type=str, help='target network type')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_args(args):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2021-10-11 17:39:01 +02:00
|
|
|
|
def init_writers_from_config(config):
|
|
|
|
|
w = {
|
|
|
|
|
'meta': None,
|
|
|
|
|
'attachment': None,
|
|
|
|
|
'proof': None,
|
2021-10-11 20:08:18 +02:00
|
|
|
|
'ext': None,
|
2021-10-11 17:39:01 +02:00
|
|
|
|
}
|
|
|
|
|
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
|
2021-10-11 20:08:18 +02:00
|
|
|
|
|
2022-02-10 09:22:21 +01:00
|
|
|
|
EArgs = {'target': str, 'directory': str, 'output_directory': str, 'metadata_endpoint': Optional[str], 'y': str}
|
2021-10-11 17:39:01 +02:00
|
|
|
|
|
2022-02-10 09:22:21 +01:00
|
|
|
|
def execute(config, eargs: EArgs):
|
2021-10-09 21:04:11 +02:00
|
|
|
|
modname = 'cic.ext.{}'.format(eargs.target)
|
|
|
|
|
cmd_mod = importlib.import_module(modname)
|
|
|
|
|
|
2021-10-11 17:39:01 +02:00
|
|
|
|
writers = init_writers_from_config(config)
|
2021-10-19 11:18:22 +02:00
|
|
|
|
|
|
|
|
|
output_writer_path_meta = eargs.output_directory
|
|
|
|
|
if eargs.metadata_endpoint != None:
|
|
|
|
|
MetadataRequestsHandler.base_url = eargs.metadata_endpoint
|
|
|
|
|
MetadataSigner.gpg_path = os.path.join('/tmp')
|
2022-02-10 09:22:21 +01:00
|
|
|
|
MetadataSigner.key_file_path = '/home/will/grassroots/cic-internal-integration/apps/cic-ussd/tests/data/pgp/privatekeys_meta.asc'
|
2021-10-19 11:18:22 +02:00
|
|
|
|
MetadataSigner.gpg_passphrase = 'merman'
|
2021-10-22 16:42:38 +02:00
|
|
|
|
writers['proof'] = KeyedWriterFactory(MetadataWriter, HTTPWriter).new
|
|
|
|
|
writers['attachment'] = KeyedWriterFactory(None, HTTPWriter).new
|
2021-10-19 11:18:22 +02:00
|
|
|
|
writers['meta'] = MetadataWriter
|
|
|
|
|
output_writer_path_meta = eargs.metadata_endpoint
|
2021-10-14 15:43:23 +02:00
|
|
|
|
|
2021-10-10 14:49:22 +02:00
|
|
|
|
ct = Token(path=eargs.directory)
|
2021-10-19 11:18:22 +02:00
|
|
|
|
cm = Meta(path=eargs.directory, writer=writers['meta'](path=output_writer_path_meta))
|
|
|
|
|
ca = Attachment(path=eargs.directory, writer=writers['attachment'](path=output_writer_path_meta))
|
|
|
|
|
cp = Proof(path=eargs.directory, attachments=ca, writer=writers['proof'](path=output_writer_path_meta))
|
2021-10-10 14:49:22 +02:00
|
|
|
|
cn = Network(path=eargs.directory)
|
2021-10-09 21:04:11 +02:00
|
|
|
|
|
2021-10-22 19:41:01 +02:00
|
|
|
|
ca.load()
|
2021-10-10 14:49:22 +02:00
|
|
|
|
ct.load()
|
2021-10-09 21:04:11 +02:00
|
|
|
|
cp.load()
|
|
|
|
|
cm.load()
|
|
|
|
|
cn.load()
|
|
|
|
|
|
2021-10-11 21:18:51 +02:00
|
|
|
|
chain_spec = None
|
|
|
|
|
try:
|
|
|
|
|
chain_spec = config.get('CHAIN_SPEC')
|
|
|
|
|
except KeyError:
|
|
|
|
|
chain_spec = cn.chain_spec
|
|
|
|
|
config.add(chain_spec, 'CHAIN_SPEC', exists_ok=True)
|
|
|
|
|
logg.debug('CHAIN_SPEC config set to {}'.format(str(chain_spec)))
|
|
|
|
|
|
|
|
|
|
#signer = cmd_mod.parse_signer(eargs.y)
|
|
|
|
|
(rpc, signer) = cmd_mod.parse_adapter(config, eargs.y)
|
2021-10-11 20:08:18 +02:00
|
|
|
|
|
2021-10-11 17:39:01 +02:00
|
|
|
|
ref = cn.resource(eargs.target)
|
|
|
|
|
chain_spec = cn.chain_spec(eargs.target)
|
2021-10-11 20:08:18 +02:00
|
|
|
|
logg.debug('found reference {} chain spec {} for target {}'.format(ref['contents'], chain_spec, eargs.target))
|
2021-10-12 11:27:26 +02:00
|
|
|
|
c = getattr(cmd_mod, 'new')(chain_spec, ref['contents'], cp, signer_hint=signer, rpc=rpc, outputs_writer=writers['ext'](path=eargs.output_directory))
|
2021-10-11 20:08:18 +02:00
|
|
|
|
c.apply_token(ct)
|
|
|
|
|
|
2021-10-12 11:27:26 +02:00
|
|
|
|
p = Processor(proof=cp, attachment=ca, metadata=cm, extensions=[c])
|
|
|
|
|
p.process()
|