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