aboutsummaryrefslogtreecommitdiff
path: root/examples/pip_install/BUILD.bazel
blob: 35f5a9338a612ee5d8991bd2f4c601885d2b6352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load(
    "@pip//:requirements.bzl",
    "data_requirement",
    "dist_info_requirement",
    "entry_point",
    "requirement",
)
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")

# Toolchain setup, this is optional.
# Demonstrate that we can use the same python interpreter for the toolchain and executing pip in pip install (see WORKSPACE).
#
#load("@rules_python//python:defs.bzl", "py_runtime_pair")
#
#py_runtime(
#    name = "python3_runtime",
#    files = ["@python_interpreter//:files"],
#    interpreter = "@python_interpreter//:python_bin",
#    python_version = "PY3",
#    visibility = ["//visibility:public"],
#)
#
#py_runtime_pair(
#    name = "my_py_runtime_pair",
#    py2_runtime = None,
#    py3_runtime = ":python3_runtime",
#)
#
#toolchain(
#    name = "my_py_toolchain",
#    toolchain = ":my_py_runtime_pair",
#    toolchain_type = "@bazel_tools//tools/python:toolchain_type",
#)
# End of toolchain setup.

py_binary(
    name = "main",
    srcs = ["main.py"],
    deps = [
        requirement("boto3"),
    ],
)

py_test(
    name = "test",
    srcs = ["test.py"],
    deps = [":main"],
)

# For pip dependencies which have entry points, the `entry_point` macro can be
# used from the generated `pip_install` repository to access a runnable binary.

alias(
    name = "yamllint",
    actual = entry_point("yamllint"),
)

# Check that our compiled requirements are up-to-date
compile_pip_requirements(
    name = "requirements",
    extra_args = ["--allow-unsafe"],
    requirements_windows = ":requirements_windows.txt",
)

# Test the use of all pip_install utilities in a single py_test
py_test(
    name = "pip_install_test",
    srcs = ["pip_install_test.py"],
    data = [
        ":yamllint",
        data_requirement("s3cmd"),
        dist_info_requirement("boto3"),
    ],
    env = {
        "WHEEL_DATA_CONTENTS": "$(rootpaths {})".format(data_requirement("s3cmd")),
        "WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("boto3")),
        "YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
    },
    deps = ["@rules_python//python/runfiles"],
)

# Assert that tags are present on resulting py_library,
# which is useful for tooling that needs to reflect on the dep graph
# to determine the packages it was built from.
genquery(
    name = "yamllint_lib_by_version",
    expression = """
    attr("tags", "\\bpypi_version=1.26.3\\b", "@pip_yamllint//:pkg")
    intersect
    attr("tags", "\\bpypi_name=yamllint\\b", "@pip_yamllint//:pkg")
    """,
    scope = [requirement("yamllint")],
)

write_file(
    name = "write_expected",
    out = "expected",
    content = [
        "@pip_yamllint//:pkg",
        "",
    ],
)

diff_test(
    name = "test_query_result",
    file1 = "expected",
    file2 = "yamllint_lib_by_version",
)