1# Copyright 2023 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15""" 16Rules to verify and update pip-compile locked requirements.txt. 17 18NOTE @aignas 2024-06-23: We are using the implementation specific name here to 19make it possible to have multiple tools inside the `pypi` directory 20""" 21 22load("//python:defs.bzl", _py_binary = "py_binary", _py_test = "py_test") 23 24def pip_compile( 25 name, 26 srcs = None, 27 src = None, 28 extra_args = [], 29 extra_deps = [], 30 generate_hashes = True, 31 py_binary = _py_binary, 32 py_test = _py_test, 33 requirements_in = None, 34 requirements_txt = None, 35 requirements_darwin = None, 36 requirements_linux = None, 37 requirements_windows = None, 38 visibility = ["//visibility:private"], 39 tags = None, 40 **kwargs): 41 """Generates targets for managing pip dependencies with pip-compile. 42 43 By default this rules generates a filegroup named "[name]" which can be included in the data 44 of some other compile_pip_requirements rule that references these requirements 45 (e.g. with `-r ../other/requirements.txt`). 46 47 It also generates two targets for running pip-compile: 48 49 - validate with `bazel test [name]_test` 50 - update with `bazel run [name].update` 51 52 If you are using a version control system, the requirements.txt generated by this rule should 53 be checked into it to ensure that all developers/users have the same dependency versions. 54 55 Args: 56 name: base name for generated targets, typically "requirements". 57 srcs: a list of files containing inputs to dependency resolution. If not specified, 58 defaults to `["pyproject.toml"]`. Supported formats are: 59 * a requirements text file, usually named `requirements.in` 60 * A `.toml` file, where the `project.dependencies` list is used as per 61 [PEP621](https://peps.python.org/pep-0621/). 62 src: file containing inputs to dependency resolution. If not specified, 63 defaults to `pyproject.toml`. Supported formats are: 64 * a requirements text file, usually named `requirements.in` 65 * A `.toml` file, where the `project.dependencies` list is used as per 66 [PEP621](https://peps.python.org/pep-0621/). 67 extra_args: passed to pip-compile. 68 extra_deps: extra dependencies passed to pip-compile. 69 generate_hashes: whether to put hashes in the requirements_txt file. 70 py_binary: the py_binary rule to be used. 71 py_test: the py_test rule to be used. 72 requirements_in: file expressing desired dependencies. Deprecated, use src or srcs instead. 73 requirements_txt: result of "compiling" the requirements.in file. 74 requirements_linux: File of linux specific resolve output to check validate if requirement.in has changes. 75 requirements_darwin: File of darwin specific resolve output to check validate if requirement.in has changes. 76 requirements_windows: File of windows specific resolve output to check validate if requirement.in has changes. 77 tags: tagging attribute common to all build rules, passed to both the _test and .update rules. 78 visibility: passed to both the _test and .update rules. 79 **kwargs: other bazel attributes passed to the "_test" rule. 80 """ 81 if len([x for x in [srcs, src, requirements_in] if x != None]) > 1: 82 fail("At most one of 'srcs', 'src', and 'requirements_in' attributes may be provided") 83 84 if requirements_in: 85 srcs = [requirements_in] 86 elif src: 87 srcs = [src] 88 else: 89 srcs = srcs or ["pyproject.toml"] 90 91 requirements_txt = name + ".txt" if requirements_txt == None else requirements_txt 92 93 # "Default" target produced by this macro 94 # Allow a compile_pip_requirements rule to include another one in the data 95 # for a requirements file that does `-r ../other/requirements.txt` 96 native.filegroup( 97 name = name, 98 srcs = kwargs.pop("data", []) + [requirements_txt], 99 visibility = visibility, 100 ) 101 102 data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] 103 104 # Use the Label constructor so this is expanded in the context of the file 105 # where it appears, which is to say, in @rules_python 106 pip_compile = Label("//python/private/pypi/dependency_resolver:dependency_resolver.py") 107 108 loc = "$(rlocationpath {})" 109 110 args = ["--src=%s" % loc.format(src) for src in srcs] + [ 111 loc.format(requirements_txt), 112 "//%s:%s.update" % (native.package_name(), name), 113 "--resolver=backtracking", 114 "--allow-unsafe", 115 ] 116 if generate_hashes: 117 args.append("--generate-hashes") 118 if requirements_linux: 119 args.append("--requirements-linux={}".format(loc.format(requirements_linux))) 120 if requirements_darwin: 121 args.append("--requirements-darwin={}".format(loc.format(requirements_darwin))) 122 if requirements_windows: 123 args.append("--requirements-windows={}".format(loc.format(requirements_windows))) 124 args.extend(extra_args) 125 126 deps = [ 127 Label("@pypi__build//:lib"), 128 Label("@pypi__click//:lib"), 129 Label("@pypi__colorama//:lib"), 130 Label("@pypi__importlib_metadata//:lib"), 131 Label("@pypi__more_itertools//:lib"), 132 Label("@pypi__packaging//:lib"), 133 Label("@pypi__pep517//:lib"), 134 Label("@pypi__pip//:lib"), 135 Label("@pypi__pip_tools//:lib"), 136 Label("@pypi__pyproject_hooks//:lib"), 137 Label("@pypi__setuptools//:lib"), 138 Label("@pypi__tomli//:lib"), 139 Label("@pypi__zipp//:lib"), 140 Label("//python/runfiles:runfiles"), 141 ] + extra_deps 142 143 tags = tags or [] 144 tags.append("requires-network") 145 tags.append("no-remote-exec") 146 tags.append("no-sandbox") 147 attrs = { 148 "args": args, 149 "data": data, 150 "deps": deps, 151 "main": pip_compile, 152 "srcs": [pip_compile], 153 "tags": tags, 154 "visibility": visibility, 155 } 156 157 # setuptools (the default python build tool) attempts to find user 158 # configuration in the user's home direcotory. This seems to work fine on 159 # linux and macOS, but fails on Windows, so we conditionally provide a fake 160 # USERPROFILE env variable to allow setuptools to proceed without finding 161 # user-provided configuration. 162 kwargs["env"] = select({ 163 "@@platforms//os:windows": {"USERPROFILE": "Z:\\FakeSetuptoolsHomeDirectoryHack"}, 164 "//conditions:default": {}, 165 }) | kwargs.get("env", {}) 166 167 py_binary( 168 name = name + ".update", 169 **attrs 170 ) 171 172 timeout = kwargs.pop("timeout", "short") 173 174 py_test( 175 name = name + "_test", 176 timeout = timeout, 177 # kwargs could contain test-specific attributes like size or timeout 178 **dict(attrs, **kwargs) 179 ) 180