colcon-core

0.20.1 · active · verified Thu Apr 16

colcon-core is the foundational Python package for 'colcon', a command-line tool designed to streamline the workflow of building, testing, and using multiple software packages, especially within robotics frameworks like ROS. It automates package discovery, dependency resolution, and environment setup. The library is currently at version 0.20.1 and follows a continuous release model, with new versions typically released as needed to support updates in related projects like ROS.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a minimal colcon workspace with a Python package, build it, and then prepare to run an executable defined within it. This example implicitly uses 'ament_python' build type, common in ROS environments. It creates the necessary directory structure and `setup.py`/`package.xml` files for `colcon` to discover and build the package.

import os

# Create a dummy workspace and a simple Python package
os.makedirs('my_workspace/src/my_python_package', exist_ok=True)

with open('my_workspace/src/my_python_package/setup.py', 'w') as f:
    f.write("""
from setuptools import setup

package_name = 'my_python_package'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages', ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml'])
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='Your Name',
    maintainer_email='you@example.com',
    description='A minimal Python package for colcon',
    license='Apache-2.0',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'my_node = my_python_package.main:main'
        ],
    },
)
""")

with open('my_workspace/src/my_python_package/package.xml', 'w') as f:
    f.write("""
<?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>my_python_package</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="you@example.com">Your Name</maintainer>
  <license>Apache-2.0</license>

  <test_depend>ament_copyright</test_depend>
  <test_depend>ament_flake8</test_depend>
  <test_depend>ament_pep257</test_depend>
  <test_depend>python3-pytest</test_depend>

  <export>
    <build_type>ament_python</build_type>
  </export>
</package>
""")

with open('my_workspace/src/my_python_package/my_python_package/main.py', 'w') as f:
    f.write("""
def main():
    print('Hello from my_python_package!')

if __name__ == '__main__':
    main()
""")

os.makedirs('my_workspace/src/my_python_package/resource', exist_ok=True)
with open('my_workspace/src/my_python_package/resource/my_python_package', 'w') as f:
    f.write('')

# Now, build the workspace using colcon
print('\n--- Running colcon build ---')
os.system('cd my_workspace && colcon build --packages-select my_python_package')

# Source the setup file and run the node
print('\n--- Sourcing setup and running node ---')
# For simplicity, we'll try to run directly if possible, or instruct sourcing.
# In a real shell, you would run: . install/setup.bash (or .ps1, .bat)
# For a self-contained python script, it's complex to source a shell file.
# Instead, we will try to invoke the installed script directly if available
node_path = 'my_workspace/install/my_python_package/lib/my_python_package/my_node'
if os.path.exists(node_path):
    os.system(node_path)
else:
    print("To run the node: cd my_workspace && . install/setup.bash && my_node")

view raw JSON →