def install_types(cache_dir: str, formatter: util.FancyFormatter, after_run: bool = False) -> None: """Install stub packages using pip if some missing stubs were detected.""" if not os.path.isdir(cache_dir): sys.stderr.write( "Error: no mypy cache directory (you must enable incremental mode)\n") sys.exit(2) fnam = build.missing_stubs_file(cache_dir) if not os.path.isfile(fnam): # If there are no missing stubs, generate no output. return with open(fnam) as f: packages = [line.strip() for line in f.readlines()] if after_run: print() print('Installing missing stub packages:') cmd = [sys.executable, '-m', 'pip', 'install'] + packages print(formatter.style(' '.join(cmd), 'none', bold=True)) print() x = input('Install? [yN] ') if not x.strip() or not x.lower().startswith('y'): print(formatter.style('mypy: Skipping installation', 'red', bold=True)) sys.exit(2) print() subprocess.run(cmd)
def console_entry() -> None: try: main(None, sys.stdout, sys.stderr) sys.stdout.flush() sys.stderr.flush() except BrokenPipeError: # Python flushes standard streams on exit; redirect remaining output # to devnull to avoid another BrokenPipeError at shutdown devnull = os.open(os.devnull, os.O_WRONLY) os.dup2(devnull, sys.stdout.fileno()) sys.exit(2) except KeyboardInterrupt: _, options = process_options(args=sys.argv[1:]) if options.show_traceback: sys.stdout.write(traceback.format_exc()) formatter = FancyFormatter(sys.stdout, sys.stderr, False) msg = "Interrupted\n" sys.stdout.write(formatter.style(msg, color="red", bold=True)) sys.stdout.flush() sys.stderr.flush() sys.exit(2)