prepare: Add -disablecoin option.

This commit is contained in:
tecnovert 2019-07-25 20:30:01 +02:00
parent 95729275b6
commit 5d238149ce
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
2 changed files with 63 additions and 2 deletions

View file

@ -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]

View file

@ -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()