def cmake_low(log, build_dir, src_dir, working_dir, opts, args, env):

    ret = 0

    cmd = ["cmake"] + opts + ["--build=" + build_dir] + args + [src_dir]

    log.info("directory: {}".format(working_dir))
    log.info("src: {}".format(src_dir))
    log.info("build dir: {}".format(build_dir))
    log.info("command:")
    for i in cmd:
        log.info("    {}".format(i))
    # log.info("command(joined): {}".format(' '.join(cmd)))

    p = None
    try:
        p = subprocess.Popen(cmd, env=env, stdout=log.stdout, stderr=log.stderr, cwd=working_dir)
    except:
        log.error(
            "exception while starting cmake\n"
            "    command line was:\n"
            "    " + repr(cmd) + wayround_org.utils.error.return_exception_info(sys.exc_info())
        )
        ret = 100

    else:

        try:
            p.wait()
        except:
            log.error(
                "Exception occurred while waiting for cmake\n{}".format(
                    wayround_org.utils.error.return_exception_info(sys.exc_info())
                )
            )
            ret = 100
        else:
            tmp_s = "cmake return code was: {}".format(p.returncode)

            if p.returncode == 0:
                log.info(tmp_s)
            else:
                log.error(tmp_s)

            ret = p.returncode

    return ret
def clean_find_garbage(command_name, opts, args, adds):
    """
    Search system for garbage making log and cleaning script

    [-h=all|cpu-vend-os-triplet] [-a=all|cpu-vend-os-triplet]

    -b=BASENAME        - system root path
    --script-type=bash - system cleaning script language (only bash supported)
    --so               - look only for .so files garbage in /usr/lib directory
    """

    import wayround_org.aipsetup.controllers
    import wayround_org.aipsetup.package_name_parser
    import wayround_org.aipsetup.client_pkg
    import wayround_org.aipsetup.system

    config = adds['config']

    ret = 0

    if wayround_org.utils.getopt.check_options(
            opts,
            opts_list=[
                '-b=',
                '--script-type=',
                '--so',
                '-h=',
                '-a='
                ]
            ) != 0:
        ret = 1

    if ret == 0:

        timestamp = wayround_org.utils.time.currenttime_stamp()

        basedir = '/'
        script = 'system_garbage_remove_{}.sh'.format(timestamp)
        script_type = 'bash'
        only_lib = False
        down_script = 'get_required_sources_{}.sh'.format(timestamp)

        if '-b' in opts:
            basedir = opts['-b']

        if '--script-type' in opts:
            script_type = opts['--script-type']

        only_lib = '--so' in opts

        host, arch = _process_h_and_a_opts_specific(opts, config)

        log = wayround_org.utils.log.Log(
            os.getcwd(), 'system_garbage', timestamp=timestamp
            )

        if not script_type in ['bash']:
            logging.error("Invalid --script-type value")
            ret = 1
        else:

            pkg_client = \
                wayround_org.aipsetup.controllers.pkg_client_by_config(
                    config
                    )

            system = wayround_org.aipsetup.controllers.sys_ctl_by_config(
                config,
                pkg_client,
                basedir=basedir
                )

            log.info("Searching for garbage")
            res = system.find_system_garbage(
                mute=False,
                only_lib=only_lib,
                host=host
                )

            if not isinstance(res, list):
                log.error("Some error while searching for garbage")
                ret = 1
            else:

                log.info("Garbage search complete")
                log.info(
                    "Separating garbage .so files to know "
                    "which packages depending on them"
                    )

                libs = wayround_org.utils.path.exclude_files_not_in_dirs(
                    res,
                    system.library_paths(host=host),
                    )

                libs = wayround_org.aipsetup.system.filter_so_files(
                    libs,
                    verbose=True
                    )

                if only_lib:
                    res = libs

                libs = wayround_org.utils.path.bases(libs)

                asp_deps = system.load_asp_deps_all(mute=False)

                asps_lkd_to_garbage = {}

                log.info("Calculating garbage dependencies")

                for asp_name in list(asp_deps.keys()):

                    if not asp_name in asps_lkd_to_garbage:
                        asps_lkd_to_garbage[asp_name] = dict()

                    for file_name in list(asp_deps[asp_name].keys()):

                        file_name_with_dest_dir = \
                            wayround_org.utils.path.insert_base(
                                file_name, basedir
                                )

                        if (not file_name_with_dest_dir
                                in asps_lkd_to_garbage[asp_name]):
                            asps_lkd_to_garbage[
                                asp_name
                                ][file_name_with_dest_dir] = set()

                        asps_lkd_to_garbage[
                            asp_name
                            ][file_name_with_dest_dir] |= \
                            (set(libs) & set(asp_deps[asp_name][file_name]))

                        if len(
                                asps_lkd_to_garbage[
                                    asp_name][file_name_with_dest_dir]
                                ) == 0:
                            del asps_lkd_to_garbage[
                                asp_name][file_name_with_dest_dir]

                    if len(asps_lkd_to_garbage[asp_name]) == 0:
                        del asps_lkd_to_garbage[asp_name]

                s = open(script, 'w')

                s.write("""\
#!/bin/bash

# This is fuse to ensure You are know what You are doing
exit 1


""")

                log.info("Writing report and cleaning script")

                res.sort()

                for i in res:
                    try:
                        log.info("    {}".format(i), echo=False)
                    except:
                        log.error("Error logging {}".format(repr(i)))

                    try:
                        s.write("rm {}\n".format(shlex.quote(i)))
                    except:
                        log.error("Error writing {}".format(repr(i)))

                log.info(
                    "Packages linked to garbage libraries:\n{}".format(
                        pprint.pformat(asps_lkd_to_garbage)
                        ),
                    echo=False
                    )

                log.info("Generating download script")
                required_packages = set()

                for i in list(asps_lkd_to_garbage.keys()):
                    p = wayround_org.aipsetup.package_name_parser.\
                        package_name_parse(i)

                    if not p:
                        log.error(
                            "Can't parse ASP name `{}' to add it to download script".format(
                                i)
                            )
                    else:
                        required_packages.add(p['groups']['name'])

                log.info("Writing download script")
                ds = open(down_script, 'w')
                ds.write(
                    """\
#!/bin/bash

aipsetup3 src getl {}
""".format(' '.join(required_packages))
                    )

                ds.close()

                s.close()

                logging.warning("""
Do not run cleaning script at once!
Check everything is correct!
Wrong cleaning can ruin your system
"""
                                )

            log.close()

    return ret
    def builder_action_autogen(self, called_as, log):

        cfg_script_name = self.builder_action_configure_define_script_name(
            called_as,
            log
            )

        ret = 0

        do_work = False

        if os.path.isfile(
                wayround_org.utils.path.join(
                    self.get_src_dir(),
                    self.source_configure_reldir,
                    cfg_script_name
                    )
                ):

            log.info("configurer found. no generator use presumed")
        else:
            log.info("configurer not found. generator use presumed")
            do_work = True

        if self.forced_autogen:
            log.info(
                "generator use is forced".format(
                    cfg_script_name
                )
            )
            do_work = True

        if do_work:

            log.info(
                "trying to find and use generator mesures"
                )

            for i in [
                    ('autogen.sh', ['./autogen.sh']),
                    ('bootstrap.sh', ['./bootstrap.sh']),
                    ('bootstrap', ['./bootstrap']),
                    ('genconfig.sh', ['./genconfig.sh']),
                    ('configure.ac', ['autoreconf', '-i']),
                    ('configure.in', ['autoreconf', '-i']),
                    ]:

                if os.path.isfile(
                        wayround_org.utils.path.join(
                            self.get_src_dir(),
                            self.source_configure_reldir,
                            i[0]
                            )
                        ):

                    log.info(
                        "found `{}'. trying to execute: {}".format(
                            i[0],
                            ' '.join(i[1])
                            )
                        )

                    wd = wayround_org.utils.path.join(
                        self.get_src_dir(),
                        self.source_configure_reldir
                        )
                    if '/' in i[1][0]:
                        tgt_file = wayround_org.utils.path.join(wd, i[1][0])
                        log.info("changing mode (+x) for: {}".format(tgt_file))
                        chmod_p = subprocess.Popen(
                            ['chmod', '+x', tgt_file],
                            cwd=wd
                            )
                        chmod_p.wait()

                    if i[1][0].endswith('.sh'):
                        i[1].insert(0, 'bash')

                    p = subprocess.Popen(
                        i[1],
                        cwd=wd,
                        stdout=log.stdout,
                        stderr=log.stderr
                        )
                    ret = p.wait()
                    break
            else:
                log.error(
                    "./{} not found and no generators found".format(
                        cfg_script_name
                        )
                    )
                ret = 2
        return ret
def waf(
        cwd,
        options,
        arguments,
        environment,
        environment_mode,
        log
        ):

    ret = 0

    cwd = wayround_org.utils.path.abspath(cwd)

    env = wayround_org.utils.osutils.env_vars_edit(
        environment,
        environment_mode
        )

    if len(environment) > 0:
        log.info(
            "Environment modifications: {}".format(
                repr(environment)
                )
            )

    if 'PYTHON' in environment:
        python = environment['PYTHON']
    else:
        if 'PATH' in environment:
            PATH = environment['PATH']
        else:
            PATH = os.environ['PATH']

        PATH = PATH.split(':')

        python = wayround_org.utils.file.which('python2', PATH)

        del PATH

    cmd = [
        python,
        wayround_org.utils.path.join(cwd, 'waf')
        ] + options + arguments

    log.info("directory: {}".format(cwd))
    log.info("command: {}".format(cmd))
    log.info("command(joined): {}".format(' '.join(cmd)))

    p = None
    try:
        p = subprocess.Popen(
            args=cmd,
            cwd=cwd,
            stdout=log.stdout,
            stderr=log.stderr,
            env=env
            )
    except:
        log.error(
            "exception while starting waf script\n"
            "    command line was:\n"
            "    " + repr(cmd) +
            wayround_org.utils.error.return_exception_info(
                sys.exc_info()
                )
            )
        ret = 100
    else:

        try:
            p.wait()
        except:
            log.error(
                "Exception occurred while waiting for configure\n" +
                wayround_org.utils.error.return_exception_info(
                    sys.exc_info()
                    )
                )
            ret = 100
        else:
            tmp_s = "configure return code was: {}".format(p.returncode)

            if p.returncode == 0:
                log.info(tmp_s)
            else:
                log.error(tmp_s)

            ret = p.returncode

    return ret