|
| 1 | +"""Utilities for `rules_python` pip rules""" |
| 2 | + |
| 3 | +_SRCS_TEMPLATE = """\ |
| 4 | +\"\"\"A generate file containing all source files used for `@rules_python//python/pip_install:pip_repository.bzl` rules |
| 5 | +
|
| 6 | +This file is auto-generated from the `@rules_python//python/pip_install/private:srcs_module.install` target. Please |
| 7 | +`bazel run` this target to apply any updates. Note that doing so will discard any local modifications. |
| 8 | +"\"\" |
| 9 | +
|
| 10 | +# Each source file is tracked as a target so `pip_repository` rules will know to automatically rebuild if any of the |
| 11 | +# sources changed. |
| 12 | +PIP_INSTALL_PY_SRCS = [ |
| 13 | + {srcs} |
| 14 | +] |
| 15 | +""" |
| 16 | + |
| 17 | +def _src_label(file): |
| 18 | + dir_path, file_name = file.short_path.rsplit("/", 1) |
| 19 | + |
| 20 | + return "@rules_python//{}:{}".format( |
| 21 | + dir_path, |
| 22 | + file_name, |
| 23 | + ) |
| 24 | + |
| 25 | +def _srcs_module_impl(ctx): |
| 26 | + srcs = [_src_label(src) for src in ctx.files.srcs] |
| 27 | + if not srcs: |
| 28 | + fail("`srcs` cannot be empty") |
| 29 | + output = ctx.actions.declare_file(ctx.label.name) |
| 30 | + |
| 31 | + ctx.actions.write( |
| 32 | + output = output, |
| 33 | + content = _SRCS_TEMPLATE.format( |
| 34 | + srcs = "\n ".join(["\"{}\",".format(src) for src in srcs]), |
| 35 | + ), |
| 36 | + ) |
| 37 | + |
| 38 | + return DefaultInfo( |
| 39 | + files = depset([output]), |
| 40 | + ) |
| 41 | + |
| 42 | +_srcs_module = rule( |
| 43 | + doc = "A rule for writing a list of sources to a templated file", |
| 44 | + implementation = _srcs_module_impl, |
| 45 | + attrs = { |
| 46 | + "srcs": attr.label( |
| 47 | + doc = "A filegroup of source files", |
| 48 | + allow_files = True, |
| 49 | + ), |
| 50 | + }, |
| 51 | +) |
| 52 | + |
| 53 | +_INSTALLER_TEMPLATE = """\ |
| 54 | +#!/bin/bash |
| 55 | +set -euo pipefail |
| 56 | +cp -f "{path}" "${{BUILD_WORKSPACE_DIRECTORY}}/{dest}" |
| 57 | +""" |
| 58 | + |
| 59 | +def _srcs_updater_impl(ctx): |
| 60 | + output = ctx.actions.declare_file(ctx.label.name + ".sh") |
| 61 | + target_file = ctx.file.input |
| 62 | + dest = ctx.file.dest.short_path |
| 63 | + |
| 64 | + ctx.actions.write( |
| 65 | + output = output, |
| 66 | + content = _INSTALLER_TEMPLATE.format( |
| 67 | + path = target_file.short_path, |
| 68 | + dest = dest, |
| 69 | + ), |
| 70 | + is_executable = True, |
| 71 | + ) |
| 72 | + |
| 73 | + return DefaultInfo( |
| 74 | + files = depset([output]), |
| 75 | + runfiles = ctx.runfiles(files = [target_file]), |
| 76 | + executable = output, |
| 77 | + ) |
| 78 | + |
| 79 | +_srcs_updater = rule( |
| 80 | + doc = "A rule for writing a `srcs.bzl` file back to the repository", |
| 81 | + implementation = _srcs_updater_impl, |
| 82 | + attrs = { |
| 83 | + "dest": attr.label( |
| 84 | + doc = "The target file to write the new `input` to.", |
| 85 | + allow_single_file = ["srcs.bzl"], |
| 86 | + mandatory = True, |
| 87 | + ), |
| 88 | + "input": attr.label( |
| 89 | + doc = "The file to write back to the repository", |
| 90 | + allow_single_file = True, |
| 91 | + mandatory = True, |
| 92 | + ), |
| 93 | + }, |
| 94 | + executable = True, |
| 95 | +) |
| 96 | + |
| 97 | +def srcs_module(name, dest, **kwargs): |
| 98 | + """A helper rule to ensure `pip_repository` rules are always up to date |
| 99 | +
|
| 100 | + Args: |
| 101 | + name (str): The name of the sources module |
| 102 | + dest (str): The filename the module should be written as in the current package. |
| 103 | + **kwargs (dict): Additional keyword arguments |
| 104 | + """ |
| 105 | + tags = kwargs.pop("tags", []) |
| 106 | + |
| 107 | + _srcs_module( |
| 108 | + name = name, |
| 109 | + tags = tags, |
| 110 | + **kwargs |
| 111 | + ) |
| 112 | + |
| 113 | + _srcs_updater( |
| 114 | + name = name + ".update", |
| 115 | + input = name, |
| 116 | + dest = dest, |
| 117 | + tags = tags, |
| 118 | + ) |
0 commit comments