colcon-metadata

0.2.5 · active · verified Fri Apr 17

colcon-metadata is an extension for the `colcon` build tool, providing the ability to read package metadata from various file formats beyond `package.xml`. It helps `colcon` discover and process packages in a more flexible way, especially useful for mixed workspaces. It is currently at version 0.2.5 and has an infrequent release cadence, often aligning with `colcon-core` updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates `colcon-metadata` by creating a temporary `colcon` workspace with a Python package defined solely by a `colcon.pkg` file. It then attempts to run `colcon build` to show how `colcon`, with the `colcon-metadata` extension, can discover and process packages based on this metadata file. Ensure `colcon-core` is installed in your environment for the `colcon` command to be available.

import os
import subprocess
import shutil

# Define workspace and package paths
workspace_dir = "temp_colcon_metadata_workspace"
package_dir = os.path.join(workspace_dir, "my_python_package")
colcon_pkg_path = os.path.join(package_dir, "colcon.pkg")

# Clean up previous runs if any
if os.path.exists(workspace_dir):
    shutil.rmtree(workspace_dir)

# Create directory structure
os.makedirs(package_dir, exist_ok=True)

# Create a dummy colcon.pkg file for metadata
colcon_pkg_content = """
type: python
name: my_python_package
version: 0.1.0
maintainers:
  - Example User <user@example.com>
"""

with open(colcon_pkg_path, "w") as f:
    f.write(colcon_pkg_content)

# Optional: Create a minimal setup.py and __init__.py for a buildable Python package
setup_py_content = """
from setuptools import setup

setup(
    name='my_python_package',
    version='0.1.0',
    packages=['my_python_package'],
    install_requires=[],
)
"""

os.makedirs(os.path.join(package_dir, "my_python_package"), exist_ok=True)
with open(os.path.join(package_dir, "setup.py"), "w") as f:
    f.write(setup_py_content)
with open(os.path.join(package_dir, "my_python_package", "__init__.py"), "w") as f:
    f.write("")

print(f"Created dummy package at: {package_dir}")
print(f"Created colcon.pkg at: {colcon_pkg_path}")

# Attempt to run 'colcon build' to demonstrate discovery
print("\nAttempting to run 'colcon build' in the workspace...")
try:
    # Check if colcon is installed
    subprocess.run(["colcon", "--version"], check=True, capture_output=True)
    
    # Run colcon build for the specific package
    result = subprocess.run(
        ["colcon", "build", "--packages-select", "my_python_package"],
        cwd=workspace_dir,
        check=True,
        capture_output=True,
        text=True
    )
    print("colcon build successful! Output:\n")
    print(result.stdout)
    if result.stderr:
        print("colcon build stderr:\n", result.stderr)
except FileNotFoundError:
    print("Error: 'colcon' command not found. Please install colcon-core: pip install colcon-core")
except subprocess.CalledProcessError as e:
    print(f"colcon build failed: {e}")
    print("stdout:\n", e.stdout)
    print("stderr:\n", e.stderr)
finally:
    # Clean up the workspace
    if os.path.exists(workspace_dir):
        print(f"\nCleaning up {workspace_dir}")
        shutil.rmtree(workspace_dir)

view raw JSON →