Fix #2475 ini_file module: bracklets in key break idempotence

Escape the regex special characters in the option name.
This commit is contained in:
Ales Nosek 2015-12-02 20:31:27 -08:00
parent aad7497f18
commit 9c74272c9b

View file

@ -99,6 +99,22 @@ import ConfigParser
import sys import sys
import os import os
# ==============================================================
# match_opt
def match_opt(option, line):
option = re.escape(option)
return re.match('%s *=' % option, line) \
or re.match('# *%s *=' % option, line) \
or re.match('; *%s *=' % option, line)
# ==============================================================
# match_active_opt
def match_active_opt(option, line):
option = re.escape(option)
return re.match('%s *=' % option, line)
# ============================================================== # ==============================================================
# do_ini # do_ini
@ -141,9 +157,7 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
if within_section and option: if within_section and option:
if state == 'present': if state == 'present':
# change the existing option line # change the existing option line
if re.match('%s *=' % option, line) \ if match_opt(option, line):
or re.match('# *%s *=' % option, line) \
or re.match('; *%s *=' % option, line):
newline = '%s = %s\n' % (option, value) newline = '%s = %s\n' % (option, value)
changed = ini_lines[index] != newline changed = ini_lines[index] != newline
ini_lines[index] = newline ini_lines[index] = newline
@ -154,14 +168,14 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
line = ini_lines[index] line = ini_lines[index]
if line.startswith('['): if line.startswith('['):
break break
if re.match('%s *=' % option, line): if match_active_opt(option, line):
del ini_lines[index] del ini_lines[index]
else: else:
index = index + 1 index = index + 1
break break
else: else:
# comment out the existing option line # comment out the existing option line
if re.match('%s *=' % option, line): if match_active_opt(option, line):
ini_lines[index] = '#%s' % ini_lines[index] ini_lines[index] = '#%s' % ini_lines[index]
changed = True changed = True
break break