Skip to content

Commit

Permalink
Merge pull request #48 from valeriosetti/enhance-config-py
Browse files Browse the repository at this point in the history
enhance common_config.py and generate_test_code.py
  • Loading branch information
bensze01 authored Oct 11, 2024
2 parents 1de0641 + c9d6bf4 commit d9a70c7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
21 changes: 19 additions & 2 deletions scripts/generate_test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,27 @@
END_CASE_REGEX = r'/\*\s*END_CASE\s*\*/'

DEPENDENCY_REGEX = r'depends_on:(?P<dependencies>.*)'
# This can be something like [!]MBEDTLS_xxx
C_IDENTIFIER_REGEX = r'!?[a-z_][a-z0-9_]*'
# This is a generic relation operator: ==, !=, >[=], <[=]
CONDITION_OPERATOR_REGEX = r'[!=]=|[<>]=?'
# forbid 0ddd which might be accidentally octal or accidentally decimal
CONDITION_VALUE_REGEX = r'[-+]?(0x[0-9a-f]+|0|[1-9][0-9]*)'
# This can be (almost) anything as long as:
# - it starts with a number or a letter or a "("
# - it contains only
# - numbers
# - letters
# - spaces
# - math operators, i.e "+", "-", "*", "/"
# - bitwise operators, i.e. "^", "|", "&", "~", "<<", ">>"
# - parentheses, i.e. "()"
CONDITION_VALUE_REGEX = r'[\w|\(][\s\w\(\)\+\-\*\/\^\|\&\~\<\>]*'
CONDITION_REGEX = r'({})(?:\s*({})\s*({}))?$'.format(C_IDENTIFIER_REGEX,
CONDITION_OPERATOR_REGEX,
CONDITION_VALUE_REGEX)
# Match numerical values that start with a 0 because they can be accidentally
# octal or accidentally decimal. Hexadecimal values starting with '0x' are
# valid of course.
AMBIGUOUS_INTEGER_REGEX = r'\b0[0-9]+'
TEST_FUNCTION_VALIDATION_REGEX = r'\s*void\s+(?P<func_name>\w+)\s*\('
FUNCTION_ARG_LIST_END_REGEX = r'.*\)'
EXIT_LABEL_REGEX = r'^exit:'
Expand Down Expand Up @@ -398,6 +412,9 @@ def validate_dependency(dependency):
:return: input dependency stripped of leading & trailing white spaces.
"""
dependency = dependency.strip()
m = re.search(AMBIGUOUS_INTEGER_REGEX, dependency)
if m:
raise GeneratorInputError('Ambiguous integer literal: '+ m.group(0))
if not re.match(CONDITION_REGEX, dependency, re.I):
raise GeneratorInputError('Invalid dependency %s' % dependency)
return dependency
Expand Down
27 changes: 27 additions & 0 deletions scripts/mbedtls_framework/config_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ 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
regex = re.compile('|'.join(regexs))
for setting in self.settings.values():
if regex.search(setting.name):
if setting.active or not only_enabled:
yield setting.name

def __setitem__(self, name, value):
"""If name is known, set its value.
Expand Down Expand Up @@ -353,6 +363,7 @@ def add_adapter(self, name, function, description):
subparser.set_defaults(adapter=function)

def _common_parser_options(self, default_file_path):
# pylint: disable=too-many-branches
"""Common parser options for config manipulation tool."""

self.parser.add_argument(
Expand Down Expand Up @@ -392,12 +403,22 @@ 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."""
pass

def main(self):
# pylint: disable=too-many-branches
"""Common main fuction for config manipulation tool."""

args = self.args
Expand All @@ -412,6 +433,12 @@ 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)
sys.stdout.write("\n".join(match_list))
elif args.command == 'get-all-enabled':
match_list = config.get_matching(args.regexs, True)
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 d9a70c7

Please sign in to comment.