Skip to content

Commit 9d01d90

Browse files
authored
Add stubgen autogeneration (#453)
* Add stubgen auto generaiton after bindings build * Generating stubs through cmake does not work as expected with ninja with a in build startegy and isnt working in install because the install order is random * fix missing quote * Add python path for stubgen * Try to find out what is wrong when generating * Change script to python , add Sofa.Component generation and allow the use of either pybind11 or mypy * fix action * try fix env for stubgen * try fix action * Add env for SOFA libs * Remove output * Add echot osee line * Add more informaiton * Fix python script * Try fix unit tests * Fix mistake in split * add filter to skip synchronizing files that already exist in dest folder. This should be removed once create_sofa_stubs is able to merge the generated stubs with existing implementation * Fix windows stubgen by printing a warning instead of throwing an exception when input type is unknown * Fix warning print
1 parent 0476b52 commit 9d01d90

File tree

4 files changed

+587
-1
lines changed

4 files changed

+587
-1
lines changed

.github/workflows/ci.yml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,41 @@ jobs:
7272
echo ${CCACHE_BASEDIR}
7373
ccache -s
7474
fi
75-
75+
76+
77+
- name: Set env vars for stubfiles
78+
shell: bash
79+
run: |
80+
#Setup env
81+
# Set env vars for tests
82+
if [[ "$RUNNER_OS" == "Windows" ]]; then
83+
echo "$WORKSPACE_INSTALL_PATH/lib" >> $GITHUB_PATH
84+
echo "$WORKSPACE_INSTALL_PATH/bin" >> $GITHUB_PATH
85+
elif [[ "$RUNNER_OS" == "macOS" ]]; then
86+
echo "DYLD_LIBRARY_PATH=$WORKSPACE_ARTIFACT_PATH/lib:$SOFA_ROOT/lib:$DYLD_LIBRARY_PATH" | tee -a $GITHUB_ENV
87+
else # Linux
88+
echo "LD_LIBRARY_PATH=$WORKSPACE_ARTIFACT_PATH/lib:$SOFA_ROOT/lib:$LD_LIBRARY_PATH" | tee -a $GITHUB_ENV
89+
fi
90+
91+
echo "PYTHONPATH=$WORKSPACE_INSTALL_PATH/lib/python3/site-packages" | tee -a $GITHUB_ENV
92+
93+
- name: Generate stubfiles
94+
shell: bash
95+
run: |
96+
97+
#Install stubgen
98+
${{ steps.sofa.outputs.python_exe }} -m pip install mypy pybind11-stubgen
99+
100+
#For now use pybind11. This might be parametrized as an input of this action
101+
echo "Launching the stub generation with '${{ steps.sofa.outputs.python_exe }} ${{ env.WORKSPACE_SRC_PATH }}/scripts/generate_stubs.py -d $WORKSPACE_INSTALL_PATH/lib/python3/site-packages -m Sofa --use_pybind11'"
102+
103+
${{ steps.sofa.outputs.python_exe }} ${{ env.WORKSPACE_SRC_PATH }}/scripts/generate_stubs.py -d "$WORKSPACE_INSTALL_PATH/lib/python3/site-packages" -m Sofa --use_pybind11
104+
105+
#Go back to previous env
106+
echo "PYTHONPATH=" | tee -a $GITHUB_ENV
107+
108+
109+
76110
- name: Set env vars for artifacts
77111
shell: bash
78112
run: |

scripts/generate_stubs.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
import argparse
3+
from utils import generate_module_stubs, generate_component_stubs
4+
5+
6+
def main(site_package_dir,modules_name,use_pybind11 = False):
7+
8+
work_dir = site_package_dir[0]
9+
modules = modules_name
10+
11+
#Generate stubs using either pybind11-stubgen or mypy version of stubgen
12+
13+
print(f"Generating stubgen for modules: {modules}")
14+
15+
for module_name in modules:
16+
generate_module_stubs(module_name, work_dir,use_pybind11)
17+
18+
#Generate stubs for components using the factory
19+
target_name="Sofa.Component"
20+
generate_component_stubs(work_dir,target_name)
21+
22+
23+
24+
if __name__ == "__main__":
25+
parser = argparse.ArgumentParser(
26+
prog='generate_stubs',
27+
description='Generates python stubs for SOFA modules')
28+
29+
parser.add_argument('--use_pybind11',action='store_true',help='If flag is present, will use pybind11-stubgen instead of mypy stugen')
30+
parser.add_argument('-d','--site_package_dir',nargs=1,help='Path to the site-package folder containing the SOFA modules')
31+
parser.add_argument('-m','--modules_name',nargs='+',help='List of modules names to generate stubs for')
32+
33+
args = parser.parse_args()
34+
35+
main(args.site_package_dir,args.modules_name,args.use_pybind11)

0 commit comments

Comments
 (0)