Fix interfaces_file for proper file contents (#37818)

The generated file was completely unusable by the system
therefore the fix which ensures that diffing the file
prior to changes and after only shows diffs

Furthermore the code did not work for Python 3.6
>       f.writelines(to_bytes(lines, errors='surrogate_or_strict'))
E       TypeError: a bytes-like object is required, not 'int'

The other modifications (lambda variable renaming) is to
comply with default flake8 rules
This commit is contained in:
Olivier Bourdon 2018-03-28 11:21:43 +02:00 committed by John R Barker
commit 612d0d6634
8 changed files with 248 additions and 6 deletions

View file

@ -290,11 +290,11 @@ def setInterfaceOption(module, lines, iface, option, raw_value, state):
if option in ["pre-up", "up", "down", "post-up"] and value is not None and value != "None":
for target_option in filter(lambda i: i['value'] == value, target_options):
changed = True
lines = list(filter(lambda l: l != target_option, lines))
lines = list(filter(lambda ln: ln != target_option, lines))
else:
changed = True
for target_option in target_options:
lines = list(filter(lambda l: l != target_option, lines))
lines = list(filter(lambda ln: ln != target_option, lines))
else:
module.fail_json(msg="Error: unsupported state %s, has to be either present or absent" % state)
@ -322,7 +322,7 @@ def write_changes(module, lines, dest):
tmpfd, tmpfile = tempfile.mkstemp()
f = os.fdopen(tmpfd, 'wb')
f.writelines(to_bytes(lines, errors='surrogate_or_strict'))
f.write(to_bytes(''.join(lines), errors='surrogate_or_strict'))
f.close()
module.atomic_move(tmpfile, os.path.realpath(dest))