Skip to content

Commit d9a70c7

Browse files
authored
Merge pull request #48 from valeriosetti/enhance-config-py
enhance common_config.py and generate_test_code.py
2 parents 1de0641 + c9d6bf4 commit d9a70c7

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

scripts/generate_test_code.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,27 @@
193193
END_CASE_REGEX = r'/\*\s*END_CASE\s*\*/'
194194

195195
DEPENDENCY_REGEX = r'depends_on:(?P<dependencies>.*)'
196+
# This can be something like [!]MBEDTLS_xxx
196197
C_IDENTIFIER_REGEX = r'!?[a-z_][a-z0-9_]*'
198+
# This is a generic relation operator: ==, !=, >[=], <[=]
197199
CONDITION_OPERATOR_REGEX = r'[!=]=|[<>]=?'
198-
# forbid 0ddd which might be accidentally octal or accidentally decimal
199-
CONDITION_VALUE_REGEX = r'[-+]?(0x[0-9a-f]+|0|[1-9][0-9]*)'
200+
# This can be (almost) anything as long as:
201+
# - it starts with a number or a letter or a "("
202+
# - it contains only
203+
# - numbers
204+
# - letters
205+
# - spaces
206+
# - math operators, i.e "+", "-", "*", "/"
207+
# - bitwise operators, i.e. "^", "|", "&", "~", "<<", ">>"
208+
# - parentheses, i.e. "()"
209+
CONDITION_VALUE_REGEX = r'[\w|\(][\s\w\(\)\+\-\*\/\^\|\&\~\<\>]*'
200210
CONDITION_REGEX = r'({})(?:\s*({})\s*({}))?$'.format(C_IDENTIFIER_REGEX,
201211
CONDITION_OPERATOR_REGEX,
202212
CONDITION_VALUE_REGEX)
213+
# Match numerical values that start with a 0 because they can be accidentally
214+
# octal or accidentally decimal. Hexadecimal values starting with '0x' are
215+
# valid of course.
216+
AMBIGUOUS_INTEGER_REGEX = r'\b0[0-9]+'
203217
TEST_FUNCTION_VALIDATION_REGEX = r'\s*void\s+(?P<func_name>\w+)\s*\('
204218
FUNCTION_ARG_LIST_END_REGEX = r'.*\)'
205219
EXIT_LABEL_REGEX = r'^exit:'
@@ -398,6 +412,9 @@ def validate_dependency(dependency):
398412
:return: input dependency stripped of leading & trailing white spaces.
399413
"""
400414
dependency = dependency.strip()
415+
m = re.search(AMBIGUOUS_INTEGER_REGEX, dependency)
416+
if m:
417+
raise GeneratorInputError('Ambiguous integer literal: '+ m.group(0))
401418
if not re.match(CONDITION_REGEX, dependency, re.I):
402419
raise GeneratorInputError('Invalid dependency %s' % dependency)
403420
return dependency

scripts/mbedtls_framework/config_common.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ def get(self, name, default=None):
9595
else:
9696
return default
9797

98+
def get_matching(self, regexs, only_enabled):
99+
"""Get all symbols matching one of the regexs."""
100+
if not regexs:
101+
return
102+
regex = re.compile('|'.join(regexs))
103+
for setting in self.settings.values():
104+
if regex.search(setting.name):
105+
if setting.active or not only_enabled:
106+
yield setting.name
107+
98108
def __setitem__(self, name, value):
99109
"""If name is known, set its value.
100110
@@ -353,6 +363,7 @@ def add_adapter(self, name, function, description):
353363
subparser.set_defaults(adapter=function)
354364

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

358369
self.parser.add_argument(
@@ -392,12 +403,22 @@ def _common_parser_options(self, default_file_path):
392403
'unset-all',
393404
help="""Comment out all #define whose name contains a match for REGEX.""")
394405
parser_unset_all.add_argument('regexs', metavar='REGEX', nargs='*')
406+
parser_get_all = self.subparsers.add_parser(
407+
'get-all',
408+
help="""Get all #define whose name contains a match for REGEX.""")
409+
parser_get_all.add_argument('regexs', metavar='REGEX', nargs='*')
410+
parser_get_all_enabled = self.subparsers.add_parser(
411+
'get-all-enabled',
412+
help="""Get all enabled #define whose name contains a match for REGEX.""")
413+
parser_get_all_enabled.add_argument('regexs', metavar='REGEX', nargs='*')
414+
395415

396416
def custom_parser_options(self):
397417
"""Adds custom options for the parser. Designed for overridden by descendant."""
398418
pass
399419

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

403424
args = self.args
@@ -412,6 +433,12 @@ def main(self):
412433
if value:
413434
sys.stdout.write(value + '\n')
414435
return 0 if args.symbol in config else 1
436+
elif args.command == 'get-all':
437+
match_list = config.get_matching(args.regexs, False)
438+
sys.stdout.write("\n".join(match_list))
439+
elif args.command == 'get-all-enabled':
440+
match_list = config.get_matching(args.regexs, True)
441+
sys.stdout.write("\n".join(match_list))
415442
elif args.command == 'set':
416443
if not args.force and args.symbol not in config.settings:
417444
sys.stderr.write(

0 commit comments

Comments
 (0)