11"""
2- Creates an output .pyi file from a given python module
2+ Creates an output .pyi file from a given python module.
3+
4+ Arguments are:
5+ package outpath [subpackage outpath...] -- package mapped_file
36"""
47
58import importlib .util
@@ -29,8 +32,11 @@ def find_spec(cls, fullname, path, target=None):
2932 return importlib .util .spec_from_file_location (fullname , m )
3033
3134
32- def _write_pyi (package_name : str , output_pyi : str ):
35+ def _write_pyi (package_name , generated_pyi : T . Dict [ pathlib . PurePath , pathlib . Path ] ):
3336
37+ # We can't control where stubgen writes files, so tell it to output
38+ # to a temporary directory and then we copy the files from there to
39+ # our desired location
3440 with tempfile .TemporaryDirectory () as tmpdir :
3541 # Call pybind11-stubgen
3642 sys .argv = [
@@ -45,28 +51,40 @@ def _write_pyi(package_name: str, output_pyi: str):
4551
4652 pybind11_stubgen .main ()
4753
48- import os
49- os .system (f"find { tmpdir } " )
50-
5154 # stubgen doesn't take a direct output filename, so move the file
5255 # to our desired location
53- elems = package_name . split ( "." )
54- elems [ - 1 ] = f" { elems [ - 1 ] } .pyi"
55- pathlib . Path ( tmpdir , * elems ).rename (output_pyi )
56+ tmpdir_pth = pathlib . Path ( tmpdir )
57+ for infile , output in generated_pyi . items ():
58+ ( tmpdir_pth / infile ).rename (output )
5659
5760
5861def main ():
59- try :
60- _ , package_name , output_pyi = sys .argv [:3 ]
61- except ValueError :
62+
63+ generated_pyi : T .Dict [pathlib .PurePath , pathlib .Path ] = {}
64+ argv = sys .argv
65+
66+ if len (argv ) < 3 :
6267 print (inspect .cleandoc (__doc__ or "" ), file = sys .stderr )
6368 sys .exit (1 )
6469
70+ # Package name first
71+ package_name = argv [1 ]
72+
73+ # Output file map: input output
74+ idx = 2
75+ while idx < len (argv ):
76+ if argv [idx ] == "--" :
77+ idx += 1
78+ break
79+
80+ generated_pyi [pathlib .PurePath (argv [idx ])] = pathlib .Path (argv [idx + 1 ])
81+ idx += 2
82+
6583 # Arguments are used to set up the package map
6684 package_map = _PackageFinder .mapping
67- for i in range (3 , len (sys . argv ), 2 ):
68- package_map [sys . argv [i ]] = sys . argv [i + 1 ]
69-
85+ for i in range (idx , len (argv ), 2 ):
86+ package_map [argv [i ]] = argv [i + 1 ]
87+
7088 # Add parent packages too
7189 # .. assuming there are __init__.py in each package
7290 for pkg in list (package_map .keys ()):
@@ -76,15 +94,14 @@ def main():
7694 break
7795 ppkg = pkg [:idx ]
7896 if ppkg not in package_map :
79- package_map [ppkg ] = join (dirname (dirname (package_map [pkg ])), "__init__.py" )
97+ package_map [ppkg ] = join (
98+ dirname (dirname (package_map [pkg ])), "__init__.py"
99+ )
80100 pkg = ppkg
81101
82- import pprint
83- pprint .pprint (package_map )
84-
85102 sys .meta_path .insert (0 , _PackageFinder )
86103
87- _write_pyi (package_name , output_pyi )
104+ _write_pyi (package_name , generated_pyi )
88105
89106
90107if __name__ == "__main__" :
0 commit comments