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

Vratislav Podzimek vpodzime at redhat.com
Wed May 7 07:22:41 UTC 2014


On Tue, 2014-05-06 at 14:47 -0500, David Lehman wrote:
> ---
>  blivet/util.py     | 43 +++++++++++++++++++++++++++++++++++++++++++
>  examples/common.py | 25 -------------------------
>  2 files changed, 43 insertions(+), 25 deletions(-)
> 
> diff --git a/blivet/util.py b/blivet/util.py
> index d90b9e9..f37a428 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,44 @@ 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(log_file='/tmp/blivet.log'):
> +    """ Configure the blivet logger to write out a log file.
> +
> +        :keyword str log_file: path to the log file (default: /tmp/blivet.log)
> +    """
> +    log.setLevel(logging.DEBUG)
> +    program_log.setLevel(logging.DEBUG)
> +    handler = logging.FileHandler(log_file)
> +    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
ACK. Thanks.

-- 
Vratislav Podzimek

Anaconda Rider | Red Hat, Inc. | Brno - Czech Republic



More information about the anaconda-patches mailing list