|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build script for generating Python protobuf code.""" |
| 3 | + |
| 4 | +import subprocess |
| 5 | +import shutil |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +# Get the proton root directory |
| 11 | +ROOT_DIR = Path(__file__).parent.parent.parent |
| 12 | +PYTHON_DIR = Path(__file__).parent.parent |
| 13 | +DIST_DIR = PYTHON_DIR / "dist" |
| 14 | + |
| 15 | + |
| 16 | +def clean(): |
| 17 | + """Remove the dist directory.""" |
| 18 | + if DIST_DIR.exists(): |
| 19 | + print(f"Cleaning {DIST_DIR}") |
| 20 | + shutil.rmtree(DIST_DIR) |
| 21 | + |
| 22 | + |
| 23 | +def publish(): |
| 24 | + """Build wheel for publishing.""" |
| 25 | + if not DIST_DIR.exists(): |
| 26 | + print("Error: dist directory not found. Run 'build' first.", file=sys.stderr) |
| 27 | + sys.exit(1) |
| 28 | + |
| 29 | + # Check if build package is installed |
| 30 | + check_build = subprocess.run( |
| 31 | + [sys.executable, "-m", "build", "--version"], capture_output=True |
| 32 | + ) |
| 33 | + if check_build.returncode != 0: |
| 34 | + print("Error: 'build' package not installed. Install it with:") |
| 35 | + print(" pip install build") |
| 36 | + sys.exit(1) |
| 37 | + |
| 38 | + print("Building wheel...") |
| 39 | + result = subprocess.run([sys.executable, "-m", "build", "--wheel"], cwd=DIST_DIR) |
| 40 | + |
| 41 | + if result.returncode != 0: |
| 42 | + print("Wheel build failed!", file=sys.stderr) |
| 43 | + sys.exit(1) |
| 44 | + |
| 45 | + print("Wheel built successfully!") |
| 46 | + print(f"Output: {DIST_DIR}/dist/") |
| 47 | + |
| 48 | + |
| 49 | +def build(): |
| 50 | + """Generate Python code from proto files.""" |
| 51 | + clean() |
| 52 | + |
| 53 | + cmd = [ |
| 54 | + "buf", |
| 55 | + "generate", |
| 56 | + "--template", |
| 57 | + "python/scripts/buf.gen.yaml", |
| 58 | + "--include-imports", |
| 59 | + "--path", |
| 60 | + "raystack", |
| 61 | + ".", |
| 62 | + ] |
| 63 | + |
| 64 | + print(f"Running: {' '.join(cmd)}") |
| 65 | + result = subprocess.run(cmd, cwd=ROOT_DIR) |
| 66 | + |
| 67 | + if result.returncode != 0: |
| 68 | + print("Build failed!", file=sys.stderr) |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + # Create __init__.py files in all directories |
| 72 | + if DIST_DIR.exists(): |
| 73 | + print("Creating __init__.py files...") |
| 74 | + for dir_path in DIST_DIR.rglob("*"): |
| 75 | + if dir_path.is_dir(): |
| 76 | + init_file = dir_path / "__init__.py" |
| 77 | + if not init_file.exists(): |
| 78 | + init_file.touch() |
| 79 | + |
| 80 | + # Copy pyproject.toml template to dist |
| 81 | + template_file = Path(__file__).parent / "pyproject.template.toml" |
| 82 | + if template_file.exists(): |
| 83 | + print("Copying pyproject.toml to dist...") |
| 84 | + content = template_file.read_text() |
| 85 | + |
| 86 | + # Generate CalVer format: YYYY.MM.DD.HHMMSS |
| 87 | + from datetime import datetime |
| 88 | + |
| 89 | + now = datetime.utcnow() |
| 90 | + version_str = now.strftime("%Y.%m.%d.%H%M%S") |
| 91 | + content = content.replace('version = "0.1.0"', f'version = "{version_str}"') |
| 92 | + print(f"Set version to: {version_str}") |
| 93 | + (DIST_DIR / "pyproject.toml").write_text(content) |
| 94 | + |
| 95 | + # Copy README to dist |
| 96 | + readme_file = PYTHON_DIR / "README.md" |
| 97 | + if readme_file.exists(): |
| 98 | + print("Copying README.md to dist...") |
| 99 | + shutil.copy(readme_file, DIST_DIR / "README.md") |
| 100 | + |
| 101 | + # Copy LICENSE to dist |
| 102 | + license_file = ROOT_DIR / "LICENSE" |
| 103 | + if license_file.exists(): |
| 104 | + print("Copying LICENSE to dist...") |
| 105 | + shutil.copy(license_file, DIST_DIR / "LICENSE") |
| 106 | + |
| 107 | + print("Build successful!") |
| 108 | + |
| 109 | + |
| 110 | +def build_cli(): |
| 111 | + """CLI entry point for build command.""" |
| 112 | + build() |
| 113 | + |
| 114 | + |
| 115 | +def main(): |
| 116 | + import argparse |
| 117 | + |
| 118 | + parser = argparse.ArgumentParser(description="Build Python protobuf package") |
| 119 | + parser.add_argument( |
| 120 | + "command", choices=["clean", "build", "publish"], help="Command to run" |
| 121 | + ) |
| 122 | + |
| 123 | + args = parser.parse_args() |
| 124 | + |
| 125 | + if args.command == "clean": |
| 126 | + clean() |
| 127 | + elif args.command == "build": |
| 128 | + build() |
| 129 | + elif args.command == "publish": |
| 130 | + publish() |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + main() |
0 commit comments