diff --git a/.gitignore b/.gitignore index c31942f607a9d08295d470d491ba0383fc3af1e5..37d86d917bafc110fb66419a3a688ef5bb2636f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.pdf /result.md test-*.log +/comments.md diff --git a/README.md b/README.md index 2e483b7a0f273884ef4b1129ac76ee10e21bfe7e..0bd062427393842a801d0ae0b30e267e7de36876 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,18 @@ $ pandoc -oresult.html result.md See [Dependencies]. +### Comments + +For some rules, comments are included with common solutions to test failures or warnings. + +To generate `comments.md`, use the `--comments` option. + +``` {.sh} +$ ./parser.py --comments ... +``` + ### Custom search + For a custom Key:value search, the next two arguments *MUST be included together.* The program will search and display files that met that constraint, without the crosscheck, and display the names, guid, and key:value to the command line. `python3 parser.py ` you can use the `test_dict` below to see available keys. diff --git a/parser.py b/parser.py index dfe145c6c24542524c9fdaa4aa6abac097775c1b..4e8e6786b1f209a76eee012e29eb87e8865bd33b 100755 --- a/parser.py +++ b/parser.py @@ -327,6 +327,7 @@ def matches_crit(test, crit): def apply_rules(cross_check, conf): # Prepare statistics counters stats = {} + comments = 0 for r in conf: stats[r['rule']] = 0 @@ -343,6 +344,9 @@ def apply_rules(cross_check, conf): rule = r['rule'] + if 'comments' in r['update']: + comments += 1 + logging.debug( f"Applying rule `{rule}'" f" to test {i} `{test['name']}'") @@ -368,6 +372,74 @@ def apply_rules(cross_check, conf): f"Updated {n} {maybe_plural(n, 'test')} out of {s}" f" after applying {r} {maybe_plural(r, 'rule')}") + if comments and not args.comments: + logging.info( + f"{comments} comments present, use '--comments'" + f" to output them to file.") + + +# Pull comments from the rules +def check_comments(cross_check, conf): + stats = {} + comments = 0 + md = 'comments.md' + + gen_comments(md) + + for r in conf: + stats[r['rule']] = 0 + + s = len(cross_check) + + for i in range(s): + test = cross_check[i] + + for r in conf: + if not matches_crit(test, r['criteria']): + continue + + rule = r['rule'] + + if 'comments' in r['update']: + comments += 1 + append_comments( + md, + f"guid: {r['criteria']['guid']} \ + comment: {r['update']['comments']}\n" + ) + + stats[rule] += 1 + break + + n = 0 + + for rule, cnt in stats.items(): + n += cnt + + if n: + if comments and md: + logging.info( + f"{comments} {maybe_plural(comments, 'comment')}" + f" added to {md}, please have a look.") + + +# Generate the md file for comments +def gen_comments(md): + # Create comments.md if it does not exist + logging.debug(f"Generate {md}") + + if os.path.exists(f"{md}"): + os.remove(f"{md}") + + open(f'{md}', 'w+') + + +# Append comments to the comments file +def append_comments(md, comment): + with open(md, 'a+') as resultfile: + logging.debug('Writing comment') + resultfile.write(comment) + # Load YAML configuration file # See the README.md for details on the configuration file format. @@ -974,6 +1046,8 @@ if __name__ == '__main__': parser.add_argument( '--seq-db', help='Known sequence files database filename', default=f'{here}/seq_db.yaml') + parser.add_argument( + '--comments', action='store_true', help='Output comments') parser.add_argument('log_file', nargs='?', help='Input .ekl filename') parser.add_argument('seq_file', nargs='?', help='Input .seq filename') parser.add_argument('find_key', nargs='?', help='Search key') @@ -1093,6 +1167,10 @@ if __name__ == '__main__': # Print a one-line summary print_summary(bins, res_keys) + # Check for and print comments + if args.comments: + check_comments(cross_check, conf) + # Print meta-data if args.print_meta: print() diff --git a/sample/sample.yaml b/sample/sample.yaml index ceacfb5262ccfe91b11dadf09980d75ba91ab1c1..808ff397a9d0f7719f468a1f059a448905e05d0e 100644 --- a/sample/sample.yaml +++ b/sample/sample.yaml @@ -23,3 +23,19 @@ test set: EFICompliantTest update: result: IGNORED + +- rule: A sample rule to force a comment + criteria: + descr: UEFI Compliant Test + device path: No device path + group: GenericTest + guid: B27660E2-0E87-4794-82F1-E6BDBD8B7442 + log: EfiCompliantBBTestPlatform_uefi.c + name: UEFI Compliant - IPsec protocols must be implemented + result: WARNING + revision: '0x00010001' + set guid: A0A8BED3-3D6F-4AD8-907A-84D52EE1543B + sub set: PlatformSpecificElements + test set: EFICompliantTest + update: + comments: Sample Comment diff --git a/schemas/template-schema.yaml b/schemas/template-schema.yaml index b19728f91dccdf55cc3e6fc20ab04c42a5a6ad15..281b3a02b07feec12b0cffb474b03bbbd36acf69 100644 --- a/schemas/template-schema.yaml +++ b/schemas/template-schema.yaml @@ -321,7 +321,7 @@ items: - const: '' Updated by: type: string - additionalProperties: false + additionalProperties: true minProperties: 1 update: type: object diff --git a/tests/test-parser b/tests/test-parser index 2ed50f686b0ffe48716d4569c943c8f35a1940ae..d005d1834977a334cace3a21690bede0bd74449d 100755 --- a/tests/test-parser +++ b/tests/test-parser @@ -33,7 +33,7 @@ out="$tmp/out" args=(--config sample/sample.yaml sample/sample.ekl sample/sample.seq) parser.py "${args[@]}" |& tee "$out" grep -q 'Identified.* as "Test sample' "$out" -grep -q 'Updated 1 test.* after applying 1 rule' "$out" +grep -q 'Updated 2 test.* after applying 2 rule' "$out" grep -q 'Meta-data' result.md echo -n 'null config, ' >&3 @@ -71,6 +71,9 @@ md="$tmp/out.md" parser.py "${args[@]}" --md "$md" |& tee "$out" grep -q '# SCT Summary' "$md" +echo -n 'comments, '>&3 +parser.py "${args[@]}" --comments |& tee "$out" + echo -n 'yaml, ' >&3 yaml="$tmp/out.yaml" parser.py "${args[@]}" --yaml "$yaml" |& tee "$out"