From 5d238149cebd86a48f3c8592be294f30077b35ef Mon Sep 17 00:00:00 2001 From: tecnovert Date: Thu, 25 Jul 2019 20:30:01 +0200 Subject: [PATCH] prepare: Add -disablecoin option. --- bin/basicswap_prepare.py | 34 ++++++++++++++++++++++++++++++++++ tests/test_prepare.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/bin/basicswap_prepare.py b/bin/basicswap_prepare.py index 8ce38d1..a11672b 100644 --- a/bin/basicswap_prepare.py +++ b/bin/basicswap_prepare.py @@ -240,6 +240,7 @@ def printHelp(): logger.info('--withcoin= Prepare system to run daemon for coin.') logger.info('--withoutcoin= Do not prepare system to run daemon for coin.') logger.info('--addcoin= Add coin to existing setup.') + logger.info('--disablecoin= Make coin inactive.') logger.info('--preparebinonly Don\'t prepare settings or datadirs.') logger.info('\n' + 'Known coins: %s', ', '.join(known_coins.keys())) @@ -281,6 +282,7 @@ def main(): prepare_bin_only = False with_coins = {'particl', 'litecoin'} add_coin = '' + disable_coin = '' for v in sys.argv[1:]: if len(v) < 2 or v[0] != '-': @@ -334,6 +336,11 @@ def main(): add_coin = s[1] with_coins = [add_coin, ] continue + if name == 'disablecoin': + if s[1] not in known_coins: + exitWithError('Unknown coin {}'.format(s[1])) + disable_coin = s[1] + continue exitWithError('Unknown argument {}'.format(v)) @@ -388,6 +395,24 @@ def main(): } } + if disable_coin != '': + logger.info('Disabling coin: %s', disable_coin) + if not os.path.exists(config_path): + exitWithError('{} does not exist'.format(config_path)) + with open(config_path) as fs: + settings = json.load(fs) + + if disable_coin not in settings['chainclients']: + exitWithError('{} has not been prepared'.format(disable_coin)) + settings['chainclients'][disable_coin]['connection_type'] = 'none' + settings['chainclients'][disable_coin]['manage_daemon'] = False + + with open(config_path, 'w') as fp: + json.dump(settings, fp, indent=4) + + logger.info('Done.') + return 0 + if add_coin != '': logger.info('Adding coin: %s', add_coin) if not os.path.exists(config_path): @@ -396,6 +421,15 @@ def main(): settings = json.load(fs) if add_coin in settings['chainclients']: + coin_settings = settings['chainclients'][add_coin] + if coin_settings['connection_type'] == 'none' and coin_settings['manage_daemon'] is False: + logger.info('Enabling coin: %s', add_coin) + coin_settings['connection_type'] = 'rpc' + coin_settings['manage_daemon'] = True + with open(config_path, 'w') as fp: + json.dump(settings, fp, indent=4) + logger.info('Done.') + return 0 exitWithError('{} is already in the settings file'.format(add_coin)) settings['chainclients'][add_coin] = chainclients[add_coin] diff --git a/tests/test_prepare.py b/tests/test_prepare.py index 238803b..4e6acc7 100644 --- a/tests/test_prepare.py +++ b/tests/test_prepare.py @@ -12,6 +12,7 @@ from unittest.mock import patch from io import StringIO import logging import shutil +import json import bin.basicswap_prepare as prepareSystem test_path = os.path.expanduser('~/test_basicswap') @@ -30,13 +31,15 @@ class Test(unittest.TestCase): except Exception as e: logger.warning('tearDownClass %s', str(e)) - def test_no_overwrite(self): + def test(self): testargs = ['basicswap-prepare', '-datadir=' + test_path] with patch.object(sys, 'argv', testargs): prepareSystem.main() - self.assertTrue(os.path.exists(os.path.join(test_path, 'basicswap.json'))) + config_path = os.path.join(test_path, 'basicswap.json') + self.assertTrue(os.path.exists(config_path)) + logger.info('Test no overwrite') testargs = ['basicswap-prepare', '-datadir=' + test_path] with patch('sys.stderr', new=StringIO()) as fake_stderr: with patch.object(sys, 'argv', testargs): @@ -47,6 +50,30 @@ class Test(unittest.TestCase): logger.info('fake_stderr.getvalue() %s', fake_stderr.getvalue()) self.assertTrue('exists, exiting' in fake_stderr.getvalue()) + logger.info('Test addcoin new') + testargs = ['basicswap-prepare', '-datadir=' + test_path, '-addcoin=namecoin'] + with patch.object(sys, 'argv', testargs): + prepareSystem.main() + with open(config_path) as fs: + settings = json.load(fs) + self.assertTrue(settings['chainclients']['namecoin']['connection_type'] == 'rpc') + + logger.info('Test disablecoin') + testargs = ['basicswap-prepare', '-datadir=' + test_path, '-disablecoin=namecoin'] + with patch.object(sys, 'argv', testargs): + prepareSystem.main() + with open(config_path) as fs: + settings = json.load(fs) + self.assertTrue(settings['chainclients']['namecoin']['connection_type'] == 'none') + + logger.info('Test addcoin existing') + testargs = ['basicswap-prepare', '-datadir=' + test_path, '-disablecoin=namecoin'] + with patch.object(sys, 'argv', testargs): + prepareSystem.main() + with open(config_path) as fs: + settings = json.load(fs) + self.assertTrue(settings['chainclients']['namecoin']['connection_type'] == 'rpc') + if __name__ == '__main__': unittest.main()