Skip to content

Commit

Permalink
Add catkin support (#14)
Browse files Browse the repository at this point in the history
* Add catkin support

* Rename genpy_stubgen to genpyi

* Update README
  • Loading branch information
bonprosoft authored Apr 27, 2021
1 parent fec7afc commit 6914e3e
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 20 deletions.
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.0.2)
project(genpyi)
find_package(catkin REQUIRED COMPONENTS genmsg genpy)

catkin_package(
CATKIN_DEPENDS genmsg genpy
CFG_EXTRAS genpyi-extras.cmake
)

add_subdirectory(scripts)

file(WRITE ${CATKIN_DEVEL_PREFIX}/${GENMSG_LANGS_DESTINATION}/genpyi "Python Stub")
install(FILES ${CATKIN_DEVEL_PREFIX}/${GENMSG_LANGS_DESTINATION}/genpyi
DESTINATION ${GENMSG_LANGS_DESTINATION})

catkin_python_setup()
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@ pip install genpy-stubgen

## Usage

### catkin

Add `genpyi` along with `message_generation` to `find_package` in CMakeLists.txt.
`genmsg` will find `genpyi` automatically when building msg/srv files.

Also, keep in mind that your package should have the build dependency for `genpyi` in `package.xml`
to make sure that catkin finishes the build of `genpyi` before building your package.

Examples:

- CMakeLists.txt
```cmake
find_package(catkin REQUIRED COMPONENTS std_msgs message_generation genpyi)
```
- package.xml
```xml
<build_depend>genpyi</build_depend>
```

### CLI

```
$ genpy_stubgen --help
Usage: genpy_stubgen [-h] {msg,srv,module} ...
positional arguments:
{msg,srv,module}
msg Generate stub files from .msg files
Expand Down Expand Up @@ -74,22 +92,3 @@ $ genpy_stubgen srv nav_msgs --out-dir out \
--out-dir OUT_DIR Output directory. If the option is unset, __init__.pyi
will be generated in the same directory as package_dir.
```

### CMake

**TODO**

Call `generate_genpy_stub` along with `generate_messages` and `add_service_files`.

Examples:
```cmake
generate_genpy_stub(
DEPENDENCIES
std_msgs
geometry_msgs
MESSAGES
Custom.msg
SERVICES
CustomService.srv
)
```
74 changes: 74 additions & 0 deletions cmake/genpyi-extras.cmake.em
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@[if DEVELSPACE]@
# location of scripts in develspace
set(GENPYI_DIR "@(CMAKE_CURRENT_SOURCE_DIR)/scripts")
@[else]@
# location of scripts in installspace
set(GENPYI_DIR "${genpyi_DIR}/../../../@(CATKIN_PACKAGE_BIN_DESTINATION)")
@[end if]@

set(GENPYI_BIN ${GENPYI_DIR}/run_genpyi.py)

macro(_generate_genpyi ARG_KIND ARG_PKG ARG_FILE ARG_IFLAGS ARG_MSG_DEPS ARG_GEN_OUTPUT_DIR)
file(MAKE_DIRECTORY ${ARG_GEN_OUTPUT_DIR})

# in order to get output file path
get_filename_component(FILE_NAME ${ARG_FILE} NAME)
get_filename_component(FILE_SHORT_NAME ${ARG_FILE} NAME_WE)

set(GENERATED_FILE_NAME _${FILE_SHORT_NAME}.pyi)
set(GEN_OUTPUT_FILE ${ARG_GEN_OUTPUT_DIR}/${GENERATED_FILE_NAME})

add_custom_command(OUTPUT ${GEN_OUTPUT_FILE}
DEPENDS ${GENPYI_BIN} ${ARG_FILE} ${ARG_MSG_DEPS}
COMMAND ${CATKIN_ENV} ${PYTHON_EXECUTABLE} ${GENPYI_BIN} ${ARG_KIND} ${ARG_PKG}
--out-dir ${ARG_GEN_OUTPUT_DIR}
${ARG_IFLAGS}
${ARG_FILE}
COMMENT "Generating Python Stub from ${ARG_PKG}/${FILE_SHORT_NAME}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)

list(APPEND ALL_GEN_OUTPUT_FILES_pyi ${GEN_OUTPUT_FILE})

endmacro()

macro(_generate_msg_pyi ARG_PKG ARG_MSG ARG_IFLAGS ARG_MSG_DEPS ARG_GEN_OUTPUT_DIR)
_generate_genpyi("msg" ${ARG_PKG} ${ARG_MSG} "${ARG_IFLAGS}" "${ARG_MSG_DEPS}" "${ARG_GEN_OUTPUT_DIR}/msg")
endmacro()

macro(_generate_srv_pyi ARG_PKG ARG_SRV ARG_IFLAGS ARG_MSG_DEPS ARG_GEN_OUTPUT_DIR)
_generate_genpyi("srv" ${ARG_PKG} ${ARG_SRV} "${ARG_IFLAGS}" "${ARG_MSG_DEPS}" "${ARG_GEN_OUTPUT_DIR}/srv")
endmacro()

macro(_generate_module_pyi ARG_PKG ARG_GEN_OUTPUT_DIR ARG_GENERATED_FILES)
# place an empty __init__.pyi in the parent folder of msg/srv
if(NOT EXISTS ${ARG_GEN_OUTPUT_DIR}/__init__.pyi)
file(WRITE ${ARG_GEN_OUTPUT_DIR}/__init__.pyi "")
endif()

foreach(type "msg" "srv")
set(GEN_OUTPUT_DIR "${ARG_GEN_OUTPUT_DIR}/${type}")
set(GEN_OUTPUT_FILE ${GEN_OUTPUT_DIR}/__init__.pyi)

if(IS_DIRECTORY ${GEN_OUTPUT_DIR})
add_custom_command(OUTPUT ${GEN_OUTPUT_FILE}
DEPENDS ${GENPYI_BIN}
COMMAND ${CATKIN_ENV} ${PYTHON_EXECUTABLE} ${GENPYI_BIN} module
--module-finder py
--out-dir ${GEN_OUTPUT_DIR}
${GEN_OUTPUT_DIR}
COMMENT "Generating Python Stub ${type} __init__.pyi for ${ARG_PKG}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
list(APPEND ALL_GEN_OUTPUT_FILES_pyi ${GEN_OUTPUT_FILE})
endif()

endforeach()

endmacro()

# {lang}_INSTALL_DIR is to control ARG_GEN_OUTPUT_DIR
# See: https://github.com/ros/genmsg/blob/7d8b6ce6f43b6e39ea8261125d270f2d3062356f/cmake/pkg-genmsg.cmake.em#L85-L96
if(NOT EXISTS @(PROJECT_NAME)_SOURCE_DIR)
set(genpyi_INSTALL_DIR ${PYTHON_INSTALL_DIR})
endif()
33 changes: 33 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<?xml-model
href="http://download.ros.org/schema/package_format3.xsd"
schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>genpyi</name>
<version>0.0.1</version>
<description>
Python stub generator from genmsg specs
</description>
<maintainer email="[email protected]">Yuki Igarashi</maintainer>
<maintainer email="[email protected]">Tamaki Nishino</maintainer>
<license>Apache</license>

<url type="website">https://github.com/rospypi/genpy_stubgen</url>
<url type="bugtracker">https://github.com/rospypi/genpy_stubgen/issues</url>
<url type="repository">https://github.com/rospypi/genpy_stubgen</url>

<author>Yuki Igarashi</author>
<author>Tamaki Nishino</author>

<depend>genmsg</depend>
<depend>genpy</depend>

<buildtool_depend version_gte="0.5.74">catkin</buildtool_depend>
<buildtool_depend condition="$ROS_PYTHON_VERSION == 2">python-setuptools</buildtool_depend>
<buildtool_depend condition="$ROS_PYTHON_VERSION == 3">python3-setuptools</buildtool_depend>

<export>
<message_generator>pyi</message_generator>
<architecture_independent/>
</export>
</package>
3 changes: 3 additions & 0 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
catkin_install_python(
PROGRAMS run_genpyi.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
6 changes: 6 additions & 0 deletions scripts/run_genpyi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python
from genpy_stubgen.cli import cli


if __name__ == "__main__":
cli()

0 comments on commit 6914e3e

Please sign in to comment.