blob: 36e81a5d569a746567776da4f1aa57cbd5fc8570 [file] [log] [blame]
Serge Guelton6da47be2019-01-03 15:44:24 +00001#!/usr/bin/env python
Adam Nemet6a466e72017-03-02 17:00:59 +00002
3from __future__ import print_function
4
5desc = '''Generate the difference of two YAML files into a new YAML file (works on
6pair of directories too). A new attribute 'Added' is set to True or False
7depending whether the entry is added or removed from the first input to the
8next.
9
10The tools requires PyYAML.'''
11
12import yaml
13# Try to use the C parser.
14try:
15 from yaml import CLoader as Loader
16except ImportError:
17 from yaml import Loader
18
19import optrecord
20import argparse
21from collections import defaultdict
Adam Nemet6a466e72017-03-02 17:00:59 +000022
23if __name__ == '__main__':
24 parser = argparse.ArgumentParser(description=desc)
Adam Nemetef314682017-07-17 18:00:41 +000025 parser.add_argument(
26 'yaml_dir_or_file_1',
27 help='An optimization record file or a directory searched for optimization '
28 'record files that are used as the old version for the comparison')
29 parser.add_argument(
30 'yaml_dir_or_file_2',
31 help='An optimization record file or a directory searched for optimization '
32 'record files that are used as the new version for the comparison')
Adam Nemet6a466e72017-03-02 17:00:59 +000033 parser.add_argument(
34 '--jobs',
35 '-j',
Zachary Turner135e9422018-01-05 22:05:13 +000036 default=None,
Adam Nemet6a466e72017-03-02 17:00:59 +000037 type=int,
Brian Gesiak23ff0952017-06-10 21:33:27 +000038 help='Max job count (defaults to %(default)s, the current CPU count)')
Brian Gesiakfb669912017-06-29 18:56:25 +000039 parser.add_argument(
Adam Nemetbd8ae822018-02-26 21:15:51 +000040 '--max-size',
41 '-m',
42 default=100000,
43 type=int,
44 help='Maximum number of remarks stored in an output file')
45 parser.add_argument(
Brian Gesiakfb669912017-06-29 18:56:25 +000046 '--no-progress-indicator',
47 '-n',
48 action='store_true',
49 default=False,
50 help='Do not display any indicator of how many YAML files were read.')
Adam Nemetbd8ae822018-02-26 21:15:51 +000051 parser.add_argument('--output', '-o', default='diff{}.opt.yaml')
Adam Nemet6a466e72017-03-02 17:00:59 +000052 args = parser.parse_args()
53
Adam Nemet41247e12017-09-29 05:20:53 +000054 files1 = optrecord.find_opt_files(args.yaml_dir_or_file_1)
55 files2 = optrecord.find_opt_files(args.yaml_dir_or_file_2)
Adam Nemet6a466e72017-03-02 17:00:59 +000056
Brian Gesiakfb669912017-06-29 18:56:25 +000057 print_progress = not args.no_progress_indicator
58 all_remarks1, _, _ = optrecord.gather_results(files1, args.jobs, print_progress)
59 all_remarks2, _, _ = optrecord.gather_results(files2, args.jobs, print_progress)
Adam Nemet6a466e72017-03-02 17:00:59 +000060
61 added = set(all_remarks2.values()) - set(all_remarks1.values())
62 removed = set(all_remarks1.values()) - set(all_remarks2.values())
63
64 for r in added:
65 r.Added = True
66 for r in removed:
67 r.Added = False
Adam Nemetb2613732017-07-19 22:04:59 +000068
Adam Nemetbd8ae822018-02-26 21:15:51 +000069 result = list(added | removed)
Adam Nemetb2613732017-07-19 22:04:59 +000070 for r in result:
71 r.recover_yaml_structure()
72
Adam Nemetbd8ae822018-02-26 21:15:51 +000073 for i in range(0, len(result), args.max_size):
74 with open(args.output.format(i / args.max_size), 'w') as stream:
75 yaml.dump_all(result[i:i + args.max_size], stream)