|
1 | | -""" |
2 | | -Python wheel repository rules. |
3 | | -
|
4 | | -The calculated wheel version suffix depends on the wheel type: |
5 | | -- nightly: .dev{build_date} |
6 | | -- release: ({custom_version_suffix})? |
7 | | -- custom: .dev{build_date}(+{git_hash})?({custom_version_suffix})? |
8 | | -- snapshot (default): -0 |
9 | | -
|
10 | | -The following environment variables can be set: |
11 | | -{wheel_type}: ML_WHEEL_TYPE |
12 | | -{build_date}: ML_WHEEL_BUILD_DATE (should be YYYYMMDD or YYYY-MM-DD) |
13 | | -{git_hash}: ML_WHEEL_GIT_HASH |
14 | | -{custom_version_suffix}: ML_WHEEL_VERSION_SUFFIX |
15 | | -
|
16 | | -Examples: |
17 | | -1. nightly wheel version: 2.19.0.dev20250107 |
18 | | - Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=nightly |
19 | | - --repo_env=ML_WHEEL_BUILD_DATE=20250107 |
20 | | -2. release wheel version: 2.19.0 |
21 | | - Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=release |
22 | | -3. release candidate wheel version: 2.19.0-rc1 |
23 | | - Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=release |
24 | | - --repo_env=ML_WHEEL_VERSION_SUFFIX=-rc1 |
25 | | -4. custom wheel version: 2.19.0.dev20250107+cbe478fc5-custom |
26 | | - Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=custom |
27 | | - --repo_env=ML_WHEEL_BUILD_DATE=$(git show -s --format=%as HEAD) |
28 | | - --repo_env=ML_WHEEL_GIT_HASH=$(git rev-parse HEAD) |
29 | | - --repo_env=ML_WHEEL_VERSION_SUFFIX=-custom |
30 | | -5. snapshot wheel version: 2.19.0-0 |
31 | | - Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=snapshot |
32 | | -
|
33 | | -""" |
| 1 | +""" Repository and build rules for Python wheels packaging utilities. """ |
34 | 2 |
|
35 | 3 | def _get_host_environ(repository_ctx, name, default_value = None): |
36 | 4 | """Returns the value of an environment variable on the host platform. |
@@ -127,3 +95,125 @@ python_wheel_version_suffix_repository = repository_rule( |
127 | 95 | implementation = _python_wheel_version_suffix_repository_impl, |
128 | 96 | environ = _ENVIRONS, |
129 | 97 | ) |
| 98 | + |
| 99 | +""" Repository rule for storing Python wheel filename version suffix. |
| 100 | +
|
| 101 | +The calculated wheel version suffix depends on the wheel type: |
| 102 | +- nightly: .dev{build_date} |
| 103 | +- release: ({custom_version_suffix})? |
| 104 | +- custom: .dev{build_date}(+{git_hash})?({custom_version_suffix})? |
| 105 | +- snapshot (default): -0 |
| 106 | +
|
| 107 | +The following environment variables can be set: |
| 108 | +{wheel_type}: ML_WHEEL_TYPE |
| 109 | +{build_date}: ML_WHEEL_BUILD_DATE (should be YYYYMMDD or YYYY-MM-DD) |
| 110 | +{git_hash}: ML_WHEEL_GIT_HASH |
| 111 | +{custom_version_suffix}: ML_WHEEL_VERSION_SUFFIX |
| 112 | +
|
| 113 | +Examples: |
| 114 | +1. nightly wheel version: 2.19.0.dev20250107 |
| 115 | + Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=nightly |
| 116 | + --repo_env=ML_WHEEL_BUILD_DATE=20250107 |
| 117 | +2. release wheel version: 2.19.0 |
| 118 | + Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=release |
| 119 | +3. release candidate wheel version: 2.19.0-rc1 |
| 120 | + Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=release |
| 121 | + --repo_env=ML_WHEEL_VERSION_SUFFIX=-rc1 |
| 122 | +4. custom wheel version: 2.19.0.dev20250107+cbe478fc5-custom |
| 123 | + Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=custom |
| 124 | + --repo_env=ML_WHEEL_BUILD_DATE=$(git show -s --format=%as HEAD) |
| 125 | + --repo_env=ML_WHEEL_GIT_HASH=$(git rev-parse HEAD) |
| 126 | + --repo_env=ML_WHEEL_VERSION_SUFFIX=-custom |
| 127 | +5. snapshot wheel version: 2.19.0-0 |
| 128 | + Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=snapshot |
| 129 | +
|
| 130 | +""" # buildifier: disable=no-effect |
| 131 | + |
| 132 | +def _transitive_py_deps_impl(ctx): |
| 133 | + outputs = depset( |
| 134 | + [], |
| 135 | + transitive = [dep[PyInfo].transitive_sources for dep in ctx.attr.deps], |
| 136 | + ) |
| 137 | + |
| 138 | + return DefaultInfo(files = outputs) |
| 139 | + |
| 140 | +_transitive_py_deps = rule( |
| 141 | + attrs = { |
| 142 | + "deps": attr.label_list( |
| 143 | + allow_files = True, |
| 144 | + providers = [PyInfo], |
| 145 | + ), |
| 146 | + }, |
| 147 | + implementation = _transitive_py_deps_impl, |
| 148 | +) |
| 149 | + |
| 150 | +def transitive_py_deps(name, deps = []): |
| 151 | + _transitive_py_deps(name = name + "_gather", deps = deps) |
| 152 | + native.filegroup(name = name, srcs = [":" + name + "_gather"]) |
| 153 | + |
| 154 | +"""Collects python files that a target depends on. |
| 155 | +
|
| 156 | +It traverses dependencies of provided targets, collect their direct and |
| 157 | +transitive python deps and then return a list of paths to files. |
| 158 | +""" # buildifier: disable=no-effect |
| 159 | + |
| 160 | +FilePathInfo = provider( |
| 161 | + "Returns path of selected files.", |
| 162 | + fields = { |
| 163 | + "files": "requested files from data attribute", |
| 164 | + }, |
| 165 | +) |
| 166 | + |
| 167 | +def _collect_data_aspect_impl(_, ctx): |
| 168 | + files = {} |
| 169 | + extensions = ctx.attr._extensions |
| 170 | + if hasattr(ctx.rule.attr, "data"): |
| 171 | + for data in ctx.rule.attr.data: |
| 172 | + for f in data.files.to_list(): |
| 173 | + if not any([f.path.endswith(ext) for ext in extensions]): |
| 174 | + continue |
| 175 | + if "pypi" in f.path: |
| 176 | + continue |
| 177 | + files[f] = True |
| 178 | + |
| 179 | + if hasattr(ctx.rule.attr, "deps"): |
| 180 | + for dep in ctx.rule.attr.deps: |
| 181 | + if dep[FilePathInfo].files: |
| 182 | + for file in dep[FilePathInfo].files.to_list(): |
| 183 | + files[file] = True |
| 184 | + |
| 185 | + return [FilePathInfo(files = depset(files.keys()))] |
| 186 | + |
| 187 | +collect_data_aspect = aspect( |
| 188 | + implementation = _collect_data_aspect_impl, |
| 189 | + attr_aspects = ["deps"], |
| 190 | + attrs = { |
| 191 | + "_extensions": attr.string_list( |
| 192 | + default = [".so", ".pyd", ".pyi", ".dll", ".dylib", ".lib", ".pd"], |
| 193 | + ), |
| 194 | + }, |
| 195 | +) |
| 196 | + |
| 197 | +def _collect_data_files_impl(ctx): |
| 198 | + files = [] |
| 199 | + for dep in ctx.attr.deps: |
| 200 | + files.extend((dep[FilePathInfo].files.to_list())) |
| 201 | + return [DefaultInfo(files = depset( |
| 202 | + files, |
| 203 | + ))] |
| 204 | + |
| 205 | +collect_data_files = rule( |
| 206 | + implementation = _collect_data_files_impl, |
| 207 | + attrs = { |
| 208 | + "deps": attr.label_list( |
| 209 | + aspects = [collect_data_aspect], |
| 210 | + ), |
| 211 | + }, |
| 212 | +) |
| 213 | + |
| 214 | +"""Rule to collect data files. |
| 215 | +
|
| 216 | +It recursively traverses `deps` attribute of the target and collects paths to |
| 217 | +files that are in `data` attribute. Then it filters all files that do not match |
| 218 | +the provided extensions. |
| 219 | +""" # buildifier: disable=no-effect |
0 commit comments