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)
def capture_with_flavors(self): ret = self._run_buck_with_flavors() if not ret == os.EX_OK: return ret result_files = self._get_analysis_result_files() all_results = issues.merge_reports_from_paths(result_files) merged_results_path = os.path.join(self.args.infer_out, config.JSON_REPORT_FILENAME) utils.dump_json_to_path(all_results, merged_results_path) utils.stdout('Results saved in {results_path}'.format( results_path=merged_results_path)) return os.EX_OK
def capture_with_flavors(self): ret = self._run_buck_with_flavors() if not ret == os.EX_OK: return ret result_files = self._get_analysis_result_files() all_results = issues.merge_reports_from_paths(result_files) merged_results_path = os.path.join(self.args.infer_out, config.JSON_REPORT_FILENAME) utils.dump_json_to_path(all_results, merged_results_path) utils.stdout('Results saved in {results_path}' .format(results_path=merged_results_path)) return os.EX_OK
def run_compilation_commands(cmds, clean_cmd): """runs compilation commands, and suggests a project cleaning command in case there is nothing to compile. """ # TODO call it in parallel if len(cmds) == 0: utils.stdout("Nothing to compile. Try running `{}` first.".format(clean_cmd)) return os.EX_NOINPUT for cmd in cmds: if cmd.start() != os.EX_OK: return os.EX_SOFTWARE return os.EX_OK
def run_compilation_commands(cmds, clean_cmd): """runs compilation commands, and suggests a project cleaning command in case there is nothing to compile. """ # TODO call it in parallel if len(cmds) == 0: utils.stdout( 'Nothing to compile. Try running `{}` first.'.format(clean_cmd)) return os.EX_NOINPUT for cmd in cmds: if cmd.start() != os.EX_OK: return os.EX_SOFTWARE return os.EX_OK
def capture(self): # these settings will instruct xcodebuild on which clang to use self.cmd += ['CC={wrapper}'.format(wrapper=CLANG_WRAPPER)] self.cmd += ['CPLUSPLUS={wrapper}'.format(wrapper=CLANGPLUSPLUS_WRAPPER)] # skip the ProcessPCH phase to fix the "newer/older" incompatibility # error for the pch files generated by apple's clang and # the open-source one self.cmd += ['GCC_PRECOMPILE_PREFIX_HEADER=NO'] try: subprocess.check_call(self.cmd, env=self.get_envvars()) return os.EX_OK except subprocess.CalledProcessError as exc: if self.args.debug: traceback.print_exc() utils.stdout(exc.output) return exc.returncode
def capture(self): # these settings will instruct xcodebuild on which clang to use self.cmd += ['CC={wrapper}'.format(wrapper=CLANG_WRAPPER)] self.cmd += [ 'CPLUSPLUS={wrapper}'.format(wrapper=CLANGPLUSPLUS_WRAPPER) ] # skip the ProcessPCH phase to fix the "newer/older" incompatibility # error for the pch files generated by apple's clang and # the open-source one self.cmd += ['GCC_PRECOMPILE_PREFIX_HEADER=NO'] try: subprocess.check_call(self.cmd, env=self.get_envvars()) return os.EX_OK except subprocess.CalledProcessError as exc: if self.args.debug: traceback.print_exc() utils.stdout(exc.output) return exc.returncode
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()
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)
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)
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