Skip to content

Commit

Permalink
add support for py_modules in setup.py (fix #399)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Dec 17, 2013
1 parent ede6229 commit c468981
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
31 changes: 31 additions & 0 deletions cmake/catkin_python_setup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,37 @@ function(catkin_python_setup)
endforeach()
endif()

# generate relay-script for each python module (and __init__.py files) if available
if(${PROJECT_NAME}_SETUP_PY_MODULES)
list(LENGTH ${PROJECT_NAME}_SETUP_PY_MODULES modules_count)
math(EXPR modules_range "${modules_count} - 1")
foreach(index RANGE ${modules_range})
list(GET ${PROJECT_NAME}_SETUP_PY_MODULES ${index} module)
list(GET ${PROJECT_NAME}_SETUP_PY_MODULE_DIRS ${index} module_dir)
set(PYTHON_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/${module_dir}/${module})
if(EXISTS ${PYTHON_SCRIPT})
get_filename_component(path ${module} PATH)
file(MAKE_DIRECTORY "${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_PYTHON_DESTINATION}/${path}")
configure_file(${catkin_EXTRAS_DIR}/templates/relay.py.in
${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_PYTHON_DESTINATION}/${module}
@ONLY)
# relay parent __init__.py files if they exist
while(NOT "${path}" STREQUAL "")
set(PYTHON_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/${module_dir}/${path}/__init__.py)
if(EXISTS ${PYTHON_SCRIPT})
file(MAKE_DIRECTORY "${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_PYTHON_DESTINATION}/${path}")
configure_file(${catkin_EXTRAS_DIR}/templates/relay.py.in
${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_PYTHON_DESTINATION}/${path}/__init__.py
@ONLY)
else()
message(WARNING "The module '${module_dir}/${module}' lacks an '__init__.py' file in the parent folder '${module_dir}/${path}' in project '${PROJECT_NAME}'")
endif()
get_filename_component(path ${path} PATH)
endwhile()
endif()
endforeach()
endif()

# generate relay-script for each python script
foreach(script ${${PROJECT_NAME}_SETUP_PY_SCRIPTS})
get_filename_component(name ${script} NAME)
Expand Down
23 changes: 20 additions & 3 deletions cmake/interrogate_setup_dot_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ def _get_locations(pkgs, package_dir):
return locations


def generate_cmake_file(package_name, version, scripts, package_dir, pkgs):
def generate_cmake_file(package_name, version, scripts, package_dir, pkgs, modules):
"""
Generates lines to add to a cmake file which will set variables
:param version: str, format 'int.int.int'
:param scripts: [list of str]: relative paths to scripts
:param package_dir: {modulename: path}
:pkgs: [list of str] python_packages declared in catkin package
:modules: [list of str] python modules
"""
prefix = '%s_SETUP_PY' % package_name
result = []
Expand Down Expand Up @@ -129,6 +130,21 @@ def generate_cmake_file(package_name, version, scripts, package_dir, pkgs):

result.append(r'set(%s_PACKAGES "%s")' % (prefix, ';'.join(pkgs)))
result.append(r'set(%s_PACKAGE_DIRS "%s")' % (prefix, ';'.join(resolved_pkgs).replace("\\", "/")))

# skip modules which collide with package names
filtered_modules = []
for modname in modules:
splits = modname.split('.')
# check all parents too
equals_package = [('.'.join(splits[:-i]) in locations) for i in range(len(splits))]
if any(equals_package):
continue
filtered_modules.append(modname)
module_locations = _get_locations(filtered_modules, package_dir)

result.append(r'set(%s_MODULES "%s")' % (prefix, ';'.join(['%s.py' % m.replace('.', '/') for m in filtered_modules])))
result.append(r'set(%s_MODULE_DIRS "%s")' % (prefix, ';'.join([module_locations[m] for m in filtered_modules]).replace("\\", "/")))

return result


Expand All @@ -155,6 +171,7 @@ def setup(*args, **kwargs):

pkgs = kwargs.get('packages', [])
scripts = kwargs.get('scripts', [])
modules = kwargs.get('py_modules', [])

unsupported_args = [
'entry_points',
Expand All @@ -163,7 +180,6 @@ def setup(*args, **kwargs):
'ext_package',
'include_package_data',
'namespace_packages',
'py_modules',
'setup_requires',
'use_2to3',
'zip_safe']
Expand All @@ -175,7 +191,8 @@ def setup(*args, **kwargs):
version=version,
scripts=scripts,
package_dir=package_dir,
pkgs=pkgs)
pkgs=pkgs,
modules=modules)
with open(outfile, 'w') as out:
out.write('\n'.join(result))

Expand Down
4 changes: 4 additions & 0 deletions cmake/templates/relay.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# creates a relay to a python script source file, acting as that file.
# The purpose is that of a symlink
with open("@PYTHON_SCRIPT@", 'r') as fh:
exec(fh.read())

0 comments on commit c468981

Please sign in to comment.