diff options
| author | John Estabrook <jestabro@vyos.io> | 2025-11-21 14:32:52 -0600 |
|---|---|---|
| committer | John Estabrook <jestabro@vyos.io> | 2025-11-23 16:52:53 -0600 |
| commit | b80bba65567c981bf7aba014edcab632c172867b (patch) | |
| tree | 54bb765a997b81f3720079a81b11c07818cde9e5 /python | |
| parent | cae2717e9a608a1706e9dc134f7d560453ec4bfe (diff) | |
| download | vyos-1x-b80bba65567c981bf7aba014edcab632c172867b.tar.gz vyos-1x-b80bba65567c981bf7aba014edcab632c172867b.zip | |
T8031: add file comparison utility for comparison modulo empty lines
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/utils/file.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/python/vyos/utils/file.py b/python/vyos/utils/file.py index 98d88d9e4..6708ade2d 100644 --- a/python/vyos/utils/file.py +++ b/python/vyos/utils/file.py @@ -312,3 +312,30 @@ 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 |
