
from __future__ import absolute_import

from vdsm.common import commands
from vdsm.common.cmdutils import CommandPath

_VIRTSPARSIFY = CommandPath("virt-sparsify",
                            "/usr/bin/virt-sparsify",)


def sparsify(src_vol, tmp_vol, dst_vol, src_format=None, dst_format=None):
    cmd = [_VIRTSPARSIFY.cmd, '--tmp', 'prebuilt:' + tmp_vol]

    if src_format:
        cmd.extend(("--format", src_format))

    if dst_format:
        cmd.extend(("--convert", dst_format))

    cmd.extend((src_vol, dst_vol))

    commands.run(cmd)


def sparsify_inplace(vol_path):
    cmd = [_VIRTSPARSIFY.cmd, '--machine-readable', '--in-place', vol_path]

    commands.run(cmd)
