Fixes #19883: Handle INI files without ending newline (#20016)

pull/15526/head
peay 8 years ago committed by Brian Coca
parent d1ebd8f411
commit 222e1c97be

@ -34,6 +34,8 @@ description:
to manage the file as a whole with, say, M(template) or M(assemble). Adds missing
sections if they don't exist.
- Before version 2.0, comments are discarded when the source file is read, and therefore will not show up in the destination file.
- Since version 2.3, this module adds missing ending newlines to files to keep in line with the POSIX standard, even when
no modifications need to be applied.
version_added: "0.9"
options:
path:
@ -172,12 +174,18 @@ def do_ini(module, filename, section=None, option=None, value=None,
if module._diff:
diff['before'] = ''.join(ini_lines)
changed = False
# last line of file may not contain a trailing newline
if ini_lines[-1] == "" or ini_lines[-1][-1] != '\n':
ini_lines[-1] += '\n'
changed = True
# append a fake section line to simplify the logic
ini_lines.append('[')
within_section = not section
section_start = 0
changed = False
msg = 'OK'
if no_extra_spaces:
assignment_format = '%s=%s\n'
@ -211,11 +219,12 @@ def do_ini(module, filename, section=None, option=None, value=None,
# change the existing option line
if match_opt(option, line):
newline = assignment_format % (option, value)
changed = ini_lines[index] != newline
if changed:
option_changed = ini_lines[index] != newline
changed = changed or option_changed
if option_changed:
msg = 'option changed'
ini_lines[index] = newline
if changed:
if option_changed:
# remove all possible option occurrences from the rest of the section
index = index + 1
while index < len(ini_lines):

Loading…
Cancel
Save