Example #1
0
def main():
    to_parse, cmd = split_args_to_parse()
    # get the module name (if any), then load it
    capture_module_name = os.path.basename(cmd[0]) if len(cmd) > 0 else None
    mod_name = get_module_name(capture_module_name)
    imported_module = None
    if mod_name:
        # There is module that supports the command
        imported_module = load_module(mod_name)

    # get the module's argparser and merge it with the global argparser
    module_argparser = []
    if imported_module:
        module_argparser.append(
            imported_module.create_argparser(capture_module_name))
    global_argparser = create_argparser(module_argparser)

    args = global_argparser.parse_args(to_parse)

    if imported_module is not None:
        utils.configure_logging(args)
        try:
            logging.info('output of locale.getdefaultlocale(): %s',
                         str(locale.getdefaultlocale()))
        except (locale.Error, ValueError) as e:
            logging.info('locale.getdefaultlocale() failed with exception: %s',
                         str(e))
        logging.info('encoding we chose in the end: %s', config.CODESET)
        logging.info('Running command %s',
                     ' '.join(map(utils.decode, sys.argv)))
        logging.info('Path to infer script %s (%s)', utils.decode(__file__),
                     os.path.realpath(utils.decode(__file__)))
        logging.info('Platform: %s', utils.decode(platform.platform()))

        def log_getenv(k):
            v = os.getenv(k)
            if v is not None:
                v = utils.decode(v)
            else:
                v = '<NOT SET>'
            logging.info('%s=%s', k, v)

        log_getenv('PATH')
        log_getenv('SHELL')
        log_getenv('PWD')

        capture_exitcode = imported_module.gen_instance(args, cmd).capture()
        if capture_exitcode != os.EX_OK:
            logging.error('Error during capture phase, exiting')
            exit(capture_exitcode)
        logging.info('Capture phase was successful')
    elif capture_module_name is not None:
        # There was a command, but it's not supported
        utils.stdout('Command "{cmd}" not recognised'.format(
            cmd='' if capture_module_name is None else capture_module_name))
        global_argparser.print_help()
        sys.exit(1)
    else:
        global_argparser.print_help()
        sys.exit(os.EX_OK)
Example #2
0
def get_build_output(build_cmd):
    from inferlib import utils
    #  TODO make it return generator to be able to handle large builds
    proc = subprocess.Popen(build_cmd, stdout=subprocess.PIPE)
    (out_chars, err_chars) = proc.communicate()
    out = utils.decode(out_chars) if out_chars is not None else ''
    err = utils.decode(err_chars) if err_chars is not None else ''
    if proc.returncode != os.EX_OK:
        utils.stderr(
            'ERROR: couldn\'t run compilation command `{}`'.format(build_cmd))
        logging.error(
            'ERROR: couldn\'t run compilation command `{}`:\n\
            *** stdout:\n{}\n*** stderr:\n{}\n'
            .format(build_cmd, out, err))
    return (proc.returncode, (out, err))
Example #3
0
 def log_getenv(k):
     v = os.getenv(k)
     if v is not None:
         v = utils.decode(v)
     else:
         v = '<NOT SET>'
     logging.info('%s=%s', k, v)
Example #4
0
 def log_getenv(k):
     v = os.getenv(k)
     if v is not None:
         v = utils.decode(v)
     else:
         v = '<NOT SET>'
     logging.info('%s=%s', k, v)
Example #5
0
File: util.py Project: tashby/infer
def get_build_output(build_cmd):
    #  TODO make it return generator to be able to handle large builds
    proc = subprocess.Popen(build_cmd, stdout=subprocess.PIPE)
    (verbose_out_chars, _) = proc.communicate()
    if proc.returncode != 0:
        utils.stderr(
            'ERROR: couldn\'t run compilation command `{}`'.format(build_cmd))
        return (proc.returncode, None)
    out = utils.decode(verbose_out_chars).split('\n')
    return (os.EX_OK, out)
Example #6
0
 def __init__(self, args, cmd):
     self.args = args
     # TODO: make the extraction of targets smarter
     self.build_cmd = [cmd[0], '--debug'] + cmd[1:]
     # That contains javac version as well
     version_str = util.run_cmd_ignore_fail([cmd[0], '--version'])
     path = os.path.join(self.args.infer_out,
                         config.JAVAC_FILELISTS_FILENAME)
     if not os.path.exists(path):
         os.mkdir(path)
     logging.info('Running with:\n' + utils.decode(version_str))
Example #7
0
 def __init__(self, args, cmd):
     self.args = args
     # TODO: make the extraction of targets smarter
     self.build_cmd = [cmd[0], '--debug'] + cmd[1:]
     # That contains javac version as well
     version_str = util.run_cmd_ignore_fail([cmd[0], '--version'])
     path = os.path.join(self.args.infer_out,
                         config.JAVAC_FILELISTS_FILENAME)
     if not os.path.exists(path):
         os.mkdir(path)
     logging.info('Running with:\n' + utils.decode(version_str))
Example #8
0
def init_stats(args, start_time):
    """Returns dictionary with target independent statistics.
    """
    return {
        "float": {},
        "int": {"cores": multiprocessing.cpu_count(), "time": int(time.time()), "start_time": int(round(start_time))},
        "normal": {
            "debug": str(args.debug),
            "analyzer": args.analyzer,
            "machine": platform.machine(),
            "node": platform.node(),
            "project": utils.decode(os.path.basename(os.getcwd())),
            "revision": utils.vcs_revision(),
            "branch": utils.vcs_branch(),
            "system": platform.system(),
            "infer_version": utils.infer_version(),
            "infer_branch": utils.infer_branch(),
        },
    }
Example #9
0
def init_stats(args, start_time):
    """Returns dictionary with target independent statistics.
    """
    return {
        'float': {},
        'int': {
            'cores': multiprocessing.cpu_count(),
            'time': int(time.time()),
            'start_time': int(round(start_time)),
        },
        'normal': {
            'debug': str(args.debug),
            'analyzer': args.analyzer,
            'machine': platform.machine(),
            'node': platform.node(),
            'project': utils.decode(os.path.basename(os.getcwd())),
            'revision': utils.vcs_revision(),
            'branch': utils.vcs_branch(),
            'system': platform.system(),
            'infer_version': utils.infer_version(),
            'infer_branch': utils.infer_branch(),
        }
    }
Example #10
0
def init_stats(args, start_time):
    """Returns dictionary with target independent statistics.
    """
    return {
        'float': {},
        'int': {
            'cores': multiprocessing.cpu_count(),
            'time': int(time.time()),
            'start_time': int(round(start_time)),
        },
        'normal': {
            'debug': str(args.debug),
            'analyzer': args.analyzer,
            'machine': platform.machine(),
            'node': platform.node(),
            'project': utils.decode(os.path.basename(os.getcwd())),
            'revision': utils.vcs_revision(),
            'branch': utils.vcs_branch(),
            'system': platform.system(),
            'infer_version': utils.infer_version(),
            'infer_branch': utils.infer_branch(),
        }
    }
Example #11
0
def main():
    to_parse, cmd = split_args_to_parse()
    # get the module name (if any), then load it
    capture_module_name = os.path.basename(cmd[0]) if len(cmd) > 0 else None
    mod_name = get_module_name(capture_module_name)
    imported_module = None
    if mod_name:
        # There is module that supports the command
        imported_module = load_module(mod_name)

    # get the module's argparser and merge it with the global argparser
    module_argparser = []
    if imported_module:
        module_argparser.append(
            imported_module.create_argparser(capture_module_name)
        )
    global_argparser = create_argparser(module_argparser)

    args = global_argparser.parse_args(to_parse)

    remove_infer_out = (imported_module is not None and
                        not args.reactive and
                        capture_module_name != 'analyze' and
                        not args.buck)
    if remove_infer_out:
        analyze.remove_infer_out(args.infer_out)

    if imported_module is not None:
        analyze.create_results_dir(args.infer_out)
        analyze.reset_start_file(args.infer_out,
                                 touch_if_present=not args.continue_capture)

        utils.configure_logging(args)
        logging.info('output of locale.getdefaultlocale(): %s',
                     str(locale.getdefaultlocale()))
        logging.info('encoding we chose in the end: %s',
                     config.CODESET)
        logging.info('Running command %s',
                     ' '.join(map(utils.decode, sys.argv)))
        logging.info('Path to infer script %s (%s)', utils.decode(__file__),
                     os.path.realpath(utils.decode(__file__)))
        if not args.buck:
            # Should not print the Infer version when Infer is run by Buck.
            # Buck reads the whole stderr output to compute the target key
            logging.info(analyze.get_infer_version())
        logging.info('Platform: %s', utils.decode(platform.platform()))

        def log_getenv(k):
            v = os.getenv(k)
            if v is not None:
                v = utils.decode(v)
            else:
                v = '<NOT SET>'
            logging.info('%s=%s', k, v)

        log_getenv('PATH')
        log_getenv('SHELL')
        log_getenv('PWD')

        capture_exitcode = imported_module.gen_instance(args, cmd).capture()
        if capture_exitcode != os.EX_OK:
            logging.error('Error during capture phase, exiting')
            exit(capture_exitcode)
        logging.info('Capture phase was successful')
    elif capture_module_name is not None:
        # There was a command, but it's not supported
        utils.stdout('Command "{cmd}" not recognised'
                     .format(cmd='' if capture_module_name is None
                             else capture_module_name))
        global_argparser.print_help()
        sys.exit(1)
    else:
        global_argparser.print_help()
        sys.exit(os.EX_OK)

    buck_not_in_compilation_database_mode =  \
        mod_name == 'buck' and not args.use_compilation_database
    if not (buck_not_in_compilation_database_mode or
            mod_name == 'javac' or
            mod_name == 'java'):
        analysis = analyze.AnalyzerWrapper(args)
        analysis.analyze_and_report()
        analysis.save_stats()
Example #12
0
import argparse
import json
import logging
import os
import shutil
import stat
import subprocess
import sys
import tempfile
import time
import zipfile

from inferlib import config, issues, utils

DEFAULT_BUCK_OUT = os.path.join(utils.decode(os.getcwd()), 'buck-out')
DEFAULT_BUCK_OUT_GEN = os.path.join(DEFAULT_BUCK_OUT, 'gen')

INFER_JSON_REPORT = os.path.join(config.BUCK_INFER_OUT,
                                 config.JSON_REPORT_FILENAME)

INFER_SCRIPT = """\
#!/usr/bin/env {python_executable}
import subprocess
import sys

cmd = {infer_command} + ['--', 'javac'] + sys.argv[1:]
subprocess.check_call(cmd)
"""

Example #13
0
def get_build_output(build_cmd):
    #  TODO make it return generator to be able to handle large builds
    proc = subprocess.Popen(build_cmd, stdout=subprocess.PIPE)
    (verbose_out_chars, _) = proc.communicate()
    return utils.decode(verbose_out_chars).split('\n')
Example #14
0
def main():
    toplevel_envvar_value = os.environ.get(TOP_LEVEL_ENVVAR, None)
    is_toplevel_instance = False
    if toplevel_envvar_value is None:
        os.environ[TOP_LEVEL_ENVVAR] = '1'
        is_toplevel_instance = True

    to_parse, cmd = split_args_to_parse()
    # get the module name (if any), then load it
    capture_module_name = os.path.basename(cmd[0]) if len(cmd) > 0 else None
    mod_name = get_module_name(capture_module_name)
    imported_module = None
    if mod_name:
        # There is module that supports the command
        imported_module = load_module(mod_name)

    # get the module's argparser and merge it with the global argparser
    module_argparser = []
    if imported_module:
        module_argparser.append(
            imported_module.create_argparser(capture_module_name)
        )
    global_argparser = create_argparser(module_argparser)

    args = global_argparser.parse_args(to_parse)

    validate_args(imported_module, args)

    remove_infer_out = (imported_module is not None and
                        not args.reactive and
                        capture_module_name != 'analyze' and
                        not args.buck)
    if remove_infer_out:
        analyze.remove_infer_out(args.infer_out)

    if imported_module is not None:
        analyze.create_results_dir(args.infer_out)
        analyze.reset_start_file(args.infer_out,
                                 touch_if_present=not args.continue_capture)

        utils.configure_logging(args)
        logging.info('output of locale.getdefaultlocale(): %s',
                     str(locale.getdefaultlocale()))
        logging.info('encoding we chose in the end: %s',
                     config.CODESET)
        logging.info('Running command %s',
                     ' '.join(map(utils.decode, sys.argv)))
        logging.info('Path to infer script %s (%s)', utils.decode(__file__),
                     os.path.realpath(utils.decode(__file__)))
        logging.info(analyze.get_infer_version())
        logging.info('Platform: %s', utils.decode(platform.platform()))

        def log_getenv(k):
            v = os.getenv(k)
            if v is not None:
                v = utils.decode(v)
            else:
                v = '<NOT SET>'
            logging.info('%s=%s', k, v)

        log_getenv('PATH')
        log_getenv('SHELL')
        log_getenv('PWD')

        capture_exitcode = imported_module.gen_instance(args, cmd).capture()
        if capture_exitcode != os.EX_OK:
            logging.error('Error during capture phase, exiting')
            exit(capture_exitcode)
        logging.info('Capture phase was successful')
    elif capture_module_name is not None:
        # There was a command, but it's not supported
        utils.stdout('Command "{cmd}" not recognised'
                     .format(cmd='' if capture_module_name is None
                             else capture_module_name))
        global_argparser.print_help()
        sys.exit(1)
    else:
        global_argparser.print_help()
        sys.exit(os.EX_OK)

    buck_not_in_compilation_database_mode =  \
        mod_name == 'buck' and not args.use_compilation_database
    if not (buck_not_in_compilation_database_mode or
            mod_name == 'javac' or
            mod_name == 'java'):
        # Something should be already captured, otherwise analysis would fail
        if not os.path.exists(os.path.join(args.infer_out, 'captured')):
            print('There was nothing to analyze, exiting')
            exit(os.EX_USAGE)
        analysis = analyze.AnalyzerWrapper(args)
        analysis.analyze_and_report()
        analysis.save_stats()

    if is_toplevel_instance is True:
        buck_out_for_stats_aggregator = None
        if (mod_name == 'buck' and
            os.path.isfile(
                os.path.join(args.infer_out,
                             config.INFER_BUCK_DEPS_FILENAME))):
            buck_out_for_stats_aggregator = 'buck-out'
        logging.info('Aggregating stats')
        output = utils.run_infer_stats_aggregator(
            args.infer_out, buck_out_for_stats_aggregator)
        logging.info(output)
Example #15
0
def main():
    to_parse, cmd = split_args_to_parse()
    # first pass to see if a capture module is forced
    initial_argparser = IgnoreFailuresArgumentParser(
        parents=[analyze.infer_parser],
        add_help=False,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    initial_args = initial_argparser.parse_args(to_parse)

    # get the module name (if any), then load it
    capture_module_name = None
    if initial_args.force_integration is not None:
        capture_module_name = initial_args.force_integration
    elif len(cmd) > 0:
        capture_module_name = os.path.basename(cmd[0])
    mod_name = get_module_name(capture_module_name)
    imported_module = None
    if mod_name:
        # There is module that supports the command
        imported_module = load_module(mod_name)

    # get the module's argparser and merge it with the global argparser
    module_argparser = []
    if imported_module:
        module_argparser.append(
            imported_module.create_argparser(capture_module_name)
        )
    global_argparser = create_argparser(module_argparser)

    args = global_argparser.parse_args(to_parse)

    if imported_module is not None:
        utils.configure_logging(args)
        try:
            logging.info('output of locale.getdefaultlocale(): %s',
                         str(locale.getdefaultlocale()))
        except (locale.Error, ValueError) as e:
            logging.info('locale.getdefaultlocale() failed with exception: %s',
                         str(e))
        logging.info('encoding we chose in the end: %s',
                     config.CODESET)
        logging.info('Running command %s',
                     ' '.join(map(utils.decode, sys.argv)))
        logging.info('Path to infer script %s (%s)', utils.decode(__file__),
                     os.path.realpath(utils.decode(__file__)))
        logging.info('Platform: %s', utils.decode(platform.platform()))

        def log_getenv(k):
            v = os.getenv(k)
            if v is not None:
                v = utils.decode(v)
            else:
                v = '<NOT SET>'
            logging.info('%s=%s', k, v)

        log_getenv('PATH')
        log_getenv('SHELL')
        log_getenv('PWD')

        capture_exitcode = imported_module.gen_instance(args, cmd).capture()
        if capture_exitcode != os.EX_OK:
            logging.error('Error during capture phase, exiting')
            exit(capture_exitcode)
        logging.info('Capture phase was successful')
    elif capture_module_name is not None:
        # There was a command, but it's not supported
        utils.stderr('Command "{cmd}" not recognised'
                     .format(cmd='' if capture_module_name is None
                             else capture_module_name))
        global_argparser.print_help()
        sys.exit(1)
    else:
        global_argparser.print_help()
        sys.exit(os.EX_OK)
Example #16
0
def main():
    toplevel_envvar_value = os.environ.get(TOP_LEVEL_ENVVAR, None)
    is_toplevel_instance = False
    if toplevel_envvar_value is None:
        os.environ[TOP_LEVEL_ENVVAR] = '1'
        is_toplevel_instance = True

    to_parse, cmd = split_args_to_parse()
    # get the module name (if any), then load it
    capture_module_name = os.path.basename(cmd[0]) if len(cmd) > 0 else None
    mod_name = get_module_name(capture_module_name)
    imported_module = None
    if mod_name:
        # There is module that supports the command
        imported_module = load_module(mod_name)

    # get the module's argparser and merge it with the global argparser
    module_argparser = []
    if imported_module:
        module_argparser.append(
            imported_module.create_argparser(capture_module_name)
        )
    global_argparser = create_argparser(module_argparser)

    args = global_argparser.parse_args(to_parse)

    validate_args(imported_module, args)

    remove_infer_out = (imported_module is not None
                        and not args.reactive
                        and capture_module_name != 'analyze'
                        and not args.buck)
    if remove_infer_out:
        analyze.remove_infer_out(args.infer_out)

    if imported_module is not None:
        analyze.create_results_dir(args.infer_out)
        analyze.reset_start_file(args.infer_out,
                                 touch_if_present=not args.continue_capture)

        utils.configure_logging(args)
        logging.info('output of locale.getdefaultlocale(): %s',
                     str(locale.getdefaultlocale()))
        logging.info('encoding we chose in the end: %s',
                     config.CODESET)
        logging.info('Running command %s',
                     ' '.join(map(utils.decode, sys.argv)))
        logging.info('Path to infer script %s (%s)', utils.decode(__file__),
                     os.path.realpath(utils.decode(__file__)))
        logging.info(analyze.get_infer_version())
        logging.info('Platform: %s', utils.decode(platform.platform()))

        def log_getenv(k):
            v = os.getenv(k)
            if v is not None:
                v = utils.decode(v)
            else:
                v = '<NOT SET>'
            logging.info('%s=%s', k, v)

        log_getenv('PATH')
        log_getenv('SHELL')
        log_getenv('PWD')

        capture_exitcode = imported_module.gen_instance(args, cmd).capture()
        if capture_exitcode != os.EX_OK:
            logging.error('Error during capture phase, exiting')
            exit(capture_exitcode)
        logging.info('Capture phase was successful')
    elif capture_module_name is not None:
        # There was a command, but it's not supported
        utils.stdout('Command "{cmd}" not recognised'
                     .format(cmd='' if capture_module_name is None
                             else capture_module_name))
        global_argparser.print_help()
        sys.exit(1)
    else:
        global_argparser.print_help()
        sys.exit(os.EX_OK)

    if not (mod_name == 'buck' or mod_name == 'javac'):
        # Something should be already captured, otherwise analysis would fail
        if not os.path.exists(os.path.join(args.infer_out, 'captured')):
            print('There was nothing to analyze, exiting')
            exit(os.EX_USAGE)
        analysis = analyze.AnalyzerWrapper(args)
        analysis.analyze_and_report()
        analysis.save_stats()

    if is_toplevel_instance is True:
        buck_out_for_stats_aggregator = None
        if (mod_name == 'buck' and
            os.path.isfile(
                os.path.join(args.infer_out,
                             config.INFER_BUCK_DEPS_FILENAME))):
            buck_out_for_stats_aggregator = 'buck-out'
        logging.info('Aggregating stats')
        output = utils.run_infer_stats_aggregator(
            args.infer_out, buck_out_for_stats_aggregator)
        logging.info(output)

    if args.fail_on_bug:
        bugs_filename = os.path.join(args.infer_out,
                                     config.JSON_REPORT_FILENAME)
        try:
            bugs = utils.load_json_from_path(bugs_filename)
            if len(bugs) > 0:
                sys.exit(config.BUG_FOUND_ERROR_CODE)
        except OSError:
            pass
Example #17
0
def main():
    to_parse, cmd = split_args_to_parse()
    # get the module name (if any), then load it
    capture_module_name = os.path.basename(cmd[0]) if len(cmd) > 0 else None
    mod_name = get_module_name(capture_module_name)
    imported_module = None
    if mod_name:
        # There is module that supports the command
        imported_module = load_module(mod_name)

    # get the module's argparser and merge it with the global argparser
    module_argparser = []
    if imported_module:
        module_argparser.append(
            imported_module.create_argparser(capture_module_name)
        )
    global_argparser = create_argparser(module_argparser)

    args = global_argparser.parse_args(to_parse)

    remove_infer_out = (imported_module is not None and
                        not args.reactive and
                        capture_module_name != 'analyze' and
                        not args.buck)
    if remove_infer_out:
        analyze.remove_infer_out(args.infer_out)

    if imported_module is not None:
        analyze.create_results_dir(args.infer_out)
        analyze.reset_start_file(args.infer_out,
                                 touch_if_present=not args.continue_capture)

        utils.configure_logging(args)
        try:
            logging.info('output of locale.getdefaultlocale(): %s',
                         str(locale.getdefaultlocale()))
        except (locale.Error, ValueError) as e:
            logging.info('locale.getdefaultlocale() failed with exception: %s',
                         str(e))
        logging.info('encoding we chose in the end: %s',
                     config.CODESET)
        logging.info('Running command %s',
                     ' '.join(map(utils.decode, sys.argv)))
        logging.info('Path to infer script %s (%s)', utils.decode(__file__),
                     os.path.realpath(utils.decode(__file__)))
        logging.info('Platform: %s', utils.decode(platform.platform()))

        def log_getenv(k):
            v = os.getenv(k)
            if v is not None:
                v = utils.decode(v)
            else:
                v = '<NOT SET>'
            logging.info('%s=%s', k, v)

        log_getenv('PATH')
        log_getenv('SHELL')
        log_getenv('PWD')

        capture_exitcode = imported_module.gen_instance(args, cmd).capture()
        if capture_exitcode != os.EX_OK:
            logging.error('Error during capture phase, exiting')
            exit(capture_exitcode)
        logging.info('Capture phase was successful')
    elif capture_module_name is not None:
        # There was a command, but it's not supported
        utils.stdout('Command "{cmd}" not recognised'
                     .format(cmd='' if capture_module_name is None
                             else capture_module_name))
        global_argparser.print_help()
        sys.exit(1)
    else:
        global_argparser.print_help()
        sys.exit(os.EX_OK)
Example #18
0
import argparse
import json
import logging
import os
import shutil
import stat
import subprocess
import sys
import tempfile
import time
import zipfile

from inferlib import config, issues, utils

DEFAULT_BUCK_OUT = os.path.join(utils.decode(os.getcwd()), 'buck-out')
DEFAULT_BUCK_OUT_GEN = os.path.join(DEFAULT_BUCK_OUT, 'gen')

INFER_JSON_REPORT = os.path.join(config.BUCK_INFER_OUT,
                                 config.JSON_REPORT_FILENAME)

USE_INFER_CACHE = False

INFER_SCRIPT = """\
#!/usr/bin/env {python_executable}
import subprocess
import sys

cmd = {infer_command} + ['--', 'javac'] + sys.argv[1:]
subprocess.check_call(cmd)
"""
Example #19
0
def get_build_output(build_cmd):
    #  TODO make it return generator to be able to handle large builds
    proc = subprocess.Popen(build_cmd, stdout=subprocess.PIPE)
    (verbose_out_chars, _) = proc.communicate()
    return utils.decode(verbose_out_chars).split('\n')