From c0f356721fcd2c1553e6e35ee28cdb398d0d2a99 Mon Sep 17 00:00:00 2001 From: Peter Law Date: Fri, 11 Nov 2022 22:20:43 +0000 Subject: [PATCH] First pass at building a stubs package from the sources This uses mypy's stubgen to derive stubs from the source code, then wraps the generated stubs in a minimal Python package. --- .github/workflows/build_stubs_package.yml | 34 +++++++++++++++++++++ resources/python_stubs/.gitignore | 6 ++++ resources/python_stubs/README.md | 7 +++++ resources/python_stubs/build.sh | 11 +++++++ resources/python_stubs/requirements.txt | 2 ++ resources/python_stubs/setup.py | 37 +++++++++++++++++++++++ 6 files changed, 97 insertions(+) create mode 100644 .github/workflows/build_stubs_package.yml create mode 100644 resources/python_stubs/.gitignore create mode 100644 resources/python_stubs/README.md create mode 100755 resources/python_stubs/build.sh create mode 100644 resources/python_stubs/requirements.txt create mode 100644 resources/python_stubs/setup.py diff --git a/.github/workflows/build_stubs_package.yml b/.github/workflows/build_stubs_package.yml new file mode 100644 index 00000000000..78971fbaf52 --- /dev/null +++ b/.github/workflows/build_stubs_package.yml @@ -0,0 +1,34 @@ +name: Build Python Stubs + +# TODO: work out where this should actually run +on: + push: + branches: + - master + pull_request: + +jobs: + build-python-stubs: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Python 3.11 + uses: actions/setup-python@v4 + with: + python-version: '3.11' + cache: pip + cache-dependency-path: resources/python_stubs/requirements.txt + + - name: Install Dependencies + run: | + pip install -r resources/python_stubs/requirements.txt + + - name: Build the package + run: | + resources/python_stubs/build.sh + + - uses: actions/upload-artifact@v3 + with: + name: dist + path: resources/python_stubs/dist diff --git a/resources/python_stubs/.gitignore b/resources/python_stubs/.gitignore new file mode 100644 index 00000000000..5e543c6e511 --- /dev/null +++ b/resources/python_stubs/.gitignore @@ -0,0 +1,6 @@ +*.egg-info + +controller-stubs/*.pyi + +build +dist diff --git a/resources/python_stubs/README.md b/resources/python_stubs/README.md new file mode 100644 index 00000000000..046b72857ae --- /dev/null +++ b/resources/python_stubs/README.md @@ -0,0 +1,7 @@ +# Webots Stubs + +Typing stubs for [Webots][webots]' `controller` package. + +These are auto-generated from the Python API which comes with Webots. + +[webots]: https://www.cyberbotics.com/ diff --git a/resources/python_stubs/build.sh b/resources/python_stubs/build.sh new file mode 100755 index 00000000000..1c19dc093e7 --- /dev/null +++ b/resources/python_stubs/build.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +cd $(dirname $0) + +rm -rf controller-stubs + +stubgen ../../lib/controller/python/controller --output . + +mv controller controller-stubs + +python setup.py sdist bdist_wheel diff --git a/resources/python_stubs/requirements.txt b/resources/python_stubs/requirements.txt new file mode 100644 index 00000000000..1f042e764c4 --- /dev/null +++ b/resources/python_stubs/requirements.txt @@ -0,0 +1,2 @@ +mypy +wheel diff --git a/resources/python_stubs/setup.py b/resources/python_stubs/setup.py new file mode 100644 index 00000000000..cd5c528437a --- /dev/null +++ b/resources/python_stubs/setup.py @@ -0,0 +1,37 @@ +from pathlib import Path + +from setuptools import setup, find_packages # type: ignore[import] + +long_description = (Path(__file__).parent / 'README.md').read_text() + +setup( + name='webots-stubs', + version='0.1.0', # TODO: version with the Webots version number? + url='https://www.cyberbotics.com/', + project_urls={ + 'Issue tracker': 'https://github.com/cyberbotics/webots/issues', + 'Source Code': 'https://github.com/cyberbotics/webots', + }, + description="Type stubs for Webots' 'controller' package.", + long_description=long_description, + long_description_content_type='text/markdown', + + package_data={'controller-stubs': ['*.pyi']}, + packages=['controller-stubs'], + + license='Apache 2.0', + + classifiers=[ + 'Development Status :: 2 - Pre-Alpha', + 'Natural Language :: English', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + 'Programming Language :: Python :: 3 :: Only', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + 'Programming Language :: Python :: 3.11', + 'Topic :: Software Development', + ], +)