Module: check_mk
Branch: master
Commit: d6c5b088b13b86f26ee6868f0cf3c4fb3a0152a8
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=d6c5b088b13b86…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Oct 14 14:41:10 2016 +0200
Replaced own in memory pseudo file object with standard StringIO
---
cmk_base/packaging.py | 7 ++++---
modules/automation.py | 4 ++--
modules/check_mk.py | 44 +++-----------------------------------------
3 files changed, 9 insertions(+), 46 deletions(-)
diff --git a/cmk_base/packaging.py b/cmk_base/packaging.py
index 2a357d3..98d476e 100644
--- a/cmk_base/packaging.py
+++ b/cmk_base/packaging.py
@@ -30,6 +30,7 @@ import pprint
import sys
import tarfile
import time
+from cStringIO import StringIO
import cmk.tty as tty
import cmk.paths
@@ -328,8 +329,8 @@ def create_mkp_file(package, file_name=None, file_object=None):
return info
tar = tarfile.open(name=file_name, fileobj=file_object, mode="w:gz")
- info_file = fake_file(pprint.pformat(package))
- info = create_info("info", info_file.size())
+ info_file = StringIO(pprint.pformat(package))
+ info = create_info("info", len(info_file.getvalue()))
tar.addfile(info, info_file)
# Now pack the actual files into sub tars
@@ -342,7 +343,7 @@ def create_mkp_file(package, file_name=None, file_object=None):
subtarname = part + ".tar"
subdata = os.popen("tar cf - --dereference --force-local -C '%s'
%s" % (dir, " ".join(filenames))).read()
info = create_info(subtarname, len(subdata))
- tar.addfile(info, fake_file(subdata))
+ tar.addfile(info, StringIO(subdata))
tar.close()
diff --git a/modules/automation.py b/modules/automation.py
index d19908e..5705e95 100644
--- a/modules/automation.py
+++ b/modules/automation.py
@@ -1137,9 +1137,9 @@ def automation_get_package(args):
if not package:
raise MKAutomationError("Package not installed or corrupt")
- output_file = fake_file()
+ output_file = StringIO()
cmk_base.packaging.create_mkp_file(package, file_object=output_file)
- return package, output_file.content()
+ return package, output_file.getvalue()
def automation_create_or_edit_package(args, mode):
diff --git a/modules/check_mk.py b/modules/check_mk.py
index d6a2f71..9c7eccc 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -45,6 +45,8 @@ import fcntl
import py_compile
import inspect
+from cStringIO import StringIO
+
from cmk.regex import regex, is_regex
from cmk.exceptions import MKGeneralException, MKTerminate, MKBailOut
import cmk.debug
@@ -2660,46 +2662,6 @@ def pack_autochecks():
# | implemented here. |
# '----------------------------------------------------------------------'
-class fake_file:
- def __init__(self, content=""):
- self._content = content
- self._pointer = 0
-
- def size(self):
- return len(self._content)
-
- def read(self, size):
- size = min(size, len(self._content) - self._pointer)
- new_end = self._pointer + size
- data = self._content[self._pointer:new_end]
- self._pointer = new_end
- return data
-
- def write(self, data):
- self._content += data
-
- def content(self):
- return self._content
-
- def tell(self):
- return self._pointer
-
- def seek(self, offset, whence=0):
- if whence == 0:
- new_pointer = offset
- elif whence == 1:
- new_pointer = self._pointer + offset
- elif whence == 2:
- new_pointer = self.size() - offset
- else:
- raise IOError("Invalid value for whence")
-
- if new_pointer < 0:
- raise IOError("Invalid seek")
-
- self._pointer = new_pointer
-
-
def do_backup(tarname):
import tarfile
logger.verbose("Creating backup file '%s'...", tarname)
@@ -2731,7 +2693,7 @@ def do_backup(tarname):
info.type = tarfile.REGTYPE
info.name = subtarname
logger.verbose(" Added %s (%s) with a size of %s", descr, absdir,
render.bytes(info.size))
- tar.addfile(info, fake_file(subdata))
+ tar.addfile(info, StringIO(subdata))
tar.close()
logger.verbose("Successfully created backup.")