Module: check_mk
Branch: master
Commit: ebe678b265aefec212200671dcb46fdfec265163
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ebe678b265aefeā¦
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Thu Jul 28 13:25:18 2016 +0200
New lib function save_file() without MK header
---
lib/store.py | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/lib/store.py b/lib/store.py
index 1885d36..dea4bce 100644
--- a/lib/store.py
+++ b/lib/store.py
@@ -168,20 +168,25 @@ def load_from_mk_file(path, key, default, **kwargs):
return load_mk_file(path, {key: default}, **kwargs)[key]
+def save_mk_file(path, mk_content):
+ content = "# Written by Check_MK store (%s)\n\n" % \
+ time.strftime("%Y-%m-%d %H:%M:%S")
+ content += mk_content
+ content += "\n"
+ save_file(path, content)
+
+
# Saving assumes a locked destination file (usually done by loading code)
# Then the new file is written to a temporary file and moved to the target path
-def save_mk_file(path, formated):
+def save_file(path, content, mode=0660):
try:
tmp_path = None
with tempfile.NamedTemporaryFile("w", dir=os.path.dirname(path),
prefix=os.path.basename(path)+".new",
delete=False) as tmp:
tmp_path = tmp.name
-
- os.chmod(tmp_path, 0660)
- tmp.write("# Written by Check_MK store (%s)\n\n" %
time.strftime("%Y-%m-%d %H:%M:%S"))
- tmp.write(formated)
- tmp.write("\n")
+ os.chmod(tmp_path, mode)
+ tmp.write(content)
os.rename(tmp_path, path)