Main entry point for the program.
(options: Options)
| 1839 | |
| 1840 | |
| 1841 | def generate_stubs(options: Options) -> None: |
| 1842 | """Main entry point for the program.""" |
| 1843 | mypy_opts = mypy_options(options) |
| 1844 | py_modules, pyc_modules, c_modules = collect_build_targets(options, mypy_opts) |
| 1845 | all_modules = py_modules + pyc_modules + c_modules |
| 1846 | all_module_names = sorted(m.module for m in all_modules) |
| 1847 | # Use parsed sources to generate stubs for Python modules. |
| 1848 | generate_asts_for_modules(py_modules, options.parse_only, mypy_opts, options.verbose) |
| 1849 | files = [] |
| 1850 | for mod in py_modules + pyc_modules: |
| 1851 | assert mod.path is not None, "Not found module was not skipped" |
| 1852 | target = mod.module.replace(".", "/") |
| 1853 | if os.path.basename(mod.path) in ["__init__.py", "__init__.pyc"]: |
| 1854 | target += "/__init__.pyi" |
| 1855 | else: |
| 1856 | target += ".pyi" |
| 1857 | target = os.path.join(options.output_dir, target) |
| 1858 | files.append(target) |
| 1859 | with generate_guarded(mod.module, target, options.ignore_errors, options.verbose): |
| 1860 | generate_stub_for_py_module( |
| 1861 | mod, |
| 1862 | target, |
| 1863 | parse_only=options.parse_only, |
| 1864 | inspect=options.inspect or mod in pyc_modules, |
| 1865 | include_private=options.include_private, |
| 1866 | export_less=options.export_less, |
| 1867 | include_docstrings=options.include_docstrings, |
| 1868 | doc_dir=options.doc_dir, |
| 1869 | all_modules=all_module_names, |
| 1870 | ) |
| 1871 | |
| 1872 | # Separately analyse C modules using different logic. |
| 1873 | for mod in c_modules: |
| 1874 | if any(py_mod.module.startswith(mod.module + ".") for py_mod in all_modules): |
| 1875 | target = mod.module.replace(".", "/") + "/__init__.pyi" |
| 1876 | else: |
| 1877 | target = mod.module.replace(".", "/") + ".pyi" |
| 1878 | target = os.path.join(options.output_dir, target) |
| 1879 | files.append(target) |
| 1880 | with generate_guarded(mod.module, target, options.ignore_errors, options.verbose): |
| 1881 | generate_stub_for_c_module( |
| 1882 | mod.module, |
| 1883 | target, |
| 1884 | known_modules=all_module_names, |
| 1885 | doc_dir=options.doc_dir, |
| 1886 | include_private=options.include_private, |
| 1887 | export_less=options.export_less, |
| 1888 | include_docstrings=options.include_docstrings, |
| 1889 | ) |
| 1890 | num_modules = len(all_modules) |
| 1891 | if not options.quiet and num_modules > 0: |
| 1892 | print("Processed %d modules" % num_modules) |
| 1893 | if len(files) == 1: |
| 1894 | print(f"Generated {files[0]}") |
| 1895 | else: |
| 1896 | print(f"Generated files under {common_dir_prefix(files)}" + os.sep) |
| 1897 | |
| 1898 |
searching dependent graphs…