Module: check_mk
Branch: master
Commit: 0be9b5adb86e752a581d4b1e0ec0300fd944ffc5
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0be9b5adb86e75…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Sep 30 01:35:13 2016 +0200
Commented out experimental file io using python context manager
---
bin/mkbackup | 4 +-
lib/store.py | 164 +++++++++++++++++++++++++++++------------------------------
2 files changed, 84 insertions(+), 84 deletions(-)
diff --git a/bin/mkbackup b/bin/mkbackup
index 3c02f6c..ed38f8b 100755
--- a/bin/mkbackup
+++ b/bin/mkbackup
@@ -1345,8 +1345,8 @@ def save_state(new_attrs, update=True):
state = {}
state.update(new_attrs)
- with store.open(state_path(), "w") as f:
- json.dump(state, f, sort_keys=True, indent=4, separators=(',', ':
'))
+ store.save_file(state_path(),
+ json.dumps(state, sort_keys=True, indent=4, separators=(',', ':
')))
def state_path():
diff --git a/lib/store.py b/lib/store.py
index 98a76b8..e5d3d29 100644
--- a/lib/store.py
+++ b/lib/store.py
@@ -38,8 +38,6 @@ import pprint
from .exceptions import MKGeneralException
-# TODO: Please note that this is still experimental.
-
# TODO: Clean this up one day by using the way recommended by gettext.
# (See
https://docs.python.org/2/library/gettext.html). For this we
# need the path to the locale files here.
@@ -48,86 +46,6 @@ try:
except NameError:
_ = lambda x: x # Fake i18n when not available
-
-# Simple class to offer locked file access via flock for cross process locking
-class LockedOpen(object):
- def __init__(self, path, *args, **kwargs):
- self._path = path
- self._open_args = args
- self._open_kwargs = kwargs
- self._file_obj = None
-
-
- def __enter__(self):
- # If not existant, create the file that the open can not fail in
- # read mode and the lock is possible
- if not os.path.exists(self._path):
- file(self._path, "a+")
-
- f = file(self._path, *self._open_args, **self._open_kwargs)
-
- # Handle the case where the file has been renamed while waiting
- while True:
- fcntl.flock(f.fileno(), fcntl.LOCK_EX)
- fnew = file(self._path, *self._open_args, **self._open_kwargs)
- if os.path.sameopenfile(f.fileno(), fnew.fileno()):
- fnew.close()
- break
- else:
- f.close()
- f = fnew
-
- self._file_obj = f
- self._file_obj.__enter__()
- return self
-
-
- def __exit__(self, _exc_type, _exc_value, _traceback):
- result = self._file_obj.__exit__(_exc_type, _exc_value, _traceback)
- return result
-
-
- def __getattr__(self, name):
- return getattr(self._file_obj, name)
-
-
-# This class offers locked file opening. Read operations are made on the
-# locked file. All write operation go to a temporary file which replaces
-# the locked file while closing the object.
-class LockedOpenWithTempfile(LockedOpen):
- def __init__(self, name, mode):
- super(LockedOpenWithTempfile, self).__init__(name, "r")
- self._new_file_obj = None
- self._new_file_mode = mode
-
-
- def __enter__(self):
- super(LockedOpenWithTempfile, self).__enter__()
- self._new_file_obj = tempfile.NamedTemporaryFile(self._new_file_mode,
- dir=os.path.dirname(self._path),
- prefix=os.path.basename(self._path)+"_tmp",
- delete=False)
- self._new_file_obj.__enter__()
- return self
-
-
- def write(self, txt):
- self._new_file_obj.write(txt)
-
-
- def writelines(self, seq):
- self._new_file_obj.writelines(seq)
-
-
- def __exit__(self, _exc_type, _exc_value, _traceback):
- self._new_file_obj.__exit__(_exc_type, _exc_value, _traceback)
- os.rename(self._new_file_obj.name, self._path)
- return super(LockedOpenWithTempfile, self).__exit__(_exc_type, _exc_value,
_traceback)
-
-
-open = LockedOpenWithTempfile
-
-
#.
# .--.mk Configs---------------------------------------------------------.
# | _ ____ __ _ |
@@ -328,3 +246,85 @@ def release_all_locks():
g_aquired_locks = []
g_locked_paths = []
+
+
+# Experimental but not used yet.
+## Simple class to offer locked file access via flock for cross process locking
+#class LockedOpen(object):
+# def __init__(self, path, *args, **kwargs):
+# self._path = path
+# self._open_args = args
+# self._open_kwargs = kwargs
+# self._file_obj = None
+#
+#
+# def __enter__(self):
+# # If not existant, create the file that the open can not fail in
+# # read mode and the lock is possible
+# if not os.path.exists(self._path):
+# file(self._path, "a+")
+#
+# f = file(self._path, *self._open_args, **self._open_kwargs)
+#
+# # Handle the case where the file has been renamed while waiting
+# while True:
+# fcntl.flock(f.fileno(), fcntl.LOCK_EX)
+# fnew = file(self._path, *self._open_args, **self._open_kwargs)
+# if os.path.sameopenfile(f.fileno(), fnew.fileno()):
+# fnew.close()
+# break
+# else:
+# f.close()
+# f = fnew
+#
+# self._file_obj = f
+# self._file_obj.__enter__()
+# return self
+#
+#
+# def __exit__(self, _exc_type, _exc_value, _traceback):
+# result = self._file_obj.__exit__(_exc_type, _exc_value, _traceback)
+# return result
+#
+#
+# def __getattr__(self, name):
+# return getattr(self._file_obj, name)
+#
+#
+## This class offers locked file opening. Read operations are made on the
+## locked file. All write operation go to a temporary file which replaces
+## the locked file while closing the object.
+#class LockedOpenWithTempfile(LockedOpen):
+# def __init__(self, name, mode):
+# super(LockedOpenWithTempfile, self).__init__(name, "r")
+# self._new_file_obj = None
+# self._new_file_mode = mode
+#
+#
+# def __enter__(self):
+# super(LockedOpenWithTempfile, self).__enter__()
+# self._new_file_obj = tempfile.NamedTemporaryFile(self._new_file_mode,
+# dir=os.path.dirname(self._path),
+# prefix=os.path.basename(self._path)+"_tmp",
+# delete=False)
+# self._new_file_obj.__enter__()
+# return self
+#
+#
+# def write(self, txt):
+# self._new_file_obj.write(txt)
+#
+#
+# def writelines(self, seq):
+# self._new_file_obj.writelines(seq)
+#
+#
+# def __exit__(self, _exc_type, _exc_value, _traceback):
+# self._new_file_obj.__exit__(_exc_type, _exc_value, _traceback)
+# os.rename(self._new_file_obj.name, self._path)
+# return super(LockedOpenWithTempfile, self).__exit__(_exc_type, _exc_value,
_traceback)
+#
+#
+#open = LockedOpenWithTempfile
+
+