Let TestProcessProto configure settings for process it runs:
BMConfigParser now saves file path which it reads and saves to it.
This commit is contained in:
parent
942259dbf9
commit
1c4c98f360
|
@ -7,7 +7,6 @@ import shutil
|
||||||
import os
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import state
|
|
||||||
from singleton import Singleton
|
from singleton import Singleton
|
||||||
|
|
||||||
BMConfigDefaults = {
|
BMConfigDefaults = {
|
||||||
|
@ -99,7 +98,10 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
|
||||||
lambda x: x.startswith('BM-'), BMConfigParser().sections())
|
lambda x: x.startswith('BM-'), BMConfigParser().sections())
|
||||||
|
|
||||||
def read(self, filenames):
|
def read(self, filenames):
|
||||||
|
"""Read config from file or list of files"""
|
||||||
ConfigParser.ConfigParser.read(self, filenames)
|
ConfigParser.ConfigParser.read(self, filenames)
|
||||||
|
if isinstance(filenames, str):
|
||||||
|
self._src = filenames
|
||||||
for section in self.sections():
|
for section in self.sections():
|
||||||
for option in self.options(section):
|
for option in self.options(section):
|
||||||
try:
|
try:
|
||||||
|
@ -116,14 +118,16 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
|
||||||
except ConfigParser.InterpolationError:
|
except ConfigParser.InterpolationError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
def save(self):
|
def save(self, filename=None):
|
||||||
fileName = os.path.join(state.appdata, 'keys.dat')
|
"""Save config to filename or to file from which it was read"""
|
||||||
fileNameBak = '.'.join([
|
if not filename:
|
||||||
fileName, datetime.now().strftime("%Y%j%H%M%S%f"), 'bak'])
|
filename = self._src
|
||||||
|
filename_bak = '.'.join([
|
||||||
|
filename, datetime.now().strftime("%Y%j%H%M%S%f"), 'bak'])
|
||||||
# create a backup copy to prevent the accidental loss due to
|
# create a backup copy to prevent the accidental loss due to
|
||||||
# the disk write failure
|
# the disk write failure
|
||||||
try:
|
try:
|
||||||
shutil.copyfile(fileName, fileNameBak)
|
shutil.copyfile(filename, filename_bak)
|
||||||
# The backup succeeded.
|
# The backup succeeded.
|
||||||
fileNameExisted = True
|
fileNameExisted = True
|
||||||
except (IOError, Exception):
|
except (IOError, Exception):
|
||||||
|
@ -131,11 +135,11 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
|
||||||
# didn't exist before.
|
# didn't exist before.
|
||||||
fileNameExisted = False
|
fileNameExisted = False
|
||||||
# write the file
|
# write the file
|
||||||
with open(fileName, 'wb') as configfile:
|
with open(filename, 'wb') as configfile:
|
||||||
self.write(configfile)
|
self.write(configfile)
|
||||||
# delete the backup
|
# delete the backup
|
||||||
if fileNameExisted:
|
if fileNameExisted:
|
||||||
os.remove(fileNameBak)
|
os.remove(filename_bak)
|
||||||
|
|
||||||
def validate(self, section, option, value):
|
def validate(self, section, option, value):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -11,6 +11,8 @@ import unittest
|
||||||
|
|
||||||
import psutil
|
import psutil
|
||||||
|
|
||||||
|
from pybitmessage.bmconfigparser import BMConfigParser
|
||||||
|
|
||||||
|
|
||||||
def put_signal_file(path, filename):
|
def put_signal_file(path, filename):
|
||||||
"""Creates file, presence of which is a signal about some event."""
|
"""Creates file, presence of which is a signal about some event."""
|
||||||
|
@ -28,12 +30,28 @@ class TestProcessProto(unittest.TestCase):
|
||||||
'keys.dat', 'debug.log', 'messages.dat', 'knownnodes.dat',
|
'keys.dat', 'debug.log', 'messages.dat', 'knownnodes.dat',
|
||||||
'.api_started', 'unittest.lock'
|
'.api_started', 'unittest.lock'
|
||||||
)
|
)
|
||||||
|
_settings = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
"""Setup environment and start pybitmessage"""
|
"""Setup environment and start pybitmessage"""
|
||||||
cls.home = os.environ['BITMESSAGE_HOME'] = tempfile.gettempdir()
|
cls.home = os.environ['BITMESSAGE_HOME'] = tempfile.gettempdir()
|
||||||
put_signal_file(cls.home, 'unittest.lock')
|
put_signal_file(cls.home, 'unittest.lock')
|
||||||
|
cls._start_process()
|
||||||
|
# let pybitmessage generate default config
|
||||||
|
# and update it with settings configured for test case if any
|
||||||
|
if cls._settings:
|
||||||
|
cls._stop_process(10)
|
||||||
|
config = BMConfigParser()
|
||||||
|
config.read(os.path.join(cls.home, 'keys.dat'))
|
||||||
|
cls._cleanup_files()
|
||||||
|
for setting in cls._settings.iteritems():
|
||||||
|
config.set('bitmessagesettings', *setting)
|
||||||
|
config.save()
|
||||||
|
cls._start_process()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _start_process(cls):
|
||||||
subprocess.call(cls._process_cmd) # nosec
|
subprocess.call(cls._process_cmd) # nosec
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
cls.pid = int(cls._get_readline('singleton.lock'))
|
cls.pid = int(cls._get_readline('singleton.lock'))
|
||||||
|
|
Reference in New Issue
Block a user