using ConfigParser.read_file for PY3

This commit is contained in:
Yu Ming Zhu 2018-11-06 00:15:03 +00:00 committed by Mike McLean
parent 8cc1c93dc2
commit 9ddae41877
6 changed files with 76 additions and 23 deletions

View file

@ -5823,10 +5823,7 @@ def handle_image_build(options, session, args):
if not os.path.exists(task_options.config): if not os.path.exists(task_options.config):
parser.error(_("%s not found!" % task_options.config)) parser.error(_("%s not found!" % task_options.config))
section = 'image-build' section = 'image-build'
config = six.moves.configparser.ConfigParser() config = koji.read_config_files(task_options.config)
conf_fd = open(task_options.config)
config.readfp(conf_fd)
conf_fd.close()
if not config.has_section(section): if not config.has_section(section):
parser.error(_("single section called [%s] is required" % section)) parser.error(_("single section called [%s] is required" % section))
# pluck out the positional arguments first # pluck out the positional arguments first

View file

@ -1709,10 +1709,7 @@ def read_config(profile_name, user_config=None):
# Load the configs in a particular order # Load the configs in a particular order
got_conf = False got_conf = False
for configFile in configs: for configFile in configs:
f = open(configFile) config = read_config_files(configFile)
config = six.moves.configparser.ConfigParser()
config.readfp(f)
f.close()
if config.has_section(profile_name): if config.has_section(profile_name):
got_conf = True got_conf = True
for name, value in config.items(profile_name): for name, value in config.items(profile_name):
@ -1806,6 +1803,21 @@ def get_profile_module(profile_name, config=None):
return mod return mod
def read_config_files(config_files, parser=None):
if not isinstance(config_files, (list, tuple)):
config_files = [config_files]
if parser is None:
parser = six.moves.configparser.ConfigParser
config = parser()
for config_file in config_files:
with open(config_file, 'r') as f:
if six.PY2:
config.readfp(f)
else:
config.read_file(f)
return config
class PathInfo(object): class PathInfo(object):
# ASCII numbers and upper- and lower-case letter for use in tmpdir() # ASCII numbers and upper- and lower-case letter for use in tmpdir()
ASCII_CHARS = [chr(i) for i in list(range(48, 58)) + list(range(65, 91)) + list(range(97, 123))] ASCII_CHARS = [chr(i) for i in list(range(48, 58)) + list(range(65, 91)) + list(range(97, 123))]

View file

@ -733,13 +733,7 @@ def parse_maven_params(confs, chain=False, scratch=False):
Return a map whose keys are package names and values are config parameters. Return a map whose keys are package names and values are config parameters.
""" """
if not isinstance(confs, (list, tuple)): config = koji.read_config_files(confs)
confs = [confs]
config = six.moves.configparser.ConfigParser()
for conf in confs:
conf_fd = open(conf)
config.readfp(conf_fd)
conf_fd.close()
builds = {} builds = {}
for package in config.sections(): for package in config.sections():
buildtype = 'maven' buildtype = 'maven'
@ -753,10 +747,12 @@ def parse_maven_params(confs, chain=False, scratch=False):
raise ValueError("A wrapper-rpm must depend on exactly one package") raise ValueError("A wrapper-rpm must depend on exactly one package")
else: else:
raise ValueError("Unsupported build type: %s" % buildtype) raise ValueError("Unsupported build type: %s" % buildtype)
if not 'scmurl' in params: if 'scmurl' not in params:
raise ValueError("%s is missing the scmurl parameter" % package) raise ValueError("%s is missing the scmurl parameter" % package)
builds[package] = params builds[package] = params
if not builds: if not builds:
if not isinstance(confs, (list, tuple)):
confs = [confs]
raise ValueError("No sections found in: %s" % ', '.join(confs)) raise ValueError("No sections found in: %s" % ', '.join(confs))
return builds return builds

View file

@ -270,10 +270,8 @@ def send_queued_msgs(cbtype, *args, **kws):
log = logging.getLogger('koji.plugin.protonmsg') log = logging.getLogger('koji.plugin.protonmsg')
global CONFIG global CONFIG
if not CONFIG: if not CONFIG:
conf = six.moves.configparser.SafeConfigParser() CONFIG = koji.read_config_files(CONFIG_FILE,
with open(CONFIG_FILE) as conffile: six.moves.configparser.SafeConfigParser)
conf.readfp(conffile)
CONFIG = conf
urls = CONFIG.get('broker', 'urls').split() urls = CONFIG.get('broker', 'urls').split()
test_mode = False test_mode = False
if CONFIG.has_option('broker', 'test_mode'): if CONFIG.has_option('broker', 'test_mode'):

View file

@ -158,6 +158,47 @@ class MiscFunctionTestCase(unittest.TestCase):
m.assert_not_called() m.assert_not_called()
class ConfigFileTestCase(unittest.TestCase):
"""Test config file reading functions"""
@mock_open()
@mock.patch("six.moves.configparser.ConfigParser", spec=True)
@mock.patch("six.moves.configparser.SafeConfigParser", spec=True)
def test_read_config_files(self, scp_clz, cp_clz, open_mock):
files = 'test1.conf'
conf = koji.read_config_files(files)
self.assertTrue(isinstance(conf,
six.moves.configparser.ConfigParser.__class__))
cp_clz.assert_called_once()
open_mock.assert_called_once_with(files, 'r')
if six.PY2:
cp_clz.return_value.readfp.assert_called_once()
else:
cp_clz.return_value.read_file.assert_called_once()
open_mock.reset_mock()
cp_clz.reset_mock()
files = ['test1.conf', 'test2.conf']
koji.read_config_files(files)
cp_clz.assert_called_once()
open_mock.assert_has_calls([call('test1.conf', 'r'),
call('test2.conf', 'r')],
any_order=True)
if six.PY2:
self.assertEqual(cp_clz.return_value.readfp.call_count, 2)
else:
self.assertEqual(cp_clz.return_value.read_file.call_count, 2)
open_mock.reset_mock()
cp_clz.reset_mock()
conf = koji.read_config_files(files,
six.moves.configparser.SafeConfigParser)
self.assertTrue(isinstance(conf,
six.moves.configparser.SafeConfigParser.__class__))
cp_clz.assert_not_called()
scp_clz.assert_called_once()
class MavenUtilTestCase(unittest.TestCase): class MavenUtilTestCase(unittest.TestCase):
"""Test maven relative functions""" """Test maven relative functions"""
maxDiff = None maxDiff = None
@ -494,7 +535,10 @@ class MavenUtilTestCase(unittest.TestCase):
config = six.moves.configparser.ConfigParser() config = six.moves.configparser.ConfigParser()
path = os.path.dirname(__file__) path = os.path.dirname(__file__)
with open(path + cfile, 'r') as conf_file: with open(path + cfile, 'r') as conf_file:
config.readfp(conf_file) if six.PY2:
config.readfp(conf_file)
else:
config.read_file(conf_file)
return config return config
def test_formatChangelog(self): def test_formatChangelog(self):

View file

@ -270,7 +270,10 @@ connect_timeout = 10
send_timeout = 60 send_timeout = 60
""") """)
conf = SafeConfigParser() conf = SafeConfigParser()
conf.readfp(confdata) if six.PY2:
conf.readfp(confdata)
else:
conf.read_file(confdata)
self.handler = protonmsg.TimeoutHandler('amqps://broker1.example.com:5671', [], conf) self.handler = protonmsg.TimeoutHandler('amqps://broker1.example.com:5671', [], conf)
@patch('protonmsg.SSLDomain') @patch('protonmsg.SSLDomain')
@ -291,7 +294,10 @@ connect_timeout = 10
send_timeout = 60 send_timeout = 60
""") """)
conf = SafeConfigParser() conf = SafeConfigParser()
conf.readfp(confdata) if six.PY2:
conf.readfp(confdata)
else:
conf.read_file(confdata)
handler = protonmsg.TimeoutHandler('amqp://broker1.example.com:5672', [], conf) handler = protonmsg.TimeoutHandler('amqp://broker1.example.com:5672', [], conf)
event = MagicMock() event = MagicMock()
handler.on_start(event) handler.on_start(event)