def pool_worker_main(item: WorkItemInput, output: multiprocessing.queues.Queue) -> None: try: # TODO figure out a more reliable way to suppress this. Redirect output? # Ignore ctrl-c in workers to reduce noisy tracebacks (the parent will kill us): signal.signal(signal.SIGINT, signal.SIG_IGN) if hasattr(os, 'nice'): # analysis should run at a low priority os.nice(10) set_debug(False) filename, options, deadline = item stats: Counter[str] = Counter() options.stats = stats _, module_name = extract_module_from_file(filename) try: module = load_by_qualname(module_name) except NotFound: return except ErrorDuringImport as e: orig, frame = e.args message = AnalysisMessage(MessageType.IMPORT_ERR, str(orig), frame.filename, frame.lineno, 0, '') output.put((filename, stats, [message])) debug(f'Not analyzing "{filename}" because import failed: {e}') return messages = analyze_any(module, options) output.put((filename, stats, messages)) except BaseException as e: raise CrosshairInternal( 'Worker failed while analyzing ' + filename) from e
def unwalled_main(cmd_args: Union[List[str], argparse.Namespace]) -> None: if isinstance(cmd_args, argparse.Namespace): args = cmd_args else: args = command_line_parser().parse_args(cmd_args) set_debug(args.verbose) options = option_set_from_dict(args.__dict__) if sys.path and sys.path[0] != "": # fall back to current directory to look up modules sys.path.append("") if args.action == "check": exitcode = check(args, options, sys.stdout, sys.stderr) elif args.action == "diffbehavior": defaults = DEFAULT_OPTIONS.overlay( AnalysisOptionSet( per_condition_timeout=2.5, per_path_timeout=30.0, # mostly, we don't want to time out paths )) exitcode = diffbehavior(args, defaults.overlay(options), sys.stdout, sys.stderr) elif args.action == "watch": exitcode = watch(args, options) else: print(f'Unknown action: "{args.action}"', file=sys.stderr) exitcode = 2 sys.exit(exitcode)
def pool_worker_main(item: WorkItemInput, output: multiprocessing.queues.Queue) -> None: try: # TODO figure out a more reliable way to suppress this. Redirect output? # Ignore ctrl-c in workers to reduce noisy tracebacks (the parent will kill us): signal.signal(signal.SIGINT, signal.SIG_IGN) if hasattr(os, 'nice'): # analysis should run at a low priority os.nice(10) set_debug(False) (stats, messages) = pool_worker_process_item(item) filename = item[0] output.put((filename, stats, messages)) except BaseException as e: raise CrosshairInternal('Worker failed while analyzing ' + filename) from e
def main() -> None: args = command_line_parser().parse_args() set_debug(args.verbose) options = process_level_options(args) if sys.path and sys.path[0] != '': # fall back to current directory to look up modules sys.path.append('') if args.action == 'check': exitcode = check(args, options) elif args.action == 'showresults': exitcode = showresults(args, options) elif args.action == 'watch': exitcode = watch(args, options) else: print(f'Unknown action: "{args.action}"', file=sys.stderr) exitcode = 1 sys.exit(exitcode)
def mypy_and_check(cmd_args: Optional[List[str]] = None) -> None: if cmd_args is None: cmd_args = sys.argv[1:] cmd_args = ["check"] + cmd_args check_args, mypy_args = command_line_parser().parse_known_args(cmd_args) set_debug(check_args.verbose) mypy_cmd_args = mypy_args + check_args.target debug("Running mypy with the following arguments:", " ".join(mypy_cmd_args)) try: from mypy import api except ModuleNotFoundError: print("Unable to find mypy; skipping", file=sys.stderr) else: _mypy_out, mypy_err, mypy_ret = api.run(mypy_cmd_args) print(mypy_err, file=sys.stderr) if mypy_ret != 0: sys.exit(mypy_ret) engage_auditwall() debug("Running crosshair with these args:", check_args) unwalled_main(check_args)
def main(cmd_args: Optional[List[str]] = None) -> None: if cmd_args is None: cmd_args = sys.argv[1:] args = command_line_parser().parse_args(cmd_args) set_debug(args.verbose) options = process_level_options(args) if sys.path and sys.path[0] != '': # fall back to current directory to look up modules sys.path.append('') if args.action == 'check': exitcode = check(args, options, sys.stdout) elif args.action == 'diffbehavior': exitcode = diffbehavior(args, options) elif args.action == 'showresults': exitcode = showresults(args, options) elif args.action == 'watch': exitcode = watch(args, options) else: print(f'Unknown action: "{args.action}"', file=sys.stderr) exitcode = 1 sys.exit(exitcode)
def pool_worker_main(item: WorkItemInput, output: multiprocessing.queues.Queue) -> None: try: # TODO figure out a more reliable way to suppress this. Redirect output? # Ignore ctrl-c in workers to reduce noisy tracebacks (the parent will kill us): signal.signal(signal.SIGINT, signal.SIG_IGN) if hasattr(os, 'nice'): # <- is this the right way to detect availability? os.nice(10) # analysis should run at a low priority set_debug(False) member, options, deadline = item stats: Counter[str] = Counter() options.stats = stats try: fn = member.get_member() except NotFound: return messages = analyze_any(fn, options) output.put((member, stats, messages)) except BaseException as e: raise CrosshairInternal('Worker failed while analyzing ' + member.qual_name) from e
def unwalled_main(cmd_args: Union[List[str], argparse.Namespace]) -> int: parser = command_line_parser() if isinstance(cmd_args, argparse.Namespace): args = cmd_args else: args = parser.parse_args(cmd_args) if not args.action: parser.print_help(sys.stderr) return 2 set_debug(args.verbose) debug("Installed plugins:", installed_plugins) options = option_set_from_dict(args.__dict__) # fall back to current directory to look up modules with add_to_pypath(*([""] if sys.path and sys.path[0] != "" else [])): if args.action == "check": return check(args, options, sys.stdout, sys.stderr) elif args.action == "diffbehavior": defaults = DEFAULT_OPTIONS.overlay( AnalysisOptionSet( per_condition_timeout=2.5, per_path_timeout= 30.0, # mostly, we don't want to time out paths )) return diffbehavior(args, defaults.overlay(options), sys.stdout, sys.stderr) elif args.action == "cover": defaults = DEFAULT_OPTIONS.overlay( AnalysisOptionSet( per_condition_timeout=2.5, per_path_timeout= 30.0, # mostly, we don't want to time out paths )) return cover(args, defaults.overlay(options), sys.stdout, sys.stderr) elif args.action == "watch": return watch(args, options) else: print(f'Unknown action: "{args.action}"', file=sys.stderr) return 2
pre: bool(l) post[]: _ in l ''' return max(l) self.assertEqual(*check_unknown(f)) def test_min_ok(self) -> None: def f(l: List[float]) -> float: ''' pre: bool(l) post[]: _ in l ''' return min(l) self.assertEqual(*check_unknown(f)) def test_datetime_fail(self) -> None: import datetime def f(num_months: int) -> datetime.date: ''' post: _.year == 2000 ''' dt = datetime.date(2000, 1, 1) return dt + datetime.timedelta(days=30 * num_months) self.assertEqual(*check_fail(f)) if __name__ == '__main__': if ('-v' in sys.argv) or ('--verbose' in sys.argv): set_debug(True) unittest.main()
def pytest_configure(config): if "-v" in argv or "-vv" in argv: set_debug(True)