Python Usage
Overview
The CLDPM Python SDK can be used both as a CLI tool and as a library in your Python code.
Programmatic Usage
Loading Configuration
from pathlib import Path
from cldpm.core.config import load_cldpm_config, load_project_config
# Load mono repo configuration
repo_root = Path("/path/to/monorepo")
cldpm_config = load_cldpm_config(repo_root)
print(cldpm_config.name) # "my-monorepo"
print(cldpm_config.projects_dir) # "projects"
print(cldpm_config.shared_dir) # "shared"
# Load project configuration
project_path = repo_root / "projects" / "my-project"
project_config = load_project_config(project_path)
print(project_config.name) # "my-project"
print(project_config.dependencies.skills) # ["code-review"]Resolving Projects
from cldpm.core.resolver import resolve_project
# Resolve by name
result = resolve_project("my-project", repo_root)
# Or by path
result = resolve_project("projects/my-project", repo_root)
# Result structure
print(result["name"]) # "my-project"
print(result["path"]) # "/path/to/projects/my-project"
print(result["shared"]) # Shared components
print(result["local"]) # Local components
# Iterate shared skills
for skill in result["shared"]["skills"]:
print(f"Shared: {skill['name']}")
print(f" Files: {skill['files']}")
# Iterate local skills
for skill in result["local"]["skills"]:
print(f"Local: {skill['name']}")Managing Symlinks
from cldpm.core.linker import (
sync_project_links,
add_component_link,
remove_project_links,
)
# Sync all symlinks for a project
result = sync_project_links(project_path, repo_root)
print(f"Created: {result['created']}")
print(f"Missing: {result['missing']}")
print(f"Failed: {result['failed']}")
# Add a single component link
success = add_component_link(
project_path,
"skills", # Component type
"code-review", # Component name
repo_root
)
# Remove all symlinks
remove_project_links(project_path)Working with Component Dependencies
from cldpm.core.config import load_component_metadata
from cldpm.core.resolver import get_all_dependencies_for_component
# Load component metadata
metadata = load_component_metadata("agents", "security-audit", repo_root)
if metadata:
print(f"Component: {metadata.name}")
print(f"Description: {metadata.description}")
print(f"Skill deps: {metadata.dependencies.skills}")
print(f"Hook deps: {metadata.dependencies.hooks}")
# Get all dependencies (including transitive)
all_deps = get_all_dependencies_for_component("agents", "security-audit", repo_root)
print(f"All skill deps: {all_deps['skills']}")
print(f"All hook deps: {all_deps['hooks']}")File System Utilities
from cldpm.utils.fs import find_repo_root, ensure_dir
# Find mono repo root from current directory
repo_root = find_repo_root()
if repo_root is None:
print("Not in a CLDPM mono repo")
# Ensure directory exists
ensure_dir(Path("/path/to/new/directory"))Git Utilities
from cldpm.utils.git import (
get_github_token,
parse_repo_url,
clone_to_temp,
cleanup_temp_dir,
)
# Get GitHub token from environment
token = get_github_token()
# Parse repository URL
repo_url, subpath, branch = parse_repo_url("owner/repo/tree/main")
print(f"URL: {repo_url}") # "https://github.com/owner/repo.git"
print(f"Branch: {branch}") # "main"
# Clone to temporary directory
temp_dir = clone_to_temp(repo_url, branch, token)
try:
# Work with cloned repo
pass
finally:
cleanup_temp_dir(temp_dir)CLI Integration
Invoking CLI from Python
from click.testing import CliRunner
from cldpm.cli import cli
runner = CliRunner()
# Run commands
result = runner.invoke(cli, ["init", "my-repo"])
print(result.output)
print(result.exit_code)
# Run with isolated filesystem
with runner.isolated_filesystem():
runner.invoke(cli, ["init"])
runner.invoke(cli, ["create", "project", "my-app"])
result = runner.invoke(cli, ["get", "my-app", "-f", "json"])
print(result.output)Custom CLI Commands
import click
from cldpm.cli import cli
@cli.command()
@click.argument("name")
def custom_command(name):
"""My custom CLDPM command."""
click.echo(f"Hello, {name}!")
# Now available as: cldpm custom-command <name>Type Hints
The SDK provides full type hints via Pydantic models:
from cldpm.schemas import CldpmConfig, ProjectConfig, ProjectDependencies
# Create typed configuration
deps = ProjectDependencies(
skills=["code-review"],
agents=["debugger"],
hooks=[],
rules=[]
)
config = ProjectConfig(
name="my-project",
description="My project",
dependencies=deps
)
# Serialize to dict/JSON
config_dict = config.model_dump()
config_json = config.model_dump_json(indent=2)