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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import argparse
import json
import logging
import shlex
def main():
logging.basicConfig(format="assert_compilation: %(message)s", level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument(dest='input', type=argparse.FileType(mode='r'))
subparsers = parser.add_subparsers(dest='command')
count_parser = subparsers.add_parser('count').add_mutually_exclusive_group()
count_parser.add_argument('-eq', dest='eq', type=int)
count_parser.add_argument('-gt', dest='gt', type=int)
count_parser.add_argument('-ge', dest='ge', type=int)
count_parser.add_argument('-lt', dest='lt', type=int)
count_parser.add_argument('-le', dest='le', type=int)
entry_parser = subparsers.add_parser('contains')
entry_parser.add_argument('-file')
entry_parser.add_argument('-output')
entry_parser.add_argument('-directory')
entry_parser.add_argument('-arguments', nargs=argparse.REMAINDER)
args = parser.parse_args()
logging.debug(args)
entries = json.load(args.input)
if args.command == 'count':
count = len(entries)
logging.debug("%s has %d command entries", args.input.name, count)
if args.eq is not None and args.eq != count:
logging.info('failed: expected %d, but got %d', args.eq, count)
return 1
elif args.gt is not None and args.gt >= count:
logging.info('failed: expected greater than %d, but got %d', args.gt, count)
return 1
elif args.ge is not None and args.ge > count:
logging.info('failed: expected greater or equal %d, but got %d', args.ge, count)
return 1
elif args.lt is not None and args.lt <= count:
logging.info('failed: expected less than %d, but got %d', args.lt, count)
return 1
elif args.le is not None and args.le < count:
logging.info('failed: expected less or equal %d, but got %d', args.le, count)
return 1
elif args.command == 'contains':
logging.debug("%s has command entries: %s", args.input.name, entries)
check = filter_from(args)
count = len([cmd for cmd in entries if check(cmd)])
if count == 0:
request = to_string(args)
logging.info('failed: expected at least one entry, but found none: %s', request)
return 1
return 0
def filter_from(args):
def test(entry):
p1 = True if args.file is None else entry['file'] == args.file
p2 = True if args.output is None else entry['output'] == args.output
p3 = True if args.directory is None else entry['directory'] == args.directory
if args.arguments is not None:
if 'arguments' in entry:
p4 = entry['arguments'] == args.arguments
else:
p4 = shlex.split(entry['command']) == args.arguments
else:
p4 = True
return p1 and p2 and p3 and p4
return test
def to_string(args):
p1 = '' if args.file is None else '-file {}'.format(args.file)
p2 = '' if args.output is None else '-output {}'.format(args.output)
p3 = '' if args.directory is None else '-directory {}'.format(args.directory)
p4 = '' if args.arguments is None else ' '.join(['-arguments'] + args.arguments)
return ' '.join([p1, p2, p3, p4])
if __name__ == "__main__":
sys.exit(main())
|