diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index e0b160f..ce0fd7c 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -376,7 +376,7 @@ class BasicSwap(BaseApp): chain_client_settings = self.getChainClientSettings(coin) bindir = os.path.expanduser(chain_client_settings.get('bindir', '')) - datadir = os.path.expanduser(chain_client_settings.get('datadir', os.path.join(cfg.DATADIRS, chainparams[coin]['name']))) + datadir = os.path.expanduser(chain_client_settings.get('datadir', os.path.join(cfg.TEST_DATADIRS, chainparams[coin]['name']))) connection_type = chain_client_settings.get('connection_type', 'none') rpcauth = None @@ -2390,7 +2390,7 @@ class BasicSwap(BaseApp): try: if 'lookups' in data: self.settings['chainclients'][coin_name]['chain_lookups'] = data['lookups'] - settings_path = os.path.join(self.data_dir, 'basicswap.json') + settings_path = os.path.join(self.data_dir, cfg.CONFIG_FILENAME) shutil.copyfile(settings_path, settings_path + '.last') with open(settings_path, 'w') as fp: json.dump(self.settings, fp, indent=4) diff --git a/basicswap/config.py b/basicswap/config.py index 0abf38c..8f4ffd4 100644 --- a/basicswap/config.py +++ b/basicswap/config.py @@ -8,7 +8,9 @@ import os DEBUG = True -DATADIRS = os.path.expanduser(os.getenv('DATADIRS', '/tmp/basicswap')) +CONFIG_FILENAME = 'basicswap.json' +DEFAULT_DATADIR = '~/.basicswap' +TEST_DATADIRS = os.path.expanduser(os.getenv('DATADIRS', '/tmp/basicswap')) PARTICL_BINDIR = os.path.expanduser(os.getenv('PARTICL_BINDIR', '')) PARTICLD = os.getenv('PARTICLD', 'particld' + ('.exe' if os.name == 'nt' else '')) diff --git a/bin/basicswap_prepare.py b/bin/basicswap_prepare.py index 7c8ea32..649786a 100644 --- a/bin/basicswap_prepare.py +++ b/bin/basicswap_prepare.py @@ -249,7 +249,7 @@ def printHelp(): logger.info('Usage: basicswap-prepare ') logger.info('\n--help, -h Print help.') logger.info('--version, -v Print version.') - logger.info('--datadir=PATH Path to basicswap data directory, default:~/.basicswap.') + logger.info('--datadir=PATH Path to basicswap data directory, default:{}.'.format(cfg.DEFAULT_DATADIR)) logger.info('--bindir=PATH Path to cores directory, default:datadir/bin.') logger.info('--mainnet Run in mainnet mode.') logger.info('--testnet Run in testnet mode.') @@ -374,8 +374,7 @@ def main(): exitWithError('Unknown argument {}'.format(v)) if data_dir is None: - default_datadir = '~/.basicswap' - data_dir = os.path.join(os.path.expanduser(default_datadir)) + data_dir = os.path.join(os.path.expanduser(cfg.DEFAULT_DATADIR)) if bin_dir is None: bin_dir = os.path.join(data_dir, 'bin') @@ -387,7 +386,7 @@ def main(): if not os.path.exists(data_dir): os.makedirs(data_dir) - config_path = os.path.join(data_dir, 'basicswap.json') + config_path = os.path.join(data_dir, cfg.CONFIG_FILENAME) withchainclients = {} chainclients = { diff --git a/bin/basicswap_run.py b/bin/basicswap_run.py index 73db203..76ac68c 100644 --- a/bin/basicswap_run.py +++ b/bin/basicswap_run.py @@ -19,6 +19,7 @@ import signal import subprocess import logging +import basicswap.config as cfg from basicswap import __version__ from basicswap.basicswap import BasicSwap from basicswap.http_server import HttpThread @@ -50,7 +51,7 @@ def startDaemon(node_dir, bin_dir, daemon_bin, opts=[]): def runClient(fp, data_dir, chain): global swap_client - settings_path = os.path.join(data_dir, 'basicswap.json') + settings_path = os.path.join(data_dir, cfg.CONFIG_FILENAME) pids_path = os.path.join(data_dir, '.pids') if not os.path.exists(settings_path): @@ -153,7 +154,7 @@ def printHelp(): logger.info('Usage: basicswap-run ') logger.info('\n--help, -h Print help.') logger.info('--version, -v Print version.') - logger.info('--datadir=PATH Path to basicswap data directory, default:~/.basicswap.') + logger.info('--datadir=PATH Path to basicswap data directory, default:{}.'.format(cfg.DEFAULT_DATADIR)) logger.info('--mainnet Run in mainnet mode.') logger.info('--testnet Run in testnet mode.') logger.info('--regtest Run in regtest mode.') @@ -197,8 +198,7 @@ def main(): logger.warning('Unknown argument %s', v) if data_dir is None: - default_datadir = '~/.basicswap' - data_dir = os.path.join(os.path.expanduser(default_datadir)) + data_dir = os.path.join(os.path.expanduser(cfg.DEFAULT_DATADIR)) logger.info('Using datadir: %s', data_dir) logger.info('Chain: %s', chain) diff --git a/tests/basicswap/test_nmc.py b/tests/basicswap/test_nmc.py index c028f64..3e745b2 100644 --- a/tests/basicswap/test_nmc.py +++ b/tests/basicswap/test_nmc.py @@ -127,7 +127,7 @@ def prepareDir(datadir, nodeId, network_key, network_pubkey): nmcdatadir = os.path.join(datadir, str(NMC_NODE)) btcdatadir = os.path.join(datadir, str(BTC_NODE)) - settings_path = os.path.join(basicswap_dir, 'basicswap.json') + settings_path = os.path.join(basicswap_dir, cfg.CONFIG_FILENAME) settings = { 'zmqhost': 'tcp://127.0.0.1', 'zmqport': BASE_ZMQ_PORT + nodeId, @@ -171,7 +171,7 @@ def prepareDir(datadir, nodeId, network_key, network_pubkey): def startDaemon(nodeId, bin_dir=cfg.PARTICL_BINDIR, daemon_bin=cfg.PARTICLD): - node_dir = os.path.join(cfg.DATADIRS, str(nodeId)) + node_dir = os.path.join(cfg.TEST_DATADIRS, str(nodeId)) daemon_bin = os.path.join(bin_dir, daemon_bin) args = [daemon_bin, '-datadir=' + node_dir] @@ -180,15 +180,15 @@ def startDaemon(nodeId, bin_dir=cfg.PARTICL_BINDIR, daemon_bin=cfg.PARTICLD): def partRpc(cmd, node_id=0): - return callrpc_cli(cfg.PARTICL_BINDIR, os.path.join(cfg.DATADIRS, str(node_id)), 'regtest', cmd, cfg.PARTICL_CLI) + return callrpc_cli(cfg.PARTICL_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(node_id)), 'regtest', cmd, cfg.PARTICL_CLI) def btcRpc(cmd): - return callrpc_cli(cfg.BITCOIN_BINDIR, os.path.join(cfg.DATADIRS, str(BTC_NODE)), 'regtest', cmd, cfg.BITCOIN_CLI) + return callrpc_cli(cfg.BITCOIN_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(BTC_NODE)), 'regtest', cmd, cfg.BITCOIN_CLI) def nmcRpc(cmd): - return callrpc_cli(cfg.NAMECOIN_BINDIR, os.path.join(cfg.DATADIRS, str(NMC_NODE)), 'regtest', cmd, cfg.NAMECOIN_CLI) + return callrpc_cli(cfg.NAMECOIN_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(NMC_NODE)), 'regtest', cmd, cfg.NAMECOIN_CLI) def signal_handler(sig, frame): @@ -228,15 +228,15 @@ class Test(unittest.TestCase): cls.network_key = toWIF(PREFIX_SECRET_KEY_REGTEST, eckey.get_bytes()) cls.network_pubkey = eckey.get_pubkey().get_bytes().hex() - if os.path.isdir(cfg.DATADIRS): - logging.info('Removing ' + cfg.DATADIRS) - shutil.rmtree(cfg.DATADIRS) + if os.path.isdir(cfg.TEST_DATADIRS): + logging.info('Removing ' + cfg.TEST_DATADIRS) + shutil.rmtree(cfg.TEST_DATADIRS) for i in range(NUM_NODES): - prepareDir(cfg.DATADIRS, i, cls.network_key, cls.network_pubkey) + prepareDir(cfg.TEST_DATADIRS, i, cls.network_key, cls.network_pubkey) - prepareOtherDir(cfg.DATADIRS, NMC_NODE) - prepareOtherDir(cfg.DATADIRS, BTC_NODE, 'bitcoin.conf') + prepareOtherDir(cfg.TEST_DATADIRS, NMC_NODE) + prepareOtherDir(cfg.TEST_DATADIRS, BTC_NODE, 'bitcoin.conf') cls.daemons = [] cls.swap_clients = [] @@ -251,8 +251,8 @@ class Test(unittest.TestCase): logging.info('Started %s %d', cfg.PARTICLD, cls.daemons[-1].pid) time.sleep(1) for i in range(NUM_NODES): - basicswap_dir = os.path.join(os.path.join(cfg.DATADIRS, str(i)), 'basicswap') - settings_path = os.path.join(basicswap_dir, 'basicswap.json') + basicswap_dir = os.path.join(os.path.join(cfg.TEST_DATADIRS, str(i)), 'basicswap') + settings_path = os.path.join(basicswap_dir, cfg.CONFIG_FILENAME) with open(settings_path) as fs: settings = json.load(fs) fp = open(os.path.join(basicswap_dir, 'basicswap.log'), 'w') diff --git a/tests/basicswap/test_prepare.py b/tests/basicswap/test_prepare.py index 85d5a9d..5c31ba0 100644 --- a/tests/basicswap/test_prepare.py +++ b/tests/basicswap/test_prepare.py @@ -14,6 +14,7 @@ import logging import shutil import json +import basicswap.config as cfg import bin.basicswap_prepare as prepareSystem test_path = os.path.expanduser(os.getenv('TEST_PREPARE_PATH', '~/test_basicswap')) @@ -37,7 +38,7 @@ class Test(unittest.TestCase): with patch.object(sys, 'argv', testargs): prepareSystem.main() - config_path = os.path.join(test_path, 'basicswap.json') + config_path = os.path.join(test_path, cfg.CONFIG_FILENAME) self.assertTrue(os.path.exists(config_path)) logger.info('Test no overwrite') diff --git a/tests/basicswap/test_reload.py b/tests/basicswap/test_reload.py index e09e38a..3afb0c5 100644 --- a/tests/basicswap/test_reload.py +++ b/tests/basicswap/test_reload.py @@ -35,6 +35,7 @@ from basicswap.util import ( callrpc_cli, ) +import basicswap.config as cfg import bin.basicswap_prepare as prepareSystem import bin.basicswap_run as runSystem @@ -113,7 +114,7 @@ class Test(unittest.TestCase): for i in range(3): client_path = os.path.join(test_path, 'client{}'.format(i)) - config_path = os.path.join(client_path, 'basicswap.json') + config_path = os.path.join(client_path, cfg.CONFIG_FILENAME) try: shutil.rmtree(client_path) except Exception as ex: diff --git a/tests/basicswap/test_run.py b/tests/basicswap/test_run.py index fd453fb..bef4dc4 100644 --- a/tests/basicswap/test_run.py +++ b/tests/basicswap/test_run.py @@ -25,6 +25,7 @@ import signal import threading from urllib.request import urlopen +import basicswap.config as cfg from basicswap.basicswap import ( BasicSwap, Coins, @@ -46,8 +47,6 @@ from basicswap.http_server import ( HttpThread, ) -import basicswap.config as cfg - logger = logging.getLogger() logger.level = logging.DEBUG if not len(logger.handlers): @@ -130,7 +129,7 @@ def prepareDir(datadir, nodeId, network_key, network_pubkey): ltcdatadir = os.path.join(datadir, str(LTC_NODE)) btcdatadir = os.path.join(datadir, str(BTC_NODE)) - settings_path = os.path.join(basicswap_dir, 'basicswap.json') + settings_path = os.path.join(basicswap_dir, cfg.CONFIG_FILENAME) settings = { 'zmqhost': 'tcp://127.0.0.1', 'zmqport': BASE_ZMQ_PORT + nodeId, @@ -176,7 +175,7 @@ def prepareDir(datadir, nodeId, network_key, network_pubkey): def startDaemon(nodeId, bin_dir=cfg.PARTICL_BINDIR, daemon_bin=cfg.PARTICLD): - node_dir = os.path.join(cfg.DATADIRS, str(nodeId)) + node_dir = os.path.join(cfg.TEST_DATADIRS, str(nodeId)) daemon_bin = os.path.join(bin_dir, daemon_bin) args = [daemon_bin, '-datadir=' + node_dir] @@ -185,15 +184,15 @@ def startDaemon(nodeId, bin_dir=cfg.PARTICL_BINDIR, daemon_bin=cfg.PARTICLD): def partRpc(cmd, node_id=0): - return callrpc_cli(cfg.PARTICL_BINDIR, os.path.join(cfg.DATADIRS, str(node_id)), 'regtest', cmd, cfg.PARTICL_CLI) + return callrpc_cli(cfg.PARTICL_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(node_id)), 'regtest', cmd, cfg.PARTICL_CLI) def btcRpc(cmd): - return callrpc_cli(cfg.BITCOIN_BINDIR, os.path.join(cfg.DATADIRS, str(BTC_NODE)), 'regtest', cmd, cfg.BITCOIN_CLI) + return callrpc_cli(cfg.BITCOIN_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(BTC_NODE)), 'regtest', cmd, cfg.BITCOIN_CLI) def ltcRpc(cmd): - return callrpc_cli(cfg.LITECOIN_BINDIR, os.path.join(cfg.DATADIRS, str(LTC_NODE)), 'regtest', cmd, cfg.LITECOIN_CLI) + return callrpc_cli(cfg.LITECOIN_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(LTC_NODE)), 'regtest', cmd, cfg.LITECOIN_CLI) def signal_handler(sig, frame): @@ -242,15 +241,15 @@ class Test(unittest.TestCase): cls.network_key = toWIF(PREFIX_SECRET_KEY_REGTEST, eckey.get_bytes()) cls.network_pubkey = eckey.get_pubkey().get_bytes().hex() - if os.path.isdir(cfg.DATADIRS): - logging.info('Removing ' + cfg.DATADIRS) - shutil.rmtree(cfg.DATADIRS) + if os.path.isdir(cfg.TEST_DATADIRS): + logging.info('Removing ' + cfg.TEST_DATADIRS) + shutil.rmtree(cfg.TEST_DATADIRS) for i in range(NUM_NODES): - prepareDir(cfg.DATADIRS, i, cls.network_key, cls.network_pubkey) + prepareDir(cfg.TEST_DATADIRS, i, cls.network_key, cls.network_pubkey) - prepareOtherDir(cfg.DATADIRS, LTC_NODE) - prepareOtherDir(cfg.DATADIRS, BTC_NODE, 'bitcoin.conf') + prepareOtherDir(cfg.TEST_DATADIRS, LTC_NODE) + prepareOtherDir(cfg.TEST_DATADIRS, BTC_NODE, 'bitcoin.conf') cls.daemons = [] cls.swap_clients = [] @@ -265,8 +264,8 @@ class Test(unittest.TestCase): logging.info('Started %s %d', cfg.PARTICLD, cls.daemons[-1].pid) time.sleep(1) for i in range(NUM_NODES): - basicswap_dir = os.path.join(os.path.join(cfg.DATADIRS, str(i)), 'basicswap') - settings_path = os.path.join(basicswap_dir, 'basicswap.json') + basicswap_dir = os.path.join(os.path.join(cfg.TEST_DATADIRS, str(i)), 'basicswap') + settings_path = os.path.join(basicswap_dir, cfg.CONFIG_FILENAME) with open(settings_path) as fs: settings = json.load(fs) fp = open(os.path.join(basicswap_dir, 'basicswap.log'), 'w')