forked from bazelbuild/codelabs
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: demonstrate python run_binary/macro
- Loading branch information
Showing
7 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
requests | ||
Jinja2 | ||
jinja2-cli | ||
bazel-runfiles |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>")""", | ||
]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |