databuild/databuild/dsl/python/generator_lib.py
Stuart Axelbrooke ba18734190
Some checks failed
/ setup (push) Has been cancelled
Make dsl generation work for submodules
2025-08-06 22:16:01 -07:00

38 lines
No EOL
1.4 KiB
Python

#!/usr/bin/env python3
"""
Core DSL code generation library that can be imported by different generator binaries.
"""
import os
import importlib
def generate_dsl_package(module_path: str, graph_attr: str, output_dir: str, deps: list = None):
"""
Generate DataBuild DSL package from a graph definition.
Args:
module_path: Python module path (e.g., "databuild.test.app.dsl.graph")
graph_attr: Name of the graph attribute in the module
output_dir: Directory where to generate the DSL package
deps: List of Bazel dependency labels to use in generated BUILD.bazel
"""
# Extract the base name from the output directory for naming
name = os.path.basename(output_dir.rstrip('/')) or "graph"
try:
# Import the graph module
module = importlib.import_module(module_path)
graph = getattr(module, graph_attr)
# Generate the bazel package
graph.generate_bazel_package(name, output_dir, deps or [])
print(f"Generated DataBuild DSL package in {output_dir}")
except ImportError as e:
raise ImportError(f"Failed to import {graph_attr} from {module_path}: {e}")
except AttributeError as e:
raise AttributeError(f"Module {module_path} does not have attribute {graph_attr}: {e}")
except Exception as e:
raise Exception(f"Generation failed: {e}")