2021-11-09 23:13:30 +00:00
|
|
|
"""General utility functions"""
|
|
|
|
|
|
|
|
def _propagate_well_known_tags(tags = []):
|
|
|
|
"""Returns a list of tags filtered from the input set that only contains the ones that are considered "well known"
|
|
|
|
|
|
|
|
These are listed in Bazel's documentation:
|
|
|
|
https://docs.bazel.build/versions/main/test-encyclopedia.html#tag-conventions
|
|
|
|
https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes
|
|
|
|
|
|
|
|
Args:
|
|
|
|
tags: List of tags to filter
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List of tags that only contains the well known set
|
|
|
|
"""
|
|
|
|
|
|
|
|
WELL_KNOWN_TAGS = [
|
|
|
|
"no-sandbox",
|
|
|
|
"no-cache",
|
|
|
|
"no-remote-cache",
|
|
|
|
"no-remote-exec",
|
|
|
|
"no-remote",
|
|
|
|
"local",
|
|
|
|
"requires-network",
|
|
|
|
"block-network",
|
|
|
|
"requires-fakeroot",
|
|
|
|
"exclusive",
|
|
|
|
"manual",
|
|
|
|
"external",
|
|
|
|
]
|
|
|
|
|
2022-01-18 21:24:44 +00:00
|
|
|
# cpu:n tags allow setting the requested number of CPUs for a test target.
|
|
|
|
# More info at https://docs.bazel.build/versions/main/test-encyclopedia.html#other-resources
|
|
|
|
CPU_PREFIX = "cpu:"
|
|
|
|
|
|
|
|
return [
|
2022-02-11 15:52:01 +00:00
|
|
|
tag
|
|
|
|
for tag in tags
|
2022-01-18 21:24:44 +00:00
|
|
|
if tag in WELL_KNOWN_TAGS or tag.startswith(CPU_PREFIX)
|
|
|
|
]
|
2021-11-09 23:13:30 +00:00
|
|
|
|
|
|
|
def _to_label(param):
|
|
|
|
"""Converts a string to a Label. If Label is supplied, the same label is returned.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
param: a string representing a label or a Label
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
a Label
|
|
|
|
"""
|
|
|
|
param_type = type(param)
|
|
|
|
if param_type == "string":
|
2021-11-10 18:38:54 +00:00
|
|
|
if param.startswith("@"):
|
|
|
|
return Label(param)
|
|
|
|
if param.startswith("//"):
|
|
|
|
return Label("@" + param)
|
|
|
|
|
|
|
|
# resolve the relative label from the current package
|
|
|
|
# if 'param' is in another workspace, then this would return the label relative to that workspace, eg:
|
|
|
|
# `Label("@my//foo:bar").relative("@other//baz:bill") == Label("@other//baz:bill")`
|
|
|
|
if param.startswith(":"):
|
|
|
|
param = param[1:]
|
|
|
|
if native.package_name():
|
|
|
|
return Label("@//" + native.package_name()).relative(param)
|
|
|
|
else:
|
|
|
|
return Label("@//:" + param)
|
|
|
|
|
2021-11-09 23:13:30 +00:00
|
|
|
elif param_type == "Label":
|
|
|
|
return param
|
|
|
|
else:
|
2022-06-13 17:10:34 +00:00
|
|
|
msg = "Expected 'string' or 'Label' but got '{}'".format(param_type)
|
|
|
|
fail(msg)
|
2021-11-09 23:13:30 +00:00
|
|
|
|
|
|
|
def _is_external_label(param):
|
|
|
|
"""Returns True if the given Label (or stringy version of a label) represents a target outside of the workspace
|
|
|
|
|
|
|
|
Args:
|
|
|
|
param: a string or label
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
a bool
|
|
|
|
"""
|
|
|
|
return len(_to_label(param).workspace_root) > 0
|
|
|
|
|
|
|
|
# Path to the root of the workspace
|
|
|
|
def _path_to_workspace_root():
|
2022-07-14 04:04:58 +00:00
|
|
|
""" Returns the path to the workspace root under bazel
|
2021-11-09 23:13:30 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Path to the workspace root
|
|
|
|
"""
|
|
|
|
return "/".join([".."] * len(native.package_name().split("/")))
|
|
|
|
|
|
|
|
# Like glob() but returns directories only
|
|
|
|
def _glob_directories(include, **kwargs):
|
|
|
|
all = native.glob(include, exclude_directories = 0, **kwargs)
|
|
|
|
files = native.glob(include, **kwargs)
|
|
|
|
directories = [p for p in all if p not in files]
|
|
|
|
return directories
|
|
|
|
|
2022-07-14 04:04:58 +00:00
|
|
|
def _file_exists(path):
|
|
|
|
"""Check whether a file exists.
|
|
|
|
|
|
|
|
Useful in macros to set defaults for a configuration file if it is present.
|
|
|
|
This can only be called during the loading phase, not from a rule implementation.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
path: a label, or a string which is a path relative to this package
|
|
|
|
"""
|
|
|
|
label = _to_label(path)
|
|
|
|
file_abs = "%s/%s" % (label.package, label.name)
|
|
|
|
file_rel = file_abs[len(native.package_name()) + 1:]
|
2022-07-14 04:39:38 +00:00
|
|
|
file_glob = native.glob([file_rel], exclude_directories = 1)
|
2022-07-14 04:04:58 +00:00
|
|
|
return len(file_glob) > 0
|
|
|
|
|
2021-11-09 23:13:30 +00:00
|
|
|
utils = struct(
|
|
|
|
is_external_label = _is_external_label,
|
2022-07-14 04:04:58 +00:00
|
|
|
file_exists = _file_exists,
|
2021-11-09 23:13:30 +00:00
|
|
|
glob_directories = _glob_directories,
|
|
|
|
path_to_workspace_root = _path_to_workspace_root,
|
|
|
|
propagate_well_known_tags = _propagate_well_known_tags,
|
|
|
|
to_label = _to_label,
|
|
|
|
)
|