"""
@file
@brief Function to use inside setup.py

.. versionadded:: 1.5
"""
import os
import sys

from .. loghelper import run_cmd


def must_build(argv=None):
    """
    Determines if the module must be built before running the command
    in *argv*.

    @param      argv        if None, default to sys.argv
    @return                 boolean

    *built* means calling ``setup.py build_ext --inplace``.
    """
    if argv is None:
        argv = sys.argv
    for k in {'unittests', 'unittests_LONG', 'unittests_SKIP', 'unittests_GUI', 'build_sphinx'}:
        if k in argv:
            return True
    return False


def run_build_ext(setup_file):
    """
    Run ``setup.py build_ext --inplace``.

    @param      setup_file      setup_file
    @return                     output
    """
    exe = sys.executable
    setup = os.path.join(os.path.abspath(
        os.path.dirname(setup_file)), "setup.py")
    cmd = "{0} {1} build_ext --inplace".format(exe, setup)
    out, err = run_cmd(cmd, wait=True, change_path=os.path.dirname(setup_file))
    err0 = _filter_out_warning(err)
    if len(err0) > 0:
        raise Exception("Unable to run\n{0}\nERR:\n{1}".format(cmd,
                                                               "\n".join("### " + _ for _ in err.split("\n"))))
    return out


def _filter_out_warning(out):
    """
    Filters out (import) warning from error

    @param      out     string
    @return             filtered string
    """
    lines = out.split("\n")
    new_lines = []
    skip = False
    for line in lines:
        if len(line) == 0:
            skip = True
        elif line[0] != " ":
            skip = "ImportWarning" in line
        if not skip:
            new_lines.append(line)
    return "\n".join(new_lines)

@@ -964,16 +964,71 @@
  setup)%0A
+    chd = os.path.abspath(os.path.dirname(setup_file))%0A
     out,
@@ -1074,35 +1074,11 @@
 ath=
-os.path.dirname(setup_file)
+chd
 )%0A  

switch to absolute path