Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON as a format option for targets command in build_examples.py #25810

Merged
merged 9 commits into from
Mar 25, 2023
62 changes: 62 additions & 0 deletions scripts/build/build/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ def Accept(self, full_input: str):

return True

def ToDict(self):
"""Converts a TargetPart into a dictionary
"""

result: Dict[str, str] = {}
result['name'] = self.name
bld_args_keys = list(self.build_arguments.keys())
nekleo marked this conversation as resolved.
Show resolved Hide resolved
for key in bld_args_keys:
result[key] = str(self.build_arguments[key])

if self.only_if_re is not None:
result['only_if_re'] = str(self.only_if_re.pattern)

if self.except_if_re is not None:
result['except_if_re'] = str(self.except_if_re.pattern)

return result


def _HasVariantPrefix(value: str, prefix: str):
"""Checks if the given value is <prefix> or starts with "<prefix>-".
Expand Down Expand Up @@ -264,6 +282,50 @@ def HumanString(self):

return result

def ToDict(self):
"""Outputs a parseable description of the available variants
and modifiers:

like:

"foo": {
"builder": "<class 'builders.foo.FooBuilder'>",
nekleo marked this conversation as resolved.
Show resolved Hide resolved
"shorthand": "foo-bar-baz[-m1]
nekleo marked this conversation as resolved.
Show resolved Hide resolved
"fixed_targets": [
{
"name": "foo",
"board": "bar"
}
{
"name": "baz",
"app": "foo.baz"
}
],
"modifiers": [
{
"name": "modifier1",
"m1": "True"
}
]
},
"""
result: Dict[str, Any] = {}

result['name'] = self.name
result['builder'] = str(self.builder_class)
nekleo marked this conversation as resolved.
Show resolved Hide resolved
result['shorthand'] = self.HumanString()
result['parts'] = []
result['modifiers'] = []
nekleo marked this conversation as resolved.
Show resolved Hide resolved

for target in self.fixed_targets:
for target_part in target:
nekleo marked this conversation as resolved.
Show resolved Hide resolved
result['parts'].append(target_part.ToDict())

for target_part in self.modifiers:
result['modifiers'].append(target_part.ToDict())

return result

def AllVariants(self) -> Iterable[str]:
"""Returns all possible accepted variants by this target.

Expand Down
34 changes: 24 additions & 10 deletions scripts/build/build_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import logging
import os
import sys
Expand Down Expand Up @@ -177,21 +178,34 @@ def cmd_generate(context):

@main.command(
'targets',
help=('List the targets that would be generated/built given '
'the input arguments'))
help=('Lists the targets that can be used with the build and gen commands'))
@click.option(
'--expand',
default=False,
is_flag=True,
help='Expand all possible targets rather than the shorthand string')
'--format',
default='summary',
type=click.Choice(['summary', 'expanded', 'json'], case_sensitive=False),
help="""
summary - list of shorthand strings summarzing the available targets;

expanded - list all possible targets rather than the shorthand string;

json - a JSON representation of the available targets
""")
@click.pass_context
def cmd_targets(context, expand):
for target in build.targets.BUILD_TARGETS:
if expand:
def cmd_targets(context, format):
if format == 'expanded':
for target in build.targets.BUILD_TARGETS:
build.target.report_rejected_parts = False
for s in target.AllVariants():
print(s)
else:
elif format == 'json':
targets = []

for target in build.targets.BUILD_TARGETS:
nekleo marked this conversation as resolved.
Show resolved Hide resolved
targets.append(target.ToDict())

print(json.dumps(targets, indent=4))
else:
for target in build.targets.BUILD_TARGETS:
print(target.HumanString())


Expand Down