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:
Christian Heimes
2019-04-02 19:35:38 +02:00
parent c819716521
commit 3cb1ccb3b0
6 changed files with 53 additions and 30 deletions
+10 -3
View File
@@ -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)