|
| 1 | +"Rules to verify and update pip-compile locked requirements.txt" |
| 2 | + |
| 3 | +load("//python:defs.bzl", "py_binary", "py_test") |
| 4 | +load("//python/pip_install:repositories.bzl", "requirement") |
| 5 | + |
| 6 | +def compile_pip_requirements( |
| 7 | + name, |
| 8 | + extra_args = [], |
| 9 | + visibility = ["//visibility:private"], |
| 10 | + requirements_in = "requirements.in", |
| 11 | + requirements_txt = "requirements.txt", |
| 12 | + **kwargs): |
| 13 | + """ |
| 14 | + Macro creating targets for running pip-compile |
| 15 | +
|
| 16 | + Produce a filegroup by default, named "[name]" which can be included in the data |
| 17 | + of some other compile_pip_requirements rule that references these requirements |
| 18 | + (e.g. with `-r ../other/requirements.txt`) |
| 19 | +
|
| 20 | + Produce two targets for checking pip-compile: |
| 21 | +
|
| 22 | + - validate with `bazel test <name>_test` |
| 23 | + - update with `bazel run <name>.update` |
| 24 | +
|
| 25 | + Args: |
| 26 | + name: base name for generated targets, typically "requirements" |
| 27 | + extra_args: passed to pip-compile |
| 28 | + visibility: passed to both the _test and .update rules |
| 29 | + requirements_in: file expressing desired dependencies |
| 30 | + requirements_txt: result of "compiling" the requirements.in file |
| 31 | + **kwargs: other bazel attributes passed to the "_test" rule |
| 32 | + """ |
| 33 | + requirements_in = kwargs.pop("requirements_in", name + ".in") |
| 34 | + requirements_txt = kwargs.pop("requirements_locked", name + ".txt") |
| 35 | + |
| 36 | + # "Default" target produced by this macro |
| 37 | + # Allow a compile_pip_requirements rule to include another one in the data |
| 38 | + # for a requirements file that does `-r ../other/requirements.txt` |
| 39 | + native.filegroup( |
| 40 | + name = name, |
| 41 | + srcs = kwargs.pop("data", []) + [requirements_txt], |
| 42 | + visibility = visibility, |
| 43 | + ) |
| 44 | + |
| 45 | + data = [name, requirements_in, requirements_txt] |
| 46 | + |
| 47 | + # Use the Label constructor so this is expanded in the context of the file |
| 48 | + # where it appears, which is to say, in @rules_python |
| 49 | + pip_compile = Label("//python/pip_install:pip_compile.py") |
| 50 | + |
| 51 | + loc = "$(rootpath %s)" |
| 52 | + |
| 53 | + args = [ |
| 54 | + loc % requirements_in, |
| 55 | + loc % requirements_txt, |
| 56 | + name + ".update", |
| 57 | + ] + extra_args |
| 58 | + |
| 59 | + deps = [ |
| 60 | + requirement("click"), |
| 61 | + requirement("pip"), |
| 62 | + requirement("pip_tools"), |
| 63 | + requirement("setuptools"), |
| 64 | + ] |
| 65 | + |
| 66 | + attrs = { |
| 67 | + "args": args, |
| 68 | + "data": data, |
| 69 | + "deps": deps, |
| 70 | + "main": pip_compile, |
| 71 | + "srcs": [pip_compile], |
| 72 | + "visibility": visibility, |
| 73 | + } |
| 74 | + |
| 75 | + # cheap way to detect the bazel version |
| 76 | + _bazel_version_4_or_greater = "propeller_optimize" in dir(native) |
| 77 | + |
| 78 | + # Bazel 4.0 added the "env" attribute to py_test/py_binary |
| 79 | + if _bazel_version_4_or_greater: |
| 80 | + attrs["env"] = kwargs.pop("env", {}) |
| 81 | + |
| 82 | + py_binary( |
| 83 | + name = name + ".update", |
| 84 | + **attrs |
| 85 | + ) |
| 86 | + |
| 87 | + timeout = kwargs.pop("timeout", "short") |
| 88 | + |
| 89 | + py_test( |
| 90 | + name = name + "_test", |
| 91 | + timeout = timeout, |
| 92 | + # kwargs could contain test-specific attributes like size or timeout |
| 93 | + **dict(attrs, **kwargs) |
| 94 | + ) |
0 commit comments