xref: /aosp_15_r20/external/bazelbuild-rules_python/examples/pip_parse/BUILD.bazel (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1load("@rules_python//python:defs.bzl", "py_binary", "py_test")
2load("@rules_python//python:pip.bzl", "compile_pip_requirements")
3load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
4
5# Toolchain setup, this is optional.
6# Demonstrate that we can use the same python interpreter for the toolchain and executing pip in pip install (see WORKSPACE).
7#
8#load("@rules_python//python:defs.bzl", "py_runtime_pair")
9#
10#py_runtime(
11#    name = "python3_runtime",
12#    files = ["@python_interpreter//:files"],
13#    interpreter = "@python_interpreter//:python_bin",
14#    python_version = "PY3",
15#    visibility = ["//visibility:public"],
16#)
17#
18#py_runtime_pair(
19#    name = "my_py_runtime_pair",
20#    py2_runtime = None,
21#    py3_runtime = ":python3_runtime",
22#)
23#
24#toolchain(
25#    name = "my_py_toolchain",
26#    toolchain = ":my_py_runtime_pair",
27#    toolchain_type = "@rules_python//python:toolchain_type",
28#)
29# End of toolchain setup.
30
31py_binary(
32    name = "main",
33    srcs = ["main.py"],
34    deps = [
35        "@pypi//requests:pkg",
36        "@pypi//sphinx:pkg",
37        "@pypi//sphinxcontrib_serializinghtml:pkg",
38    ],
39)
40
41py_test(
42    name = "test",
43    srcs = ["test.py"],
44    deps = [":main"],
45)
46
47# For pip dependencies which have entry points, the `entry_point` macro can be
48# used from the generated `pip_parse` repository to access a runnable binary.
49
50py_console_script_binary(
51    name = "yamllint",
52    pkg = "@pypi//yamllint",
53)
54
55# This rule adds a convenient way to update the requirements file.
56compile_pip_requirements(
57    name = "requirements",
58    src = "requirements.in",
59    requirements_txt = "requirements_lock.txt",
60    requirements_windows = "requirements_windows.txt",
61)
62
63# Test the use of all pip_parse utilities in a single py_test
64py_test(
65    name = "pip_parse_test",
66    srcs = ["pip_parse_test.py"],
67    data = [
68        ":yamllint",
69        "@pypi//requests:dist_info",
70        "@pypi//s3cmd:data",
71    ],
72    env = {
73        "WHEEL_DATA_CONTENTS": "$(rootpaths @pypi//s3cmd:data)",
74        "WHEEL_DIST_INFO_CONTENTS": "$(rootpaths @pypi//requests:dist_info)",
75        "YAMLLINT_ENTRY_POINT": "$(rlocationpath :yamllint)",
76    },
77    deps = ["@rules_python//python/runfiles"],
78)
79