1load("@bazel_skylib//rules:diff_test.bzl", "diff_test") 2load("@bazel_skylib//rules:write_file.bzl", "write_file") 3load("@rules_python//python:pip.bzl", "compile_pip_requirements") 4 5# This rule adds a convenient way to update the requirements.txt 6# lockfile based on the requirements.in. 7compile_pip_requirements(name = "requirements") 8 9# The requirements.bzl file is generated with a reference to the interpreter for the host platform. 10# In order to check in a platform-agnostic file, we have to replace that reference with the symbol 11# loaded from our python toolchain. 12genrule( 13 name = "make_platform_agnostic", 14 srcs = ["@pip//:requirements.bzl"], 15 outs = ["requirements.clean.bzl"], 16 cmd = " | ".join([ 17 "cat $<", 18 # Insert our load statement after the existing one so we don't produce a file with buildifier warnings 19 """sed -e '/^load.*/i\\'$$'\\n''load("@python39//:defs.bzl", "interpreter")'""", 20 # Replace the bazel 6.0.0 specific comment with something that bazel 5.4.0 would produce. 21 # This enables this example to be run as a test under bazel 5.4.0. 22 """sed -e 's#@//#//#'""", 23 """sed 's#"@python39_.*//:bin/python3"#interpreter#' >$@""", 24 ]), 25) 26 27write_file( 28 name = "gen_update", 29 out = "update.sh", 30 content = [ 31 # This depends on bash, would need tweaks for Windows 32 "#!/usr/bin/env bash", 33 # Bazel gives us a way to access the source folder! 34 "cd $BUILD_WORKSPACE_DIRECTORY", 35 "cp -fv bazel-bin/requirements.clean.bzl requirements.bzl", 36 ], 37) 38 39sh_binary( 40 name = "vendor_requirements", 41 srcs = ["update.sh"], 42 data = [":make_platform_agnostic"], 43) 44 45# Similarly ensures that the requirements.bzl file is updated 46# based on the requirements.txt lockfile. 47diff_test( 48 name = "test_vendored", 49 failure_message = "Please run: bazel run //:vendor_requirements", 50 file1 = "requirements.bzl", 51 file2 = ":make_platform_agnostic", 52) 53