From e16e5cd0a67012feb8ea4bdf316dbce943c56cc2 Mon Sep 17 00:00:00 2001 From: Rob Crittenden Date: May 02 2018 12:12:11 +0000 Subject: Use a regex in installutils.get_directive instead of line splitting This will allow for whitespace around the separator and changes the default space separator into white space (space + tabs) to be more generic and work better on Ubuntu which uses tabs in its Apache configuration. https://pagure.io/freeipa/issue/7490 Signed-off-by: Rob Crittenden Reviewed-By: Florence Blanc-Renaud --- diff --git a/ipaserver/install/installutils.py b/ipaserver/install/installutils.py index fbec40e..9cee1a5 100644 --- a/ipaserver/install/installutils.py +++ b/ipaserver/install/installutils.py @@ -605,13 +605,20 @@ def get_directive(filename, directive, separator=' '): :returns: The (unquoted) value if the directive was found, None otherwise """ + # Special case: consider space as "white space" so tabs are allowed + if separator == ' ': + separator = '[ \t]+' + fd = open(filename, "r") for line in fd: if line.lstrip().startswith(directive): line = line.strip() - (directive, sep, value) = line.partition(separator) - if not sep or not value: + match = re.match(r'{}\s*{}\s*(.*)'.format(directive, separator), + line) + if match: + value = match.group(1) + else: raise ValueError("Malformed directive: {}".format(line)) result = unquote_directive_value(value.strip(), '"') diff --git a/ipatests/test_ipaserver/test_install/test_installutils.py b/ipatests/test_ipaserver/test_install/test_installutils.py index 0a426fe..757c3ed 100644 --- a/ipatests/test_ipaserver/test_install/test_installutils.py +++ b/ipatests/test_ipaserver/test_install/test_installutils.py @@ -98,6 +98,30 @@ class test_set_directive(object): os.remove(filename) +class test_get_directive(object): + def test_get_directive(self, tmpdir): + configfile = tmpdir.join('config') + configfile.write(''.join(EXAMPLE_CONFIG)) + + assert '1' == installutils.get_directive(str(configfile), + 'foo', + separator='=') + assert '2' == installutils.get_directive(str(configfile), + 'foobar', + separator='=') + + +class test_get_directive_whitespace(object): + def test_get_directive(self, tmpdir): + configfile = tmpdir.join('config') + configfile.write(''.join(WHITESPACE_CONFIG)) + + assert '1' == installutils.get_directive(str(configfile), + 'foo') + assert '2' == installutils.get_directive(str(configfile), + 'foobar') + + def test_directivesetter(tempdir): filename = os.path.join(tempdir, 'example.conf') with open(filename, 'w') as f: