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
59 changes: 59 additions & 0 deletions scripts/build/build/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ 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

build_arguments: Dict[str, str] = {}
for key, value in self.build_arguments.items():
build_arguments[key] = str(value)

result['build_arguments'] = build_arguments

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 +285,44 @@ def HumanString(self):

return result

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

like:

{
"name": "foo"
"shorthand": "foo-bar-baz[-m1]"
"parts": [
{
"name": "foo",
"build_arguments": {
"board": "bar"
}
}
{
"name": "baz",
"build_arguments": {
"app": "foo.baz"
}
}
],
"modifiers": [
{
"name": "modifier1",
"m1": "True"
}
]
}
"""
return {
'name': self.name,
'shorthand': self.HumanString(),
'parts': [part.ToDict() for target in self.fixed_targets for part in target],
nekleo marked this conversation as resolved.
Show resolved Hide resolved
'modifiers': [part.ToDict() for part in self.modifiers]
}

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

Expand Down
29 changes: 19 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,29 @@ 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':
print(json.dumps([target.ToDict() for target in build.targets.BUILD_TARGETS], indent=4))
else:
for target in build.targets.BUILD_TARGETS:
print(target.HumanString())


Expand Down