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):
parser.error(_("%s not found!" % task_options.config))
section = 'image-build'
config = six.moves.configparser.ConfigParser()
conf_fd = open(task_options.config)
config.readfp(conf_fd)
conf_fd.close()
config = koji.read_config_files(task_options.config)
if not config.has_section(section):
parser.error(_("single section called [%s] is required" % section))
# 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
got_conf = False
for configFile in configs:
f = open(configFile)
config = six.moves.configparser.ConfigParser()
config.readfp(f)
f.close()
config = read_config_files(configFile)
if config.has_section(profile_name):
got_conf = True
for name, value in config.items(profile_name):
@ -1806,6 +1803,21 @@ def get_profile_module(profile_name, config=None):
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):
# 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))]

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.
"""
if not isinstance(confs, (list, tuple)):
confs = [confs]
config = six.moves.configparser.ConfigParser()
for conf in confs:
conf_fd = open(conf)
config.readfp(conf_fd)
conf_fd.close()
config = koji.read_config_files(confs)
builds = {}
for package in config.sections():
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")
else:
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)
builds[package] = params
if not builds:
if not isinstance(confs, (list, tuple)):
confs = [confs]
raise ValueError("No sections found in: %s" % ', '.join(confs))
return builds

View file

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

View file

@ -158,6 +158,47 @@ class MiscFunctionTestCase(unittest.TestCase):
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):
"""Test maven relative functions"""
maxDiff = None
@ -494,7 +535,10 @@ class MavenUtilTestCase(unittest.TestCase):
config = six.moves.configparser.ConfigParser()
path = os.path.dirname(__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
def test_formatChangelog(self):

View file

@ -270,7 +270,10 @@ connect_timeout = 10
send_timeout = 60
""")
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)
@patch('protonmsg.SSLDomain')
@ -291,7 +294,10 @@ connect_timeout = 10
send_timeout = 60
""")
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)
event = MagicMock()
handler.on_start(event)