[PATCH 2/4] Move set_up_logging and create_sparse_file into blivet.util for reuse.

David Lehman dlehman at redhat.com
Mon May 5 18:10:35 UTC 2014


---
 blivet/util.py     | 40 ++++++++++++++++++++++++++++++++++++++++
 examples/common.py | 25 -------------------------
 2 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/blivet/util.py b/blivet/util.py
index d90b9e9..7ca91c5 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -4,6 +4,8 @@ import shutil
 import selinux
 import subprocess
 import re
+import sys
+import tempfile
 from decimal import Decimal
 
 from .size import Size
@@ -353,3 +355,41 @@ class ObjectID(object):
         self = super(ObjectID, cls).__new__(cls, *args, **kwargs)
         self.id = self._newid_gen() # pylint: disable=attribute-defined-outside-init
         return self
+
+##
+## Convenience functions for examples and tests
+##
+def set_up_logging():
+    """ Configure the blivet logger to use /tmp/blivet.log as its log file. """
+    log.setLevel(logging.DEBUG)
+    program_log.setLevel(logging.DEBUG)
+    handler = logging.FileHandler("/tmp/blivet.log")
+    handler.setLevel(logging.DEBUG)
+    formatter = logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
+    handler.setFormatter(formatter)
+    log.addHandler(handler)
+    program_log.addHandler(handler)
+    log.info("sys.argv = %s", sys.argv)
+
+def create_sparse_tempfile(name, size):
+    """ Create a temporary sparse file.
+
+        :param str name: suffix for filename
+        :param :class:`~.size.Size` size: the file size
+        :returns: the path to the newly created file
+    """
+    (fd, path) = tempfile.mkstemp(prefix="blivet.", suffix="-%s" % name)
+    os.close(fd)
+    create_sparse_file(path, size)
+    return path
+
+def create_sparse_file(path, size):
+    """ Create a sparse file.
+
+        :param str path: the full path to the file
+        :param :class:`~.size.Size` size: the size of the file
+        :returns: None
+    """
+    fd = os.open(path, os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
+    os.ftruncate(fd, size)
+    os.close(fd)
diff --git a/examples/common.py b/examples/common.py
index bb593bc..90c735f 100644
--- a/examples/common.py
+++ b/examples/common.py
@@ -1,31 +1,6 @@
-import logging
-
-def set_up_logging():
-    """ Configure the blivet logger to use /tmp/blivet.log as its log file. """
-    blivet_log = logging.getLogger("blivet")
-    blivet_log.setLevel(logging.DEBUG)
-    program_log = logging.getLogger("program")
-    program_log.setLevel(logging.DEBUG)
-    handler = logging.FileHandler("/tmp/blivet.log")
-    handler.setLevel(logging.DEBUG)
-    formatter = logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
-    handler.setFormatter(formatter)
-    blivet_log.addHandler(handler)
-    program_log.addHandler(handler)
 
 def print_devices(b):
     for device in sorted(b.devices, key=lambda d: len(d.ancestors)):
         print device    # this is a blivet.devices.StorageDevice instance
 
     print
-
-def create_sparse_file(b, name, size):
-    """ Create a sparse file for use as a disk image. """
-    import blivet
-    import tempfile
-
-    (_fd, path) = tempfile.mkstemp(prefix="blivet.", suffix="-image-%s" % name)
-
-    file_device = blivet.devices.SparseFileDevice(path, size=size)
-    file_device.create()
-    return path
-- 
1.9.0



More information about the anaconda-patches mailing list