From 3cb1ccb3b0a4f361d4d3ef667c60d5b6f4a23333 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 2 Apr 2019 15:17:52 +0200 Subject: [PATCH] Add option to remove lines from a file config_replace_variables() can now also remove lines from a file. Related: https://pagure.io/freeipa/issue/7860 Signed-off-by: Christian Heimes Reviewed-By: Alexander Bokovoy --- install/share/ds-ipa-env.conf.template | 0 ipapython/ipautil.py | 13 ++++++-- ipatests/conftest.py | 13 ++++++++ .../test_ipapython/test_directivesetter.py | 14 --------- ipatests/test_ipapython/test_ipautil.py | 30 +++++++++++++++++++ .../test_install/test_installutils.py | 13 -------- 6 files changed, 53 insertions(+), 30 deletions(-) create mode 100644 install/share/ds-ipa-env.conf.template diff --git a/install/share/ds-ipa-env.conf.template b/install/share/ds-ipa-env.conf.template new file mode 100644 index 000000000..e69de29bb diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py index 5d918df93..9da3f0790 100644 --- a/ipapython/ipautil.py +++ b/ipapython/ipautil.py @@ -1104,14 +1104,17 @@ def reverse_record_exists(ip_address): return True -def config_replace_variables(filepath, replacevars=dict(), appendvars=dict()): +def config_replace_variables(filepath, replacevars=dict(), appendvars=dict(), + removevars=None): """ Take a key=value based configuration file, and write new version - with certain values replaced or appended + with certain values replaced, appended, or removed. All (key,value) pairs from replacevars and appendvars that were not found in the configuration file, will be added there. + All entries in set removevars are removed. + It is responsibility of a caller to ensure that replacevars and appendvars do not overlap. @@ -1153,7 +1156,11 @@ $)''', re.VERBOSE) elif value.find(appendvars[option]) == -1: new_line = u"%s=%s %s\n" % (option, value, appendvars[option]) old_values[option] = value - new_config.write(new_line) + if removevars and option in removevars: + old_values[option] = value + new_line = None + if new_line is not None: + new_config.write(new_line) # Now add all options from replacevars and appendvars that were not found in the file new_vars = replacevars.copy() new_vars.update(appendvars) diff --git a/ipatests/conftest.py b/ipatests/conftest.py index dc1aec064..ebcbcd694 100644 --- a/ipatests/conftest.py +++ b/ipatests/conftest.py @@ -5,7 +5,9 @@ from __future__ import print_function import os import pprint +import shutil import sys +import tempfile import pytest @@ -145,3 +147,14 @@ def pytest_runtest_setup(item): # pylint: disable=no-member if pytest.config.option.skip_ipaapi: pytest.skip("Skip tests that needs an IPA API") + + +@pytest.fixture +def tempdir(request): + tempdir = tempfile.mkdtemp() + + def fin(): + shutil.rmtree(tempdir) + + request.addfinalizer(fin) + return tempdir diff --git a/ipatests/test_ipapython/test_directivesetter.py b/ipatests/test_ipapython/test_directivesetter.py index c52c959ba..08a30124b 100644 --- a/ipatests/test_ipapython/test_directivesetter.py +++ b/ipatests/test_ipapython/test_directivesetter.py @@ -4,11 +4,8 @@ from __future__ import absolute_import import os -import shutil import tempfile -import pytest - from ipapython import directivesetter EXAMPLE_CONFIG = [ @@ -22,17 +19,6 @@ WHITESPACE_CONFIG = [ ] -@pytest.fixture -def tempdir(request): - tempdir = tempfile.mkdtemp() - - def fin(): - shutil.rmtree(tempdir) - - request.addfinalizer(fin) - return tempdir - - class test_set_directive_lines: def test_remove_directive(self): lines = directivesetter.set_directive_lines( diff --git a/ipatests/test_ipapython/test_ipautil.py b/ipatests/test_ipapython/test_ipautil.py index 837aa729f..5ecc097a6 100644 --- a/ipatests/test_ipapython/test_ipautil.py +++ b/ipatests/test_ipapython/test_ipautil.py @@ -28,6 +28,7 @@ import pwd import socket import sys import tempfile +import textwrap import pytest import six @@ -577,3 +578,32 @@ def test_check_port_bindable_udp(udp_listen): assert not ipautil.check_port_bindable(port, socket.SOCK_DGRAM) sock.close() assert ipautil.check_port_bindable(port, socket.SOCK_DGRAM) + + +def test_config_replace_variables(tempdir): + conffile = os.path.join(tempdir, 'test.conf') + + conf = textwrap.dedent(""" + replaced=foo + removed=gone + """) + expected = textwrap.dedent(""" + replaced=bar + addreplaced=baz + """) + + with open(conffile, 'w') as f: + f.write(conf) + + result = ipautil.config_replace_variables( + conffile, + replacevars=dict(replaced="bar", addreplaced="baz"), + removevars={'removed'} + ) + assert result == { + 'removed': 'gone', 'replaced': 'foo' + } + + with open(conffile, 'r') as f: + newconf = f.read() + assert newconf == expected diff --git a/ipatests/test_ipaserver/test_install/test_installutils.py b/ipatests/test_ipaserver/test_install/test_installutils.py index 577a993f8..3d878a454 100644 --- a/ipatests/test_ipaserver/test_install/test_installutils.py +++ b/ipatests/test_ipaserver/test_install/test_installutils.py @@ -6,9 +6,7 @@ from __future__ import absolute_import import binascii import os import re -import shutil import subprocess -import tempfile import textwrap import pytest @@ -20,17 +18,6 @@ from ipaserver.install import ipa_backup from ipaserver.install import ipa_restore -@pytest.fixture -def tempdir(request): - tempdir = tempfile.mkdtemp() - - def fin(): - shutil.rmtree(tempdir) - - request.addfinalizer(fin) - return tempdir - - GPG_GENKEY = textwrap.dedent(""" %echo Generating a standard key Key-Type: RSA