#!/usr/bin/env python3
"""
Build script for Chatlog MCP Server
This script builds and packages the Chatlog MCP Server for distribution
"""
import os
import sys
import subprocess
import shutil
from pathlib import Path
def run_command(cmd, cwd=None):
"""Run a shell command and return the result"""
print(f"Running: {cmd}")
result = subprocess.run(cmd, shell=True, cwd=cwd, capture_output=True, text=True)
if result.returncode != 0:
print(f"Error: {result.stderr}")
sys.exit(1)
return result.stdout
def clean_build():
"""Clean build artifacts"""
print("\n🧹 Cleaning build artifacts...")
dirs_to_clean = ['build', 'dist', '*.egg-info']
for dir_pattern in dirs_to_clean:
for path in Path('.').glob(dir_pattern):
if path.is_dir():
shutil.rmtree(path)
print(f" Removed: {path}")
else:
path.unlink()
print(f" Removed: {path}")
def run_tests():
"""Run tests"""
print("\n🧪 Running tests...")
result = run_command("python -m pytest chatlog_mcp/tests/ -v")
print(result)
def build_package():
"""Build the package"""
print("\n📦 Building package...")
run_command("python setup.py sdist bdist_wheel")
def create_release():
"""Create release files"""
print("\n📁 Creating release...")
release_dir = Path("release")
release_dir.mkdir(exist_ok=True)
# Copy release files
files_to_copy = [
("dist/*.whl", "wheel/"),
("dist/*.tar.gz", "source/"),
("README.md", "."),
("LICENSE", "."),
("install.sh", "."),
("install.bat", "."),
("chatlog_mcp/examples/mcp-servers.json", "examples/"),
]
for src_pattern, dest_dir in files_to_copy:
dest_path = release_dir / dest_dir
dest_path.mkdir(parents=True, exist_ok=True)
for src_file in Path('.').glob(src_pattern):
shutil.copy2(src_file, dest_path / src_file.name)
print(f" Copied: {src_file} -> {dest_path / src_file.name}")
def create_docker_image():
"""Create Docker image (optional)"""
print("\n🐳 Creating Docker image...")
dockerfile = """FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN pip install -e .
CMD ["chatlog-mcp", "--help"]
"""
with open("Dockerfile", "w") as f:
f.write(dockerfile)
print(" Dockerfile created")
print(" To build: docker build -t chatlog-mcp-server .")
print(" To run: docker run -it chatlog-mcp-server")
def print_instructions():
"""Print release instructions"""
print("\n" + "="*60)
print("Release Instructions")
print("="*60)
print("\n📦 Package created successfully!")
print("\nTo install from source:")
print(" pip install dist/*.whl")
print("\nTo upload to PyPI:")
print(" pip install twine")
print(" twine upload dist/*")
print("\nFiles created:")
print(" - dist/chatlog_mcp_server-1.0.0-py3-none-any.whl")
print(" - dist/chatlog_mcp_server-1.0.0.tar.gz")
print("\nRelease directory: ./release/")
def main():
"""Main build process"""
print("="*60)
print("Chatlog MCP Server - Build Script")
print("="*60)
# Parse arguments
if "--clean" in sys.argv:
clean_build()
sys.exit(0)
if "--tests" in sys.argv:
run_tests()
sys.exit(0)
if "--docker" in sys.argv:
create_docker_image()
sys.exit(0)
# Full build process
clean_build()
run_tests()
build_package()
create_release()
print_instructions()
print("\n✅ Build completed successfully!")
if __name__ == "__main__":
main()