Example #1
0
def environment_wrap_command(env_filenames,
                             env_folder,
                             cmd,
                             subsystem=None,
                             accepted_extensions=None):
    if not env_filenames:
        return cmd
    filenames = [env_filenames
                 ] if not isinstance(env_filenames, list) else env_filenames
    bats, shs, ps1s = [], [], []

    accept = accepted_extensions or ("ps1", "bat", "sh")
    # TODO: This implemantation is dirty, improve it
    for f in filenames:
        f = f if os.path.isabs(f) else os.path.join(env_folder, f)
        if f.lower().endswith(".sh"):
            if os.path.isfile(f) and "sh" in accept:
                f = subsystem_path(subsystem, f)
                shs.append(f)
        elif f.lower().endswith(".bat"):
            if os.path.isfile(f) and "bat" in accept:
                bats.append(f)
        elif f.lower().endswith(".ps1") and "ps1" in accept:
            if os.path.isfile(f):
                ps1s.append(f)
        else:  # Simple name like "conanrunenv"
            path_bat = "{}.bat".format(f)
            path_sh = "{}.sh".format(f)
            path_ps1 = "{}.ps1".format(f)
            if os.path.isfile(path_bat) and "bat" in accept:
                bats.append(path_bat)
            if os.path.isfile(path_ps1) and "ps1" in accept:
                ps1s.append(path_ps1)
            if os.path.isfile(path_sh) and "sh" in accept:
                path_sh = subsystem_path(subsystem, path_sh)
                shs.append(path_sh)

    if bool(bats) + bool(shs) + bool(ps1s) > 1:
        raise ConanException("Cannot wrap command with different envs,"
                             " {} - {} - {}".format(bats, shs, ps1s))

    if bats:
        launchers = " && ".join('"{}"'.format(b) for b in bats)
        return '{} && {}'.format(launchers, cmd)
    elif shs:
        launchers = " && ".join('. "{}"'.format(f) for f in shs)
        return '{} && {}'.format(launchers, cmd)
    elif ps1s:
        # TODO: at the moment it only works with path without spaces
        launchers = " ; ".join('{}'.format(f) for f in ps1s)
        return 'powershell.exe {} ; cmd /c {}'.format(launchers, cmd)
    else:
        return cmd
Example #2
0
def _generate_aggregated_env(conanfile):
    def deactivates(filenames):
        # FIXME: Probably the order needs to be reversed
        result = []
        for s in filenames:
            folder, f = os.path.split(s)
            result.append(os.path.join(folder, "deactivate_{}".format(f)))
        return result

    for group, env_scripts in conanfile.env_scripts.items():
        subsystem = deduce_subsystem(conanfile, group)
        bats = []
        shs = []
        ps1s = []
        for env_script in env_scripts:
            path = os.path.join(conanfile.generators_folder, env_script)
            if env_script.endswith(".bat"):
                bats.append(path)
            elif env_script.endswith(".sh"):
                shs.append(subsystem_path(subsystem, path))
            elif env_script.endswith(".ps1"):
                ps1s.append(path)
        if shs:

            def sh_content(files):
                return ". " + " && . ".join('"{}"'.format(s) for s in files)

            filename = "conan{}.sh".format(group)
            save(os.path.join(conanfile.generators_folder, filename),
                 sh_content(shs))
            save(
                os.path.join(conanfile.generators_folder,
                             "deactivate_{}".format(filename)),
                sh_content(deactivates(shs)))
        if bats:

            def bat_content(files):
                return "\r\n".join(["@echo off"] +
                                   ['call "{}"'.format(b) for b in files])

            filename = "conan{}.bat".format(group)
            save(os.path.join(conanfile.generators_folder, filename),
                 bat_content(bats))
            save(
                os.path.join(conanfile.generators_folder,
                             "deactivate_{}".format(filename)),
                bat_content(deactivates(bats)))
        if ps1s:

            def ps1_content(files):
                return "\r\n".join(['& "{}"'.format(b) for b in files])

            filename = "conan{}.ps1".format(group)
            save(os.path.join(conanfile.generators_folder, filename),
                 ps1_content(ps1s))
            save(
                os.path.join(conanfile.generators_folder,
                             "deactivate_{}".format(filename)),
                ps1_content(deactivates(ps1s)))
Example #3
0
    def _adjust_path(self, path):
        if is_msvc(self._conanfile):
            path = path.replace('/', '\\')
        else:
            path = path.replace('\\', '/')

        path = subsystem_path(self._subsystem, path)
        return '"%s"' % path if ' ' in path else path
Example #4
0
def environment_wrap_command(env_filenames, cmd, subsystem=None, cwd=None):
    assert env_filenames
    filenames = [env_filenames
                 ] if not isinstance(env_filenames, list) else env_filenames
    bats, shs = [], []

    cwd = cwd or os.getcwd()

    for f in filenames:
        f = f if os.path.isabs(f) else os.path.join(cwd, f)
        if f.lower().endswith(".sh"):
            if os.path.isfile(f):
                f = subsystem_path(subsystem, f)
                shs.append(f)
        elif f.lower().endswith(".bat"):
            if os.path.isfile(f):
                bats.append(f)
        else:  # Simple name like "conanrunenv"
            path_bat = "{}.bat".format(f)
            path_sh = "{}.sh".format(f)
            if os.path.isfile(path_bat):
                bats.append(path_bat)
            elif os.path.isfile(path_sh):
                path_sh = subsystem_path(subsystem, path_sh)
                shs.append(path_sh)

    if bats and shs:
        raise ConanException(
            "Cannot wrap command with different envs, {} - {}".format(
                bats, shs))

    if bats:
        launchers = " && ".join('"{}"'.format(b) for b in bats)
        return '{} && {}'.format(launchers, cmd)
    elif shs:
        launchers = " && ".join('. "{}"'.format(f) for f in shs)
        return '{} && {}'.format(launchers, cmd)
    else:
        return cmd
Example #5
0
    def configure(self, build_script_folder=None):
        """
        http://jingfenghanmax.blogspot.com.es/2010/09/configure-with-host-target-and-build.html
        https://gcc.gnu.org/onlinedocs/gccint/Configure-Terms.html
        """
        source = self._conanfile.source_folder
        if build_script_folder:
            source = os.path.join(self._conanfile.source_folder,
                                  build_script_folder)

        configure_cmd = "{}/configure".format(source)
        subsystem = deduce_subsystem(self._conanfile, scope="build")
        configure_cmd = subsystem_path(subsystem, configure_cmd)
        cmd = '"{}" {}'.format(configure_cmd, self._configure_args)
        self._conanfile.output.info("Calling:\n > %s" % cmd)
        self._conanfile.run(cmd)
Example #6
0
    def get_str(self, placeholder, subsystem, pathsep):
        """
        :param subsystem:
        :param placeholder: a OS dependant string pattern of the previous env-var value like
        $PATH, %PATH%, et
        :param pathsep: The path separator, typically ; or :
        :return: a string representation of the env-var value, including the $NAME-like placeholder
        """
        values = []
        for v in self._values:
            if v is _EnvVarPlaceHolder:
                if placeholder:
                    values.append(placeholder.format(name=self._name))
            else:
                if self._path:
                    v = subsystem_path(subsystem, v)
                values.append(v)
        if self._path:
            return pathsep.join(values)

        return self._sep.join(values)
Example #7
0
def unix_path(conanfile, path):
    if not conanfile.win_bash:
        return path
    subsystem = deduce_subsystem(conanfile, scope="build")
    return subsystem_path(subsystem, path)