OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # |
| 3 # Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 import cStringIO |
| 8 import difflib |
| 9 import glob |
| 10 import optparse |
| 11 import sys |
| 12 |
| 13 import tf |
| 14 import val_runner |
| 15 |
| 16 |
| 17 def Annotate(validators, input_lines): |
| 18 test = tf.Test.Parse(input_lines) |
| 19 |
| 20 instrs = test.instructions |
| 21 |
| 22 for validator in validators: |
| 23 for i in instrs: |
| 24 i.outs[validator] = [] |
| 25 |
| 26 validation_results = test.RunValidator(validator) |
| 27 test.safe = validation_results.safe |
| 28 |
| 29 for offset, message in validation_results.errors: |
| 30 for i in reversed(instrs): |
| 31 if offset not in range(i.offset, i.end_offset): |
| 32 continue |
| 33 |
| 34 offset_prefix = '' |
| 35 if offset != i.offset: |
| 36 offset_prefix = '[at +%s] ' % (offset - i.offset) |
| 37 |
| 38 i.outs[validator].append(offset_prefix + message) |
| 39 break |
| 40 else: |
| 41 # If error does not belong to any instruction, |
| 42 # we just add it to the first instruction |
| 43 print '**************** orphan error:', (offset, message) |
| 44 i = instrs[0] |
| 45 i.outs[validator].append('[at +%s] %s' % (offset - i.offset, message)) |
| 46 |
| 47 fout = cStringIO.StringIO() |
| 48 test.Print(fout) |
| 49 fout.seek(0) |
| 50 return fout.readlines() |
| 51 |
| 52 |
| 53 def main(): |
| 54 parser = optparse.OptionParser('%prog [options] <tf file or wildcard>') |
| 55 parser.add_option( |
| 56 '--update', |
| 57 action='store_true', |
| 58 default=False, |
| 59 help=('not only print diff for annotated files, but actually' |
| 60 'update them in place') |
| 61 ) |
| 62 parser.add_option( |
| 63 '--validator', |
| 64 type=str, |
| 65 help=('only use one specific validator (one of the %s)' % |
| 66 ', '.join(val_runner.VALIDATORS)) |
| 67 ) |
| 68 val_runner.AddValidatorsOptions(parser) |
| 69 |
| 70 options, args = parser.parse_args() |
| 71 |
| 72 val_runner.ProcessValidatorsOptions(options) |
| 73 |
| 74 if len(args) != 1: |
| 75 parser.error('specify tf file or wildcard') |
| 76 |
| 77 if options.validator is not None: |
| 78 validators = [options.validator] |
| 79 else: |
| 80 validators = val_runner.VALIDATORS |
| 81 |
| 82 pattern = args[0] |
| 83 |
| 84 num_files = 0 |
| 85 differ = False |
| 86 for name in glob.glob(pattern): |
| 87 num_files += 1 |
| 88 if options.update: |
| 89 print '=' * 3, name |
| 90 |
| 91 with open(name) as fin: |
| 92 original_lines = fin.readlines() |
| 93 |
| 94 annotated_lines = Annotate(validators, original_lines) |
| 95 |
| 96 diff = difflib.unified_diff(original_lines, annotated_lines, |
| 97 fromfile=name, tofile=name) |
| 98 |
| 99 for line in diff: |
| 100 print line.rstrip() |
| 101 differ = True |
| 102 |
| 103 if options.update: |
| 104 # We are always using unix-style newlines. |
| 105 with open(name, mode='wb') as fout: |
| 106 fout.writelines(annotated_lines) |
| 107 |
| 108 if not options.update: |
| 109 if differ: |
| 110 print 'there were differences' |
| 111 sys.exit(1) |
| 112 else: |
| 113 print num_files, 'files were checked, no differences' |
| 114 sys.exit(0) |
| 115 |
| 116 |
| 117 if __name__ == '__main__': |
| 118 main() |
OLD | NEW |