Unverified Commit 0e8f32de authored by Jeffrey Booher-Kaeding's avatar Jeffrey Booher-Kaeding Committed by GitHub
Browse files

Merge pull request #10 from vstehle/for-jeffrey

Template and filter
parents cd007bb7 26e1667b
...@@ -32,6 +32,21 @@ $ ./parser.py --sort \ ...@@ -32,6 +32,21 @@ $ ./parser.py --sort \
'group,descr,set guid,test set,sub set,guid,name,log' ... 'group,descr,set guid,test set,sub set,guid,name,log' ...
``` ```
### Filtering data
The `--filter` option allows to specify a python3 expression, which is used as a
filter. The expression is evaluated for each test; if it evaluates to True the
test is kept, otherwise it is omitted. The expression has access to the test
as dict "x".
Example command, which keeps only the failed tests:
``` {.sh}
$ ./parser.py --filter "x['result'] == 'FAILURE'" ...
```
Filtering takes place after the configuration rules, which are described below.
## Configuration file ## Configuration file
It is possible to use a configuration file with command line option `--config It is possible to use a configuration file with command line option `--config
...@@ -97,6 +112,23 @@ Try it with: ...@@ -97,6 +112,23 @@ Try it with:
$ ./parser.py --config sample.yaml ... $ ./parser.py --config sample.yaml ...
``` ```
### Generating a configuration template
To ease the writing of yaml configurations, there is a `--template` option to
generate a configuration "template" from the results of a run:
``` {.sh}
$ ./parser.py --template template.yaml ...
```
This generated configuration can then be further edited manually.
* Tests with result "PASS" are omitted.
* The following tests fields are omitted from the generated rule "criteria":
"iteration", "start date" and "start time".
* The generated rule "criteria" "log" field is filtered to remove the leading
path before C filename.
## Notes ## Notes
### Known Issues: ### Known Issues:
* "comment" is currently not implemented, as formatting is not currently consistent, should reflect the comments from the test. * "comment" is currently not implemented, as formatting is not currently consistent, should reflect the comments from the test.
......
...@@ -7,6 +7,7 @@ import argparse ...@@ -7,6 +7,7 @@ import argparse
import csv import csv
import logging import logging
import json import json
import re
from packaging import version from packaging import version
try: try:
...@@ -314,6 +315,21 @@ def use_config(cross_check, filename): ...@@ -314,6 +315,21 @@ def use_config(cross_check, filename):
sanitize_yaml(conf) sanitize_yaml(conf)
apply_rules(cross_check, conf) apply_rules(cross_check, conf)
# Filter tests data
# Filter is a python expression, which is evaluated for each test
# When the expression evaluates to True, the test is kept
# Otherwise it is dropped
def filter_data(cross_check, Filter):
logging.debug(f"Filtering with `{Filter}'")
# This function "wraps" the filter and is called for each test
def function(x):
return eval(Filter)
return list(filter(function, cross_check))
# Sort tests data in-place # Sort tests data in-place
# sort_keys is a comma-separated list # sort_keys is a comma-separated list
# The first key has precedence, then the second, etc. # The first key has precedence, then the second, etc.
...@@ -359,6 +375,44 @@ def gen_yaml(cross_check, filename): ...@@ -359,6 +375,44 @@ def gen_yaml(cross_check, filename):
yaml.dump(cross_check, yamlfile, Dumper=Dumper) yaml.dump(cross_check, yamlfile, Dumper=Dumper)
# Generate yaml config template
# This is to help writing yaml config.
# We omit tests with result PASS.
# We omit some tests keys: iteration and dates.
# We remove the leading directory from C filename in log.
def gen_template(cross_check, filename):
assert('yaml' in sys.modules)
logging.debug(f'Generate {filename}')
omitted_keys = set(['iteration', 'start date', 'start time'])
t = []
i = 1
for x in cross_check:
if x['result'] == 'PASS':
continue
r = {
'rule': f'Generated rule ({i})',
'criteria': {},
'update': {'result': 'TEMPLATE'},
}
for key, value in x.items():
if key in omitted_keys:
continue
if key == 'log':
value = re.sub(r'^/.*/', '', value)
r['criteria'][key] = value
t.append(r)
i += 1
with open(filename, 'w') as yamlfile:
yaml.dump(t, yamlfile, Dumper=Dumper)
# Combine or two databases db1 and db2 coming from ekl and seq files # Combine or two databases db1 and db2 coming from ekl and seq files
# respectively into a single cross_check database # respectively into a single cross_check database
# Tests in db1, which were not meant to be run according to db2 have their # Tests in db1, which were not meant to be run according to db2 have their
...@@ -447,6 +501,7 @@ def main(): ...@@ -447,6 +501,7 @@ def main():
'--debug', action='store_true', help='Turn on debug messages') '--debug', action='store_true', help='Turn on debug messages')
parser.add_argument( parser.add_argument(
'--sort', help='Comma-separated list of keys to sort output on') '--sort', help='Comma-separated list of keys to sort output on')
parser.add_argument('--filter', help='Python expression to filter results')
parser.add_argument( parser.add_argument(
'log_file', nargs='?', default='sample.ekl', 'log_file', nargs='?', default='sample.ekl',
help='Input .ekl filename') help='Input .ekl filename')
...@@ -462,6 +517,8 @@ def main(): ...@@ -462,6 +517,8 @@ def main():
parser.add_argument( parser.add_argument(
'--config', help='Input .yaml configuration filename') '--config', help='Input .yaml configuration filename')
parser.add_argument('--yaml', help='Output .yaml filename') parser.add_argument('--yaml', help='Output .yaml filename')
parser.add_argument(
'--template', help='Output .yaml config template filename')
args = parser.parse_args() args = parser.parse_args()
...@@ -495,6 +552,10 @@ def main(): ...@@ -495,6 +552,10 @@ def main():
if 'config' in args and args.config is not None: if 'config' in args and args.config is not None:
use_config(cross_check, args.config) use_config(cross_check, args.config)
# Filter tests data, if requested
if args.filter is not None:
cross_check = filter_data(cross_check, args.filter)
# Sort tests data in-place, if requested # Sort tests data in-place, if requested
if args.sort is not None: if args.sort is not None:
sort_data(cross_check, args.sort) sort_data(cross_check, args.sort)
...@@ -558,8 +619,12 @@ def main(): ...@@ -558,8 +619,12 @@ def main():
if 'yaml' in args and args.yaml is not None: if 'yaml' in args and args.yaml is not None:
gen_yaml(cross_check, args.yaml) gen_yaml(cross_check, args.yaml)
#command line argument 3&4, key are to support a key & value search. # Generate yaml config template if requested
#these will be displayed in CLI if 'template' in args and args.template is not None:
gen_template(cross_check, args.template)
# command line argument 3&4, key are to support a key & value search.
# these will be displayed in CLI
if args.find_key is not None and args.find_value is not None: if args.find_key is not None and args.find_value is not None:
found = key_value_find(db1, args.find_key, args.find_value) found = key_value_find(db1, args.find_key, args.find_value)
#print the dict #print the dict
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment