1load("@bazel_skylib//rules:diff_test.bzl", "diff_test") 2load("@bazel_skylib//rules:write_file.bzl", "write_file") 3load( 4 "@pip//:requirements.bzl", 5 "data_requirement", 6 "dist_info_requirement", 7 "entry_point", 8 "requirement", 9) 10load("@rules_python//python:defs.bzl", "py_binary", "py_test") 11load("@rules_python//python:pip.bzl", "compile_pip_requirements") 12 13# Toolchain setup, this is optional. 14# Demonstrate that we can use the same python interpreter for the toolchain and executing pip in pip install (see WORKSPACE). 15# 16#load("@rules_python//python:defs.bzl", "py_runtime_pair") 17# 18#py_runtime( 19# name = "python3_runtime", 20# files = ["@python_interpreter//:files"], 21# interpreter = "@python_interpreter//:python_bin", 22# python_version = "PY3", 23# visibility = ["//visibility:public"], 24#) 25# 26#py_runtime_pair( 27# name = "my_py_runtime_pair", 28# py2_runtime = None, 29# py3_runtime = ":python3_runtime", 30#) 31# 32#toolchain( 33# name = "my_py_toolchain", 34# toolchain = ":my_py_runtime_pair", 35# toolchain_type = "@bazel_tools//tools/python:toolchain_type", 36#) 37# End of toolchain setup. 38 39py_binary( 40 name = "main", 41 srcs = ["main.py"], 42 deps = [ 43 requirement("boto3"), 44 ], 45) 46 47py_test( 48 name = "test", 49 srcs = ["test.py"], 50 deps = [":main"], 51) 52 53# For pip dependencies which have entry points, the `entry_point` macro can be 54# used from the generated `pip_install` repository to access a runnable binary. 55 56alias( 57 name = "yamllint", 58 actual = entry_point("yamllint"), 59) 60 61# Check that our compiled requirements are up-to-date 62compile_pip_requirements( 63 name = "requirements", 64 extra_args = ["--allow-unsafe"], 65 requirements_windows = ":requirements_windows.txt", 66) 67 68# Test the use of all pip_install utilities in a single py_test 69py_test( 70 name = "pip_install_test", 71 srcs = ["pip_install_test.py"], 72 data = [ 73 ":yamllint", 74 data_requirement("s3cmd"), 75 dist_info_requirement("boto3"), 76 ], 77 env = { 78 "WHEEL_DATA_CONTENTS": "$(rootpaths {})".format(data_requirement("s3cmd")), 79 "WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("boto3")), 80 "YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)", 81 }, 82 deps = ["@rules_python//python/runfiles"], 83) 84 85# Assert that tags are present on resulting py_library, 86# which is useful for tooling that needs to reflect on the dep graph 87# to determine the packages it was built from. 88genquery( 89 name = "yamllint_lib_by_version", 90 expression = """ 91 attr("tags", "\\bpypi_version=1.26.3\\b", "@pip_yamllint//:pkg") 92 intersect 93 attr("tags", "\\bpypi_name=yamllint\\b", "@pip_yamllint//:pkg") 94 """, 95 scope = [requirement("yamllint")], 96) 97 98write_file( 99 name = "write_expected", 100 out = "expected", 101 content = [ 102 "@pip_yamllint//:pkg", 103 "", 104 ], 105) 106 107diff_test( 108 name = "test_query_result", 109 file1 = "expected", 110 file2 = "yamllint_lib_by_version", 111) 112