2022-04-27 11:43:42 +02:00
# SPDX-License-Identifier: GPL-3.0-or-later
# standard imports
import os
import logging
import sys
import importlib
# external imports
import chainlib . cli
2022-04-27 11:59:40 +02:00
from shep . persist import PersistedState
2022-04-27 11:43:42 +02:00
# local imports
import chainsyncer . cli
from chainsyncer . settings import ChainsyncerSettings
2022-04-27 11:59:40 +02:00
from chainsyncer . filter import FilterState
2022-04-27 11:43:42 +02:00
logging . basicConfig ( level = logging . WARNING )
logg = logging . getLogger ( )
arg_flags = chainlib . cli . argflag_std_base | chainlib . cli . Flag . CHAIN_SPEC
argparser = chainlib . cli . ArgumentParser ( arg_flags )
argparser . add_argument ( ' --state-dir ' , type = str , dest = ' state_dir ' , help = ' State directory ' )
2022-04-27 11:59:40 +02:00
argparser . add_positional ( ' action ' , type = str , help = ' Action to take on lock. Repeat means re-run the locked filter. Continue means resume execution for next filter. ' )
2022-04-27 11:43:42 +02:00
sync_flags = chainsyncer . cli . SyncFlag . RANGE | chainsyncer . cli . SyncFlag . HEAD
chainsyncer . cli . process_flags ( argparser , sync_flags )
args = argparser . parse_args ( )
base_config_dir = chainsyncer . cli . config_dir ,
config = chainlib . cli . Config . from_args ( args , arg_flags , base_config_dir = base_config_dir )
config = chainsyncer . cli . process_config ( config , args , sync_flags )
config . add ( args . state_dir , ' _STATE_DIR ' , False )
logg . debug ( ' config loaded: \n {} ' . format ( config ) )
settings = ChainsyncerSettings ( )
settings . process_sync_backend ( config )
def main ( ) :
if settings . get ( ' SYNCER_BACKEND ' ) == ' mem ' :
raise ValueError ( ' cannot unlock volatile state store ' )
if settings . get ( ' SYNCER_BACKEND ' ) == ' fs ' :
2022-04-27 11:59:40 +02:00
syncer_store_module = importlib . import_module ( ' shep.store.file ' )
syncer_store_class = getattr ( syncer_store_module , ' SimpleFileStoreFactory ' )
2022-04-27 11:43:42 +02:00
elif settings . get ( ' SYNCER_BACKEND ' ) == ' rocksdb ' :
2022-04-27 11:59:40 +02:00
syncer_store_module = importlib . import_module ( ' shep.store.rocksdb ' )
syncer_store_class = getattr ( syncer_store_module , ' RocksdbStoreFactory ' )
2022-04-27 11:43:42 +02:00
else :
2022-04-27 11:59:40 +02:00
raise NotImplementedError ( ' cannot use backend: {} ' . format ( config . get ( ' SYNCER_BACKEND ' ) ) )
2022-04-27 11:43:42 +02:00
2022-04-27 11:59:40 +02:00
state_dir = config . get ( ' _STATE_DIR ' )
2022-04-27 11:43:42 +02:00
2022-04-27 11:59:40 +02:00
factory = syncer_store_class ( state_dir )
base_state = PersistedState ( factory . add , 0 , check_alias = False )
state = FilterState ( base_state , scan = True )
print ( state )
2022-04-27 11:43:42 +02:00
if __name__ == ' __main__ ' :
main ( )