mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
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 <cheimes@redhat.com> Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
This commit is contained in:
parent
c819716521
commit
3cb1ccb3b0
0
install/share/ds-ipa-env.conf.template
Normal file
0
install/share/ds-ipa-env.conf.template
Normal file
@ -1104,14 +1104,17 @@ def reverse_record_exists(ip_address):
|
|||||||
return True
|
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
|
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
|
All (key,value) pairs from replacevars and appendvars that were not found
|
||||||
in the configuration file, will be added there.
|
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
|
It is responsibility of a caller to ensure that replacevars and
|
||||||
appendvars do not overlap.
|
appendvars do not overlap.
|
||||||
|
|
||||||
@ -1153,7 +1156,11 @@ $)''', re.VERBOSE)
|
|||||||
elif value.find(appendvars[option]) == -1:
|
elif value.find(appendvars[option]) == -1:
|
||||||
new_line = u"%s=%s %s\n" % (option, value, appendvars[option])
|
new_line = u"%s=%s %s\n" % (option, value, appendvars[option])
|
||||||
old_values[option] = value
|
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
|
# Now add all options from replacevars and appendvars that were not found in the file
|
||||||
new_vars = replacevars.copy()
|
new_vars = replacevars.copy()
|
||||||
new_vars.update(appendvars)
|
new_vars.update(appendvars)
|
||||||
|
@ -5,7 +5,9 @@ from __future__ import print_function
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import pprint
|
import pprint
|
||||||
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -145,3 +147,14 @@ def pytest_runtest_setup(item):
|
|||||||
# pylint: disable=no-member
|
# pylint: disable=no-member
|
||||||
if pytest.config.option.skip_ipaapi:
|
if pytest.config.option.skip_ipaapi:
|
||||||
pytest.skip("Skip tests that needs an IPA API")
|
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
|
||||||
|
@ -4,11 +4,8 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from ipapython import directivesetter
|
from ipapython import directivesetter
|
||||||
|
|
||||||
EXAMPLE_CONFIG = [
|
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:
|
class test_set_directive_lines:
|
||||||
def test_remove_directive(self):
|
def test_remove_directive(self):
|
||||||
lines = directivesetter.set_directive_lines(
|
lines = directivesetter.set_directive_lines(
|
||||||
|
@ -28,6 +28,7 @@ import pwd
|
|||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import textwrap
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import six
|
import six
|
||||||
@ -577,3 +578,32 @@ def test_check_port_bindable_udp(udp_listen):
|
|||||||
assert not ipautil.check_port_bindable(port, socket.SOCK_DGRAM)
|
assert not ipautil.check_port_bindable(port, socket.SOCK_DGRAM)
|
||||||
sock.close()
|
sock.close()
|
||||||
assert ipautil.check_port_bindable(port, socket.SOCK_DGRAM)
|
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
|
||||||
|
@ -6,9 +6,7 @@ from __future__ import absolute_import
|
|||||||
import binascii
|
import binascii
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import shutil
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -20,17 +18,6 @@ from ipaserver.install import ipa_backup
|
|||||||
from ipaserver.install import ipa_restore
|
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("""
|
GPG_GENKEY = textwrap.dedent("""
|
||||||
%echo Generating a standard key
|
%echo Generating a standard key
|
||||||
Key-Type: RSA
|
Key-Type: RSA
|
||||||
|
Loading…
Reference in New Issue
Block a user