Skip to content

Commit

Permalink
More structured logic for Python usage in packages (MetroRobots#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
DLu authored Jan 11, 2024
1 parent 7f5f216 commit 4a1d4c1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
28 changes: 21 additions & 7 deletions src/ros_glint/glinters/cmake_installs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

from ..core import glinter
from ..cmake_ordering import insert_in_order
from .cmake import check_complex_section, section_check, get_multiword_section
from ..python_types import get_python_usage, PythonUsage
from .cmake import check_complex_section, section_check, get_multiword_section, install_cmake_dependencies


class InstallType(IntEnum):
Expand Down Expand Up @@ -297,6 +298,20 @@ def export_cplusplus_libraries(package):
section_check(package.cmake, export_targets, 'ament_export_targets')


def check_cmake_python_buildtype(package):
if package.build_type == 'ament_cmake':
acp = 'ament_cmake_python'
build_tools = package.package_xml.get_packages_by_tag('buildtool_depend')
if acp not in build_tools:
package.package_xml.insert_new_packages('buildtool_depend', [acp])
package.build_type = acp

install_cmake_dependencies(package, {acp})

if package.setup_py is None:
create_setup_py(package)


@glinter
def update_misc_installs(package):
extra_files_by_folder = collections.defaultdict(list)
Expand Down Expand Up @@ -324,13 +339,12 @@ def update_misc_installs(package):

for subfolder in existing_install_folders:
install_section_check(package.cmake, [], InstallType.SHARE, package.ros_version, subfolder=subfolder)
buildtools = package.package_xml.get_packages_by_tag('buildtool_depend')
if package.build_type == 'ament_python' or 'ament_cmake_python' in buildtools:
if package.setup_py is None:
create_setup_py(package)

for folder, files in sorted(extra_files_by_folder.items()):
package.setup_py.include_data_files(files, folder)
if get_python_usage(package) != PythonUsage.NONE:
check_cmake_python_buildtype(package)
if package.ros_version == 2:
for folder, files in sorted(extra_files_by_folder.items()):
package.setup_py.include_data_files(files, folder)


@glinter
Expand Down
28 changes: 11 additions & 17 deletions src/ros_glint/glinters/python_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from ros_introspect.components.source_code import SourceCode
from ..core import glinter
from ..cmake_ordering import insert_in_order
from .cmake import install_cmake_dependencies, section_check
from .cmake_installs import install_section_check, InstallType
from ..python_types import get_python_usage, PythonUsage
from .cmake import section_check
from .cmake_installs import install_section_check, InstallType, check_cmake_python_buildtype
from ..util import set_executable
import re

Expand All @@ -21,10 +22,9 @@

@glinter
def check_python_marker(package):
buildtools = package.package_xml.get_packages_by_tag('buildtool_depend')

if package.build_type != 'ament_python' and 'ament_cmake_python' not in buildtools:
if get_python_usage(package) == PythonUsage.NONE:
return

resource_folder = package.root / 'resource'
marker_path = resource_folder / package.name
if marker_path.exists():
Expand Down Expand Up @@ -166,15 +166,12 @@ def get_entry_points(package):
@glinter
def update_python_installs(package):
# Part 1: Library Setup for ament_cmake
if package.build_type == 'ament_cmake' and package.get_source_by_tags('pylib'):
acp = 'ament_cmake_python'
build_tools = package.package_xml.get_packages_by_tag('buildtool_depend')
if acp not in build_tools:
package.package_xml.insert_new_packages('buildtool_depend', [acp])
python_usage = get_python_usage(package)
if python_usage != PythonUsage.NONE:
check_cmake_python_buildtype(package)

install_cmake_dependencies(package, {acp})

section_check(package.cmake, ['${PROJECT_NAME}'], 'ament_python_install_package')
if python_usage == PythonUsage.MIXED_CMAKE_PYTHON:
section_check(package.cmake, ['${PROJECT_NAME}'], 'ament_python_install_package')

# Part 2: Executables
execs = package.get_source_by_tags('pyscript', 'python')
Expand Down Expand Up @@ -214,9 +211,6 @@ def update_python_installs(package):
section_check(package.cmake, exec_fns, cmd, 'PROGRAMS')

elif package.build_type == 'ament_python':
if package.setup_py is None:
create_setup_py(package)

if package.setup_cfg is None:
package.add_file(SetupCFG(package.root / 'setup.cfg', package))

Expand Down Expand Up @@ -250,5 +244,5 @@ def update_python_installs(package):
if not contains_quoted_string(console_scripts, entry):
console_scripts.append(quote_string(entry))
package.setup_py.changed = True
elif package.build_type == 'ament_cmake':
elif python_usage == PythonUsage.MIXED_CMAKE_PYTHON:
install_section_check(package.cmake, exec_fns, InstallType.PYTHON, package.ros_version)
19 changes: 19 additions & 0 deletions src/ros_glint/python_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from enum import IntEnum


class PythonUsage(IntEnum):
NONE = 0
PURE_PYTHON = 1
MIXED_CMAKE_PYTHON = 2


def get_python_usage(package):
# If there is no Python library, return NONE
if not package.get_source_by_tags('pylib') and package.build_type != 'ament_python':
return PythonUsage.NONE

# If build type is ament_cmake or ament_cmake_python
if 'ament_cmake' in package.build_type:
return PythonUsage.MIXED_CMAKE_PYTHON

return PythonUsage.PURE_PYTHON

0 comments on commit 4a1d4c1

Please sign in to comment.