mirror of
https://github.com/Mbed-TLS/mbedtls-framework.git
synced 2026-08-11 13:47:48 +00:00
Merge pull request #48 from valeriosetti/enhance-config-py
enhance common_config.py and generate_test_code.py
This commit is contained in:
@@ -193,13 +193,27 @@ BEGIN_CASE_REGEX = r'/\*\s*BEGIN_CASE\s*(?P<depends_on>.*?)\s*\*/'
|
||||
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:'
|
||||
@@ -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
|
||||
|
||||
@@ -95,6 +95,16 @@ class Config:
|
||||
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.
|
||||
|
||||
@@ -353,6 +363,7 @@ class ConfigTool(metaclass=ABCMeta):
|
||||
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(
|
||||
@@ -392,12 +403,22 @@ class ConfigTool(metaclass=ABCMeta):
|
||||
'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
|
||||
@@ -412,6 +433,12 @@ class ConfigTool(metaclass=ABCMeta):
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user