diff --git a/examples/bzlmod/container/.bazelrc b/examples/bzlmod/container/.bazelrc new file mode 100644 index 0000000000..1db7509656 --- /dev/null +++ b/examples/bzlmod/container/.bazelrc @@ -0,0 +1,26 @@ +common --@rules_python//python/config_settings:bootstrap_impl=script +common --@rules_python//python/config_settings:venvs_site_packages=no +# See https://github.com/bazel-contrib/rules_python/issues/2864#issuecomment-2859325467 +common --@rules_python//python/config_settings:venvs_use_declare_symlink=yes + + +# With container +# | vens_site_packages | venvs_use_declare_symlink | result | +# |--------------------|-----------------------------|---------| +# | yes | no | failure | +# | no | no | success | +# | no | yes | failure | +# | yes | yes | failure | +# | -----------------------------------------------------------| +# | all commented out | success | + + +# Without container +# | vens_site_packages | venvs_use_declare_symlink | result | +# |--------------------|-----------------------------|---------| +# | yes | no | failure | +# | no | no | success | +# | no | yes | success | +# | yes | yes | failure | +# | -----------------------------------------------------------| +# | all commented out | success | diff --git a/examples/bzlmod/container/.bazelversion b/examples/bzlmod/container/.bazelversion new file mode 100644 index 0000000000..2b0aa21219 --- /dev/null +++ b/examples/bzlmod/container/.bazelversion @@ -0,0 +1 @@ +8.2.1 diff --git a/examples/bzlmod/container/BUILD.bazel b/examples/bzlmod/container/BUILD.bazel new file mode 100644 index 0000000000..7af9758ae1 --- /dev/null +++ b/examples/bzlmod/container/BUILD.bazel @@ -0,0 +1,37 @@ +load("@aspect_rules_py//py:defs.bzl", "py_image_layer") +load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load") +load("@rules_python//python:defs.bzl", "py_binary") + +licenses(["notice"]) + +py_binary( + name = "server", + srcs = ["server.py"], + deps = [ + "@pypi//absl_py", + "@pypi//flask", + ], +) + +oci_image( + name = "server_image", + base = "@docker_io_python", + entrypoint = ["/app/server"], + tars = py_image_layer( + name = "server_layer", + binary = ":server", + root = "/app/", + ), +) + +oci_load( + name = "server_image.load", + image = ":server_image", + repo_tags = ["server:latest"], +) + +filegroup( + name = "server_image.tar", + srcs = [":server_image.load"], + output_group = "tarball", +) diff --git a/examples/bzlmod/container/MODULE.bazel b/examples/bzlmod/container/MODULE.bazel new file mode 100644 index 0000000000..6086852236 --- /dev/null +++ b/examples/bzlmod/container/MODULE.bazel @@ -0,0 +1,41 @@ +"""Example of python inside container.""" + +module( + name = "container-example", + version = "0.0.1", +) + +bazel_dep(name = "rules_python", version = "1.4.1") +bazel_dep(name = "rules_oci", version = "2.2.6") +bazel_dep(name = "platforms", version = "0.0.11") +bazel_dep(name = "aspect_rules_py", version = "1.6.0") +bazel_dep(name = "rules_pkg", version = "1.1.0") + +python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.toolchain( + is_default = True, + python_version = "3.12", +) +python.override(minor_mapping = {"3.12": "3.12.7"}) + +pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") +pip.parse( + hub_name = "pypi", + python_version = "3.12", + requirements_lock = "requirements.txt", +) + +use_repo(pip, "pypi") + +oci = use_extension("@rules_oci//oci:extensions.bzl", "oci") +oci.pull( + name = "docker_io_python", + digest = "sha256:034724ef64585eeb0e82385e9aabcbeabfe5f7cae2c2dcedb1da95114372b6d7", + image = "docker.io/library/python", + platforms = [ + "linux/amd64", + "linux/arm64/v8", + ], + tag = "3.13-slim", +) +use_repo(oci, "docker_io_python", "docker_io_python_linux_amd64", "docker_io_python_linux_arm64_v8") diff --git a/examples/bzlmod/container/requirements.in b/examples/bzlmod/container/requirements.in new file mode 100644 index 0000000000..31546f67ea --- /dev/null +++ b/examples/bzlmod/container/requirements.in @@ -0,0 +1,3 @@ +absl-py +flask +redis diff --git a/examples/bzlmod/container/requirements.txt b/examples/bzlmod/container/requirements.txt new file mode 100644 index 0000000000..7a55a366d2 --- /dev/null +++ b/examples/bzlmod/container/requirements.txt @@ -0,0 +1,23 @@ +# This file was autogenerated by uv via the following command: +# uv pip compile ./requirements.in -o ./requirements.txt +absl-py==2.3.1 + # via -r ./requirements.in +blinker==1.9.0 + # via flask +click==8.2.1 + # via flask +flask==3.1.1 + # via -r ./requirements.in +itsdangerous==2.2.0 + # via flask +jinja2==3.1.6 + # via flask +markupsafe==3.0.2 + # via + # flask + # jinja2 + # werkzeug +redis==6.2.0 + # via -r ./requirements.in +werkzeug==3.1.3 + # via flask diff --git a/examples/bzlmod/container/server.py b/examples/bzlmod/container/server.py new file mode 100644 index 0000000000..ffbd4d5597 --- /dev/null +++ b/examples/bzlmod/container/server.py @@ -0,0 +1,39 @@ +"""A HTTP server example.""" + +from collections.abc import Sequence +import os +import signal +import time + +from absl import app +from absl import flags +from flask import Flask + +server = Flask(__name__) + + +@server.route("/") +def index() -> str: + return f"Hello, World! This is Flask." + + +def main(argv: Sequence[str]) -> None: + del argv + + def handler(signum, frame): + # Raising an exception is a clean way to break out of server.run() + raise SystemExit("Server timed out, shutting down.") + + # Set the alarm signal handler + signal.signal(signal.SIGALRM, handler) + # Trigger the alarm after 5 seconds + signal.alarm(2) + + try: + server.run(host="0.0.0.0", port=8080, debug=False) + except SystemExit as e: + print(e) + + +if __name__ == "__main__": + app.run(main)
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: