2022-04-14 17:48:44 +00:00
|
|
|
"Helpers for copy rules"
|
|
|
|
|
2023-10-09 20:57:34 +00:00
|
|
|
CopyOptionsInfo = provider("Options for running copy actions", fields = ["execution_requirements"])
|
|
|
|
|
|
|
|
def _copy_options_impl(ctx):
|
|
|
|
return CopyOptionsInfo(
|
|
|
|
execution_requirements = COPY_EXECUTION_REQUIREMENTS_LOCAL if ctx.attr.copy_use_local_execution else {},
|
|
|
|
)
|
|
|
|
|
|
|
|
copy_options = rule(implementation = _copy_options_impl, attrs = {"copy_use_local_execution": attr.bool()})
|
|
|
|
|
|
|
|
# Helper function to be used when creating an action
|
|
|
|
def execution_requirements_for_copy(ctx):
|
|
|
|
if hasattr(ctx.attr, "_options") and CopyOptionsInfo in ctx.attr._options:
|
|
|
|
return ctx.attr._options[CopyOptionsInfo].execution_requirements
|
|
|
|
|
|
|
|
# If the rule ctx doesn't expose the CopyOptions, the default is to run locally
|
|
|
|
return COPY_EXECUTION_REQUIREMENTS_LOCAL
|
|
|
|
|
2023-10-31 20:38:32 +00:00
|
|
|
# When applied to execution_requirements of an action, these prevent the action from being sandboxed
|
|
|
|
# for improved performance.
|
2023-10-09 20:57:34 +00:00
|
|
|
COPY_EXECUTION_REQUIREMENTS_LOCAL = {
|
2023-10-06 22:20:40 +00:00
|
|
|
# ----------------+-----------------------------------------------------------------------------
|
|
|
|
# no-sandbox | Results in the action or test never being sandboxed; it can still be cached
|
2023-10-31 20:38:32 +00:00
|
|
|
# | or run remotely.
|
2023-10-06 22:20:40 +00:00
|
|
|
# ----------------+-----------------------------------------------------------------------------
|
|
|
|
# See https://bazel.google.cn/reference/be/common-definitions?hl=en&authuser=0#common-attributes
|
|
|
|
#
|
2023-10-31 20:38:32 +00:00
|
|
|
# Sandboxing for this action is wasteful since there is a 1:1 mapping of input file/directory to
|
|
|
|
# output file/directory so little room for non-hermetic inputs to sneak in to the execution.
|
2023-10-06 22:20:40 +00:00
|
|
|
"no-sandbox": "1",
|
2022-04-14 17:48:44 +00:00
|
|
|
}
|
2023-02-06 04:47:02 +00:00
|
|
|
|
|
|
|
def progress_path(f):
|
|
|
|
"""
|
|
|
|
Convert a file to an appropriate string to display in an action progress message.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
f: a file to show as a path in a progress message
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The path formatted for use in a progress message
|
|
|
|
"""
|
|
|
|
return f.short_path.removeprefix("../")
|