forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilders_util.bzl
More file actions
153 lines (127 loc) · 4.86 KB
/
builders_util.bzl
File metadata and controls
153 lines (127 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright 2025 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for builders."""
load("@bazel_skylib//lib:types.bzl", "types")
load(":bzlmod_enabled.bzl", "BZLMOD_ENABLED")
def normalize_transition_in_out_values(arg_name, values):
"""Normalize transition inputs/outputs to canonical label strings."""
for i, value in enumerate(values):
values[i] = normalize_transition_in_out_value(arg_name, value)
def normalize_transition_in_out_value(arg_name, value):
"""Normalize a transition input/output value to a canonical label string.
Args:
arg_name: {type}`str` the transition arg name, "input" or "output"
value: A label-like value to normalize.
Returns:
{type}`str` the canonical label string.
"""
if is_label(value):
return str(value)
elif types.is_string(value):
if value.startswith("//command_line_option:"):
return value
if value.startswith("@@" if BZLMOD_ENABLED else "@"):
return value
else:
fail("transition {arg_name} invalid: non-canonical string '{value}'".format(
arg_name = arg_name,
value = value,
))
else:
fail("transition {arg_name} invalid: ({type}) {value}".format(
arg_name = arg_name,
type = type(value),
value = repr(value),
))
def to_label_maybe(value):
"""Converts `value` to a `Label`, maybe.
The "maybe" qualification is because invalid values for `Label()`
are returned as-is (e.g. None, or special values that might be
used with e.g. the `default` attribute arg).
Args:
value: {type}`str | Label | None | object` the value to turn into a label,
or return as-is.
Returns:
{type}`Label | input_value`
"""
if value == None:
return None
if is_label(value):
return value
if types.is_string(value):
return Label(value)
return value
def is_label(obj):
"""Tell if an object is a `Label`."""
return type(obj) == "Label"
def kwargs_set_default_ignore_none(kwargs, key, default):
"""Normalize None/missing to `default`."""
existing = kwargs.get(key)
if existing == None:
kwargs[key] = default
def kwargs_set_default_list(kwargs, key):
"""Normalizes None/missing to list."""
existing = kwargs.get(key)
if existing == None:
kwargs[key] = []
def kwargs_set_default_dict(kwargs, key):
"""Normalizes None/missing to list."""
existing = kwargs.get(key)
if existing == None:
kwargs[key] = {}
def kwargs_set_default_doc(kwargs):
"""Sets the `doc` arg default."""
existing = kwargs.get("doc")
if existing == None:
kwargs["doc"] = ""
def kwargs_set_default_mandatory(kwargs):
"""Sets `False` as the `mandatory` arg default."""
existing = kwargs.get("mandatory")
if existing == None:
kwargs["mandatory"] = False
def kwargs_getter(kwargs, key):
"""Create a function to get `key` from `kwargs`."""
return lambda: kwargs.get(key)
def kwargs_setter(kwargs, key):
"""Create a function to set `key` in `kwargs`."""
def setter(v):
kwargs[key] = v
return setter
def kwargs_getter_doc(kwargs):
"""Creates a `kwargs_getter` for the `doc` key."""
return kwargs_getter(kwargs, "doc")
def kwargs_setter_doc(kwargs):
"""Creates a `kwargs_setter` for the `doc` key."""
return kwargs_setter(kwargs, "doc")
def kwargs_getter_mandatory(kwargs):
"""Creates a `kwargs_getter` for the `mandatory` key."""
return kwargs_getter(kwargs, "mandatory")
def kwargs_setter_mandatory(kwargs):
"""Creates a `kwargs_setter` for the `mandatory` key."""
return kwargs_setter(kwargs, "mandatory")
def list_add_unique(add_to, others, convert = None):
"""Bulk add values to a list if not already present.
Args:
add_to: {type}`list[T]` the list to add values to. It is modified
in-place.
others: {type}`collection[collection[T]]` collection of collections of
the values to add.
convert: {type}`callable | None` function to convert the values to add.
"""
existing = {v: None for v in add_to}
for values in others:
for value in values:
value = convert(value) if convert else value
if value not in existing:
add_to.append(value)