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