<commit_before>from pprof.settings import config


def lt_clang(cflags, ldflags, func=None):
    """Return a clang that hides :cflags: and :ldflags: from reordering of
    libtool.

    This will generate a wrapper script in :p:'s builddir and return a path
    to it.

    :cflags: the cflags libtool is not allowed to see.
    :ldflags: the ldflags libtool is not allowed to see.
    :returns: path to the new clang.

    """
    from plumbum import local

    print_libtool_sucks_wrapper("clang", cflags, ldflags, clang, func)
    return local["./clang"]


def lt_clang_cxx(cflags, ldflags, func=None):
    """Return a clang that hides :cflags: and :ldflags: from reordering of
    libtool.

    This will generate a wrapper script in :p:'s builddir and return a path
    to it.

    :cflags: the cflags libtool is not allowed to see.
    :ldflags: the ldflags libtool is not allowed to see.
    :returns: path to the new clang.

    """
    from plumbum import local

    print_libtool_sucks_wrapper("clang++", cflags, ldflags, clang_cxx, func)
    return local["./clang++"]


def print_libtool_sucks_wrapper(filepath, cflags, ldflags, compiler, func):
    """Print a libtool wrapper that hides :flags_to_hide: from libtool.

    :filepath:
        Where should the new compiler be?
    :flags_to_hide:
        List of flags that should be hidden from libtool
    :compiler:
        The compiler we should actually call
    """
    from plumbum.cmd import chmod
    from cloud.serialization import cloudpickle as cp
    from pprof.project import PROJECT_BLOB_F_EXT
    from os.path import abspath

    blob_f = abspath(filepath + PROJECT_BLOB_F_EXT)
    if func is not None:
        with open(blob_f, 'wb') as b:
            b.write(cp.dumps(func))

    with open(filepath, 'w') as wrapper:
        lines = '''#!/usr/bin/env python
# encoding: utf-8

from plumbum import ProcessExecutionError, local, FG
from plumbum.commands.modifiers import TEE
from pprof.utils.run import GuardedRunException
from pprof.experiment import to_utf8
from os import path
import pickle

from pprof.settings import config
config["db_host"] = "{db_host}"
config["db_port"] = "{db_port}"
config["db_name"] = "{db_name}"
config["db_user"] = "{db_user}"
config["db_pass"] = "{db_pass}"

cc=local[\"{CC}\"]
cflags={CFLAGS}
ldflags={LDFLAGS}

from sys import argv
import os
import sys

def call_original_compiler(input_files, cc, cflags, ldflags, flags):
    final_command = None
    retcode=0
    try:
        if len(input_files) > 0:
            if "-c" in flags:
                final_command = cc["-Qunused-arguments", cflags, ldflags, flags]
            else:
                final_command = cc["-Qunused-arguments", cflags, ldflags, flags]
        else:
            final_command = cc["-Qunused-arguments", flags]

        retcode, stdout, stderr = final_command & TEE

        if len(stdout) > 0:
            print stdout
        if len(stderr) > 0:
            print stderr
    except ProcessExecutionError as e:
        #FIXME: Write the fact that we had to fall back to the default
        #compiler somewhere
        retcode, _, _ = cc[flags] & TEE

    return (retcode, final_command)


input_files = [ x for x in argv[1:] if not '-' is x[0] ]
flags = argv[1:]
f = None

retcode, final_cc = call_original_compiler(input_files, cc, cflags, ldflags,
                                           flags)

with local.env(PPROF_DB_HOST="{db_host}",
           PPROF_DB_PORT="{db_port}",
           PPROF_DB_NAME="{db_name}",
           PPROF_DB_USER="{db_user}",
           PPROF_DB_PASS="{db_pass}"):
    """ FIXME: This is just a quick workaround. """
    if "conftest.c" not in input_files:
        with local.env(PPROF_CMD=str(final_cc)):
            if path.exists("{blobf}"):
                with open("{blobf}", "rb") as p:<commit_msg>fix: retry compiler command without custom flags, after failure<commit_after>from pprof.settings import config


def lt_clang(cflags, ldflags, func=None):
    """Return a clang that hides :cflags: and :ldflags: from reordering of
    libtool.

    This will generate a wrapper script in :p:'s builddir and return a path
    to it.

    :cflags: the cflags libtool is not allowed to see.
    :ldflags: the ldflags libtool is not allowed to see.
    :returns: path to the new clang.

    """
    from plumbum import local

    print_libtool_sucks_wrapper("clang", cflags, ldflags, clang, func)
    return local["./clang"]


def lt_clang_cxx(cflags, ldflags, func=None):
    """Return a clang that hides :cflags: and :ldflags: from reordering of
    libtool.

    This will generate a wrapper script in :p:'s builddir and return a path
    to it.

    :cflags: the cflags libtool is not allowed to see.
    :ldflags: the ldflags libtool is not allowed to see.
    :returns: path to the new clang.

    """
    from plumbum import local

    print_libtool_sucks_wrapper("clang++", cflags, ldflags, clang_cxx, func)
    return local["./clang++"]


def print_libtool_sucks_wrapper(filepath, cflags, ldflags, compiler, func):
    """Print a libtool wrapper that hides :flags_to_hide: from libtool.

    :filepath:
        Where should the new compiler be?
    :flags_to_hide:
        List of flags that should be hidden from libtool
    :compiler:
        The compiler we should actually call
    """
    from plumbum.cmd import chmod
    from cloud.serialization import cloudpickle as cp
    from pprof.project import PROJECT_BLOB_F_EXT
    from os.path import abspath

    blob_f = abspath(filepath + PROJECT_BLOB_F_EXT)
    if func is not None:
        with open(blob_f, 'wb') as b:
            b.write(cp.dumps(func))

    with open(filepath, 'w') as wrapper:
        lines = '''#!/usr/bin/env python
# encoding: utf-8

from plumbum import ProcessExecutionError, local, FG
from plumbum.commands.modifiers import TEE
from pprof.utils.run import GuardedRunException
from pprof.experiment import to_utf8
from os import path
import logging
import pickle

from pprof.settings import config
config["db_host"] = "{db_host}"
config["db_port"] = "{db_port}"
config["db_name"] = "{db_name}"
config["db_user"] = "{db_user}"
config["db_pass"] = "{db_pass}"

cc=local[\"{CC}\"]
cflags={CFLAGS}
ldflags={LDFLAGS}

from sys import argv
import os
import sys

log = logging.getLogger("clang")
log.addHandler(logging.StreamHandler(stream=sys.stderr))

def really_exec(cmd):
    try:
        log.info("Trying - %s", str(cmd))
        return ( cmd & TEE )
    except (GuardedRunException, ProcessExecutionError) as e:
        log.error("Failed to execute - %s", str(cmd))
        raise e


def call_original_compiler(input_files, cc, cflags, ldflags, flags):
    final_command = None
    retcode=0
    try:
        if len(input_files) > 0:
            if "-c" in flags:
                final_command = cc["-Qunused-arguments", cflags, ldflags, flags]
            else:
                final_command = cc["-Qunused-arguments", cflags, ldflags, flags]
        else:
            final_command = cc["-Qunused-arguments", flags]

        retcode, stdout, stderr = really_exec(final_command)

    except (GuardedRunException, ProcessExecutionError) as e:
        log.warn("Fallback to original flags and retry.")
        final_command = cc[flags]
        retcode, _, _ = really_exec(final_command)

    return (retcode, final_command)


input_files = [ x for x in argv[1:] if not '-' is x[0] ]
flags = argv[1:]
f = None

retcode, final_cc = call_original_compiler(input_files, cc, cflags, ldflags,
                                           flags)

with local.env(PPROF_DB_HOST="{db_host}",
           PPROF_DB_PORT="{db_port}",
           PPROF_DB_NAME="{db_name}",
           PPROF_DB_USER="{db_user}",
           PPROF_DB_PASS="{db_pass}"):
    """ FIXME: This is just a quick workaround. """
    if "conftest.c" not in input_files:
        with local.env(PPROF_CMD=str(final_cc)):
            if path.exists("{blobf}"):
                with open("{blobf}", "rb") as p:
{'fraction_non_alphanumeric': 0.10469495355259854, 'fraction_numerical': 0.002008536279186543, 'mean_word_length': 2.957774465971187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '82370', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2534, 'n_words': 787}