1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#!/usr/bin/env python
"""Check to see if results from scorecard exceed minimum required values."""
import argparse
import json
import sys
import yaml
def get_options():
"""Parse command line."""
description = __doc__.split('\n', maxsplit=1)[0]
parser = argparse.ArgumentParser(description=description)
parser.add_argument('scorecard',
metavar='FILE',
help='JSON file containing scorecard')
parser.add_argument('--config',
metavar='FILE',
help='YAML file with required minimum scores')
return parser.parse_args()
def get_scores(scorecard):
"""Extract checks from scorecard as key value pairs"""
result = {}
for check in scorecard['checks']:
result[check['name']] = check['score']
return result
def main(scorecard, config):
"""Compare scorecard scores with expectations from config.
Return number of checks that fail to meet expectations.
"""
num_fails = 0
checks = config.get('Checks')
scores = get_scores(scorecard)
for check, minval in checks.items():
score = scores.get(check, -1)
if score < minval:
num_fails += 1
print(f"{check:23}: {score:2}/{minval:2}"
f"{' ':6} {'Passed' if score >= minval else 'Failed'}")
return num_fails
if __name__ == "__main__":
options = get_options()
with open(options.scorecard, 'r', encoding="utf-8") as json_file:
score_dict = json.load(json_file)
config_dict = {'Checks': {}}
if options.config:
with open(options.config, 'r', encoding="utf-8") as yaml_file:
config_dict = yaml.safe_load(yaml_file)
sys.exit(main(score_dict, config_dict))
|