From 47f4de938f7a75951b0a071c1c8115e65b33de08 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Wed, 8 Apr 2020 08:50:54 +0200 Subject: [PATCH] jwutils.misc: Add commit_tmpfile() commit_tmpfile() is a better os.rename which doesn't do anything if source and target files are equal, and logs if does something. Signed-off-by: Jan Lindemann --- tools/python/jwutils/misc.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/python/jwutils/misc.py b/tools/python/jwutils/misc.py index cf6d19f..a3ef204 100644 --- a/tools/python/jwutils/misc.py +++ b/tools/python/jwutils/misc.py @@ -62,4 +62,13 @@ def get_derived_classes(mod, base): # export r.append(c) return r +def commit_tmpfile(tmp, path): # export + caller = log.get_caller_pos() + if os.path.isfile(path) and filecmp.cmp(tmp, path): + log.slog(log.INFO, "{} is up to date".format(path), caller=caller) + os.unlink(tmp) + else: + log.slog(log.NOTICE, "saving {}".format(path), caller=caller) + os.rename(path + '.tmp', path) + atexit.register(_cleanup)