From f1a96163c244905e6190ca842eb84438290b0741 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Thu, 6 Mar 2025 09:41:31 +0100 Subject: tools/protocol.py: Replaced shell snippet with Python code Should be more portable and performant. --- tools/protocol.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/tools/protocol.py b/tools/protocol.py index d278884..e2e3f46 100755 --- a/tools/protocol.py +++ b/tools/protocol.py @@ -22,7 +22,7 @@ import glob import os -from shlex import quote +import filecmp from posixpath import relpath from weakref import ref as wr @@ -90,20 +90,19 @@ class OpenWrite(object): self.handle.close() if ty is not None: return - frag = ''' - if cmp -s {0}.tmp {0}.old - then - : echo Unchanged: {0} - rm {0}.tmp - mv {0}.old {0} - else - echo Changed: {0} - rm {0}.old - mv {0}.tmp {0} - fi - '''.format(quote(self.filename)) - os.system(frag) + tmp_file = self.filename + '.tmp' + old_file = self.filename + '.old' + + if os.path.exists(old_file) and filecmp.cmp(tmp_file, old_file, shallow=False): + # Unchanged + os.remove(tmp_file) + os.rename(old_file, self.filename) + else: + print(f"Changed: {self.filename}") + if os.path.exists(old_file): + os.remove(old_file) + os.rename(tmp_file, self.filename) # TOC_ -- cgit v1.2.3-70-g09d2