• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
5
6# The following code loads the base python requirements and gazelle
7# requirements.
8load("@bazel_gazelle//:def.bzl", "gazelle")
9load("@pip//:requirements.bzl", "all_whl_requirements")
10load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
11load("@rules_python//python:pip.bzl", "compile_pip_requirements")
12load("@rules_python_gazelle_plugin//:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
13load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
14load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
15
16# This stanza calls a rule that generates targets for managing pip dependencies
17# with pip-compile.
18compile_pip_requirements(
19    name = "requirements",
20    extra_args = ["--allow-unsafe"],
21    requirements_in = "requirements.in",
22    requirements_txt = "requirements_lock.txt",
23    requirements_windows = "requirements_windows.txt",
24)
25
26# This repository rule fetches the metadata for python packages we
27# depend on. That data is required for the gazelle_python_manifest
28# rule to update our manifest file.
29modules_mapping(
30    name = "modules_map",
31    exclude_patterns = [
32        "^_|(\\._)+",  # This is the default.
33        "(\\.tests)+",  # Add a custom one to get rid of the psutil tests.
34    ],
35    wheels = all_whl_requirements,
36)
37
38# Gazelle python extension needs a manifest file mapping from
39# an import to the installed package that provides it.
40# This macro produces two targets:
41# - //:gazelle_python_manifest.update can be used with `bazel run`
42#   to recalculate the manifest
43# - //:gazelle_python_manifest.test is a test target ensuring that
44#   the manifest doesn't need to be updated
45# This target updates a file called gazelle_python.yaml, and
46# requires that file exist before the target is run.
47# When you are using gazelle you need to run this target first.
48gazelle_python_manifest(
49    name = "gazelle_python_manifest",
50    modules_mapping = ":modules_map",
51    pip_repository_name = "pip",
52    requirements = [
53        "//:requirements_lock.txt",
54        "//:requirements_windows.txt",
55    ],
56    # NOTE: we can use this flag in order to make our setup compatible with
57    # bzlmod.
58    use_pip_repository_aliases = True,
59)
60
61# Our gazelle target points to the python gazelle binary.
62# This is the simple case where we only need one language supported.
63# If you also had proto, go, or other gazelle-supported languages,
64# you would also need a gazelle_binary rule.
65# See https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.rst#example
66# This is the primary gazelle target to run, so that you can update BUILD.bazel files.
67# You can execute:
68# - bazel run //:gazelle update
69# - bazel run //:gazelle fix
70# See: https://github.com/bazelbuild/bazel-gazelle#fix-and-update
71gazelle(
72    name = "gazelle",
73    data = GAZELLE_PYTHON_RUNTIME_DEPS,
74    gazelle = "@rules_python_gazelle_plugin//python:gazelle_binary",
75)
76
77# The following targets are created and maintained by gazelle
78py_library(
79    name = "bzlmod_build_file_generation",
80    srcs = ["lib.py"],
81    visibility = ["//:__subpackages__"],
82    deps = ["@pip//tabulate"],
83)
84
85py_binary(
86    name = "bzlmod_build_file_generation_bin",
87    srcs = ["__main__.py"],
88    main = "__main__.py",
89    visibility = ["//:__subpackages__"],
90    deps = [":bzlmod_build_file_generation"],
91)
92
93py_test(
94    name = "bzlmod_build_file_generation_test",
95    srcs = ["__test__.py"],
96    main = "__test__.py",
97    deps = [":bzlmod_build_file_generation"],
98)
99