Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python/vyos/config_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from vyos.utils.process import rc_cmd
from vyos.defaults import DEFAULT_COMMIT_CONFIRM_MINUTES
from vyos.component_version import append_system_version
from vyos.utils.file import file_compare

SAVE_CONFIG = '/usr/libexec/vyos/vyos-save-config.py'
config_json = '/run/vyatta/config/config.json'
Expand Down Expand Up @@ -97,7 +98,7 @@ def unsaved_commits(allow_missing_config=False) -> bool:
return True
tmp_save = '/tmp/config.running'
save_config(tmp_save)
ret = not cmp(tmp_save, config_file, shallow=False)
ret = not file_compare(tmp_save, config_file)
os.unlink(tmp_save)
return ret

Expand Down
26 changes: 26 additions & 0 deletions python/vyos/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,29 @@ def move_recursive(src: str, dst: str, overwrite=False):

copy_recursive(src, dst, overwrite=overwrite)
shutil.rmtree(src)


def file_compare(file1: str, file2: str) -> bool:
"""
Compare two files modulo blank lines, leading/trailing whitespace, final
newline.

Returns:
bool: True if files are equivalent in the sense above.

"""
def non_empty(line):
return bool(line.strip())

with open(file1) as f1, open(file2) as f2:
it1 = filter(non_empty, f1)
it2 = filter(non_empty, f2)

try:
for l1, l2 in zip(it1, it2, strict=True):
if l1.strip() != l2.strip():
return False
except ValueError:
return False

return True
Loading