Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
setup.py
35
36from pathlib import Path
37from setuptools import setup
38from setuptools.command.install import install
39import subprocess
40import os
41from shutil import copy, copytree
42
43# Your custom post-install command
45 def __init__(self):
46 print('Running stubs generator')
47 self.output_root = Path(__file__).parent.absolute() / 'gen'
48 self.target_path = Path(Path(__file__).parent.absolute() / 'visp')
49 self.output_root.mkdir(exist_ok=True)
50 self.package_data = []
51
52
53 def run(self):
54 self.generate_stubs()
55 self.rearrange_stubs()
57 def generate_stubs(self):
58 '''
59 Call Mypy's tool to generate stubs in an temporary folder.
60 CPP stubs are first generated, then the python specific ones
61 '''
62 subprocess.run(['stubgen', '-o', str(self.output_root.absolute()), '--ignore-errors', '--include-docstrings', '-p', 'visp._visp'], check=True)
63 subprocess.run(['stubgen', '-o', str(self.output_root.absolute()), '--ignore-errors', '--include-docstrings', '--parse-only', '-p', 'visp.python'], check=True)
64
65 def rearrange_stubs(self):
66
67 all_submodule_names = ['python']
68
69 # Generate stubs for the bindings (C++ side) and mock it so that they appear in the true 'visp' package
70 cpp_stubs = Path(self.output_root / 'visp' / '_visp')
71 self.target_path.mkdir(exist_ok=True)
72 for pyi_file in cpp_stubs.iterdir():
73 if pyi_file.name.endswith('.pyi'):
74 if pyi_file.stem != '__init__':
75
76 all_submodule_names.append(pyi_file.stem)
77 submodule_stub_folder = self.target_path / pyi_file.stem
78 submodule_stub_folder.mkdir(exist_ok=True)
79 submodules_stubs = submodule_stub_folder / '__init__.pyi'
80 copy(pyi_file, submodules_stubs) # Copy replace old files
81
82 # Add stubs for python part
83 python_stubs_path = Path(self.output_root / 'visp' / 'python')
84 copytree(str(python_stubs_path), str(self.target_path / 'python'), dirs_exist_ok=True)
85
86
87 # Generate a complete list of modules for the __init__ stubs
88 init_stubs_path = self.target_path / '__init__.pyi'
89 with open(init_stubs_path, 'w') as init_stubs_file:
90 # init_stubs_file.write(f'from ._visp import *{os.linesep}')
91
92 for submodule in all_submodule_names:
93 init_stubs_file.write(f'from . import {submodule}{os.linesep}')
94
96 def package_data_rec(module: Path, l):
97 for submodule_path in module.iterdir():
98 if submodule_path.name.endswith('.pyi'):
99 l.append(str(submodule_path.absolute()))
100 elif submodule_path.is_dir():
101 package_data_rec(submodule_path, l)
102 else:
103 print(f'Found file {submodule_path} but it will not be added to package data!')
104 package_data_rec(self.target_path, self.package_data)
105
106stubs_generator = StubsGenerator()
107stubs_generator.run()
108
109setup(
110 packages=['visp'],
111 zip_safe=False,
112 package_data = {
113 'visp': stubs_generator.package_data
114 },
115)
generate_stubs(self)
Definition setup.py:57
build_package_data(self)
Definition setup.py:95
rearrange_stubs(self)
Definition setup.py:65
Definition setup.py:1