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