diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-03-06 09:41:31 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-04-06 20:59:07 +0200 |
commit | f1a96163c244905e6190ca842eb84438290b0741 (patch) | |
tree | c5d0a69092fce402a889d7e7657a91fcfecc2e9b | |
parent | b5ab3d6d25c024fc120746514edfff33c55074d2 (diff) | |
download | tmwa-replace-shell-snippet.tar.gz tmwa-replace-shell-snippet.tar.bz2 tmwa-replace-shell-snippet.tar.xz tmwa-replace-shell-snippet.zip |
tools/protocol.py: Replaced shell snippet with Python codereplace-shell-snippet
Should be more portable and performant.
-rwxr-xr-x | tools/protocol.py | 27 |
1 files 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_ |