Skip to content

Commit

Permalink
feat: demonstrate python run_binary/macro
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Dec 29, 2023
1 parent a851376 commit 6053cb3
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 4 deletions.
13 changes: 12 additions & 1 deletion cli/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
load("@rules_python//python:defs.bzl", "py_binary")
load("@pip//:requirements.bzl", "requirement")
load("//tools:py_header.bzl", "myorg_py_header")

myorg_py_header(
name = "fill_template",
out = "header.txt",
tmpl = "header.txt.tmpl",
)

py_binary(
name = "cli",
srcs = ["__main__.py"],
main = "__main__.py",
deps = [requirement("requests")],
deps = [
requirement("requests"),
requirement("bazel-runfiles"),
],
data = ["header.txt"],
)
3 changes: 3 additions & 0 deletions cli/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import requests
import runfiles

with open(runfiles.Create().Rlocation("_main/cli/header.txt"), "r") as f:
print(f.read())
r = requests.get('http://localhost:8081')
print(r.json())
4 changes: 4 additions & 0 deletions cli/header.txt.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Thanks for running our <codename> CLI.

Note, you will need to start the server first.
Built at {{timestamp}}!
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
requests
Jinja2
jinja2-cli
bazel-runfiles
81 changes: 78 additions & 3 deletions requirements_lock.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")

package(default_visibility = ["//visibility:public"])
py_console_script_binary(
name = "jinja2",
pkg = "@pip//jinja2_cli:pkg",
)

load("@aspect_bazel_lib//lib:jq.bzl", "jq")

jq(
name = "data",
srcs = [],
filter = "|".join([
# Don't directly reference $STAMP as it's only set when stamping
# This 'as' syntax results in $stamp being null in unstamped builds.
"$ARGS.named.STAMP as $stamp",
# Provide a default using the "alternative operator" in case $stamp is null.
""".timestamp = ($stamp.BUILD_TIMESTAMP // "<unstamped>")""",
]),
)
16 changes: 16 additions & 0 deletions tools/py_header.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"TODO"
load("@aspect_bazel_lib//lib:run_binary.bzl", "run_binary")

def myorg_py_header(name, tmpl, out, **kwargs):
run_binary(
name = name,
outs = [out],
srcs = [
tmpl,
"//tools:data.json",
],
# The tool to run in the action
tool = "//tools:jinja2",
args = ["--format=json", "-o", "$(execpath %s)" % out, "$(execpath %s)" % tmpl, "$(execpath //tools:data.json)"],
**kwargs,
)

0 comments on commit 6053cb3

Please sign in to comment.