2 '''This script parse diff result from stdin filter out trivial differences
3 defined in config file and print out the left
11 from imgdiff.trivial import Conf, Rules
12 from imgdiff.unified import parse
15 PATTERN_PREFIX = re.compile(r'.*?img[12](%(sep)sroot)?(%(sep)s.*)' %
19 def strip_prefix(filename):
20 '''Strip prefix added by imgdiff script.
22 img1/partition_table.txt -> partition_table.txt
23 img1/root/tmp/file -> /tmp/file
25 match = PATTERN_PREFIX.match(filename)
26 return match.group(2) if match else filename
29 def fix_filename(onefile):
31 onefile['filename'] = strip_prefix(onefile['filename'])
36 '''Mark one file and its content as nontrivial
38 def __init__(self, conf_filename):
39 self.rules = Rules(Conf.load(conf_filename))
41 def __call__(self, onefile):
42 self.rules.check_and_mark(onefile)
46 def nontrivial(onefile):
47 '''Filter out nontrivial'''
48 return not('ignore' in onefile and onefile['ignore'])
51 def parse_and_mark(stream, conf_filename=None):
53 Parse diff from stream and mark nontrivial defined
56 stream = parse(stream)
57 stream = map(fix_filename, stream)
60 mark_trivial = Mark(conf_filename)
61 stream = map(mark_trivial, stream)
67 parser = argparse.ArgumentParser()
68 parser.add_argument('-c', '--conf-filename',
69 help='conf for defining unimportant difference')
70 return parser.parse_args()
76 stream = parse_and_mark(sys.stdin, args.conf_filename)
77 stream = filter(nontrivial, stream)
85 if __name__ == '__main__':
89 # normally python exit 1 for exception
90 # we change it to 255 to avoid confusion with 1 difference