Skip to content

Commit

Permalink
config.py: add get-all and get-all-enabled commands
Browse files Browse the repository at this point in the history
Both commands use regex expressions to get a list of symbols.
The difference between the 2 is that:

- get-all returns both enabled and commented out symbols while
- get-all-enabled returns only enabled ones.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
  • Loading branch information
valeriosetti committed Sep 23, 2024
1 parent 8c488b1 commit 868837f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions scripts/mbedtls_framework/config_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ def get(self, name, default=None):
else:
return default

def get_matching(self, regexs, only_enabled):
"""Get all symbols matching one of the regexs."""
if not regexs:
return None
match_list = []
regex = re.compile('|'.join(regexs))
for setting in self.settings.values():
if regex.search(setting.name):
if (not only_enabled) or (only_enabled and setting.active):
match_list.append(setting.name)
return match_list

def __setitem__(self, name, value):
"""If name is known, set its value.
Expand Down Expand Up @@ -392,6 +404,15 @@ def _common_parser_options(self, default_file_path):
'unset-all',
help="""Comment out all #define whose name contains a match for REGEX.""")
parser_unset_all.add_argument('regexs', metavar='REGEX', nargs='*')
parser_get_all = self.subparsers.add_parser(
'get-all',
help="""Get all #define whose name contains a match for REGEX.""")
parser_get_all.add_argument('regexs', metavar='REGEX', nargs='*')
parser_get_all_enabled = self.subparsers.add_parser(
'get-all-enabled',
help="""Get all enabled #define whose name contains a match for REGEX.""")
parser_get_all_enabled.add_argument('regexs', metavar='REGEX', nargs='*')


def custom_parser_options(self):
"""Adds custom options for the parser. Designed for overridden by descendant."""
Expand All @@ -412,6 +433,14 @@ def main(self):
if value:
sys.stdout.write(value + '\n')
return 0 if args.symbol in config else 1
elif args.command == 'get-all':
match_list = config.get_matching(args.regexs, False)
if match_list is not None:
sys.stdout.write("\n".join(match_list))
elif args.command == 'get-all-enabled':
match_list = config.get_matching(args.regexs, True)
if match_list is not None:
sys.stdout.write("\n".join(match_list))
elif args.command == 'set':
if not args.force and args.symbol not in config.settings:
sys.stderr.write(
Expand Down

0 comments on commit 868837f

Please sign in to comment.