xref: /aosp_15_r20/external/bazelbuild-rules_python/examples/build_file_generation/BUILD.bazel (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Load various rules so that we can have bazel download
2# various rulesets and dependencies.
3# The `load` statement imports the symbol for the rule, in the defined
4# ruleset. When the symbol is loaded you can use the rule.
5load("@bazel_gazelle//:def.bzl", "gazelle")
6load("@pip//:requirements.bzl", "all_whl_requirements")
7load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
8load("@rules_python//python:pip.bzl", "compile_pip_requirements")
9load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
10load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
11
12compile_pip_requirements(
13    name = "requirements",
14    src = "requirements.in",
15    requirements_txt = "requirements_lock.txt",
16    requirements_windows = "requirements_windows.txt",
17)
18
19# This repository rule fetches the metadata for python packages we
20# depend on. That data is required for the gazelle_python_manifest
21# rule to update our manifest file.
22# To see what this rule does, try `bazel run @modules_map//:print`
23modules_mapping(
24    name = "modules_map",
25    exclude_patterns = [
26        "^_|(\\._)+",  # This is the default.
27        "(\\.tests)+",  # Add a custom one to get rid of the psutil tests.
28    ],
29    wheels = all_whl_requirements,
30)
31
32# Gazelle python extension needs a manifest file mapping from
33# an import to the installed package that provides it.
34# This macro produces two targets:
35# - //:gazelle_python_manifest.update can be used with `bazel run`
36#   to recalculate the manifest
37# - //:gazelle_python_manifest.test is a test target ensuring that
38#   the manifest doesn't need to be updated
39gazelle_python_manifest(
40    name = "gazelle_python_manifest",
41    modules_mapping = ":modules_map",
42    pip_repository_name = "pip",
43    # NOTE: We can pass a list just like in `bzlmod_build_file_generation` example
44    # but we keep a single target here for regression testing.
45    requirements = "//:requirements_lock.txt",
46)
47
48# Our gazelle target points to the python gazelle binary.
49# This is the simple case where we only need one language supported.
50# If you also had proto, go, or other gazelle-supported languages,
51# you would also need a gazelle_binary rule.
52# See https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.rst#example
53gazelle(
54    name = "gazelle",
55    gazelle = "@rules_python_gazelle_plugin//python:gazelle_binary",
56)
57
58# This rule is auto-generated and managed by Gazelle,
59# because it found the __init__.py file in this folder.
60# See: https://bazel.build/reference/be/python#py_library
61py_library(
62    name = "build_file_generation",
63    srcs = ["__init__.py"],
64    visibility = ["//:__subpackages__"],
65    deps = [
66        "//random_number_generator",
67        "@pip//flask",
68        "@pip//sphinx",
69    ],
70)
71
72# A py_binary is an executable Python program consisting of a collection of .py source files.
73# See: https://bazel.build/reference/be/python#py_binary
74#
75# This rule is auto-generated and managed by Gazelle,
76# because it found the __main__.py file in this folder.
77# This rule creates a target named //:build_file_generation_bin and you can use
78# bazel to run the target:
79# `bazel run //:build_file_generation_bin`
80py_binary(
81    name = "build_file_generation_bin",
82    srcs = ["__main__.py"],
83    main = "__main__.py",
84    visibility = ["//:__subpackages__"],
85    deps = [":build_file_generation"],
86)
87
88# A py_test is a Python unit test.
89# See: https://bazel.build/reference/be/python#py_test
90#
91# This rule is auto-generated and managed by Gazelle,
92# because it found the __test__.py file in this folder.
93# This rule creates a target named //:build_file_generation_test and you can use
94# bazel to run the target:
95# `bazel test //:build_file_generation_test`
96py_test(
97    name = "build_file_generation_test",
98    srcs = ["__test__.py"],
99    main = "__test__.py",
100    deps = [":build_file_generation"],
101)
102