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:
+10
-3
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user