from pathlib import Path
from typing import Tuple
from typing import Set


def packages_find(workspace_path: Path, tracing: str = "") -> Tuple[Path, ...]:
    """Find the various packages within a ROS2 workspace."""
    # next_tracing: str = tracing + " " if tracing else ""
    if tracing:
        print(f"{tracing}=>packages_find({workspace_path})")
    src_path: Path = workspace_path / "src"
    assert src_path.is_dir(), f"{src_path} is not a directory"

    all_packages: Set[Path] = set()
    package_path: Path
    for package_path in src_path.glob("*/package.xml"):
        all_packages.add(package_path.parent)
    for package_path in src_path.glob("*/CMakeLists.txt"):
        all_packages.add(package_path.parent)
    final_packages: Tuple[Path, ...] = tuple(sorted(all_packages))

    if tracing:
        print(f"{tracing}<=packages_find({workspace_path})=>{final_packages}")
    return tuple(final_packages)