databuild/examples/podcast_reviews/py_repl.bzl

48 lines
1.3 KiB
Python

"""Rule for creating Python REPL environments with dependencies."""
load("@rules_python//python:defs.bzl", "py_binary")
def py_repl(name, deps = [], **kwargs):
"""Creates a Python REPL with the specified dependencies.
Args:
name: Name of the resulting REPL target.
deps: List of Python targets whose dependencies should be available in the REPL.
**kwargs: Additional arguments to pass to the py_binary.
"""
# Create the script that will be our REPL entry point
script_name = name + "_script.py"
native.genrule(
name = name + "_script_gen",
outs = [script_name],
cmd = """
cat > $@ << 'EOF'
#!/usr/bin/env python3
'''
Python REPL with preloaded dependencies from Bazel.
This script sets up the Python path and launches an interactive Python session.
'''
import os
import sys
import code
print("Python REPL initialized with project dependencies")
print("Type 'exit()' or press Ctrl+D to exit")
print("")
# Launch interactive Python shell
code.interact(banner="", local=locals())
EOF
""",
)
# Wrap in a py_binary to handle runfiles properly
py_binary(
name = name,
srcs = [script_name],
main = script_name,
python_version = "PY3",
deps = deps,
**kwargs
)