blob: 6b20d82c7eec162e693d0faa2e1a69955e427505 [file] [log] [blame]
Adam Nemet6ab2d482017-03-02 17:00:59 +00001#!/usr/bin/env python2.7
2
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
22from multiprocessing import cpu_count, Pool
Adam Nemet6ab2d482017-03-02 17:00:59 +000023
24if __name__ == '__main__':
25 parser = argparse.ArgumentParser(description=desc)
Adam Nemet659d7db2017-07-17 18:00:41 +000026 parser.add_argument(
27 'yaml_dir_or_file_1',
28 help='An optimization record file or a directory searched for optimization '
29 'record files that are used as the old version for the comparison')
30 parser.add_argument(
31 'yaml_dir_or_file_2',
32 help='An optimization record file or a directory searched for optimization '
33 'record files that are used as the new version for the comparison')
Adam Nemet6ab2d482017-03-02 17:00:59 +000034 parser.add_argument(
35 '--jobs',
36 '-j',
37 default=cpu_count(),
38 type=int,
Brian Gesiak701386d2017-06-10 21:33:27 +000039 help='Max job count (defaults to %(default)s, the current CPU count)')
Brian Gesiak5e0a9462017-06-29 18:56:25 +000040 parser.add_argument(
41 '--no-progress-indicator',
42 '-n',
43 action='store_true',
44 default=False,
45 help='Do not display any indicator of how many YAML files were read.')
Adam Nemet6ab2d482017-03-02 17:00:59 +000046 parser.add_argument('--output', '-o', default='diff.opt.yaml')
47 args = parser.parse_args()
48
Adam Nemet9d57dc62017-09-29 05:20:53 +000049 files1 = optrecord.find_opt_files(args.yaml_dir_or_file_1)
50 files2 = optrecord.find_opt_files(args.yaml_dir_or_file_2)
Adam Nemet6ab2d482017-03-02 17:00:59 +000051
Brian Gesiak5e0a9462017-06-29 18:56:25 +000052 print_progress = not args.no_progress_indicator
53 all_remarks1, _, _ = optrecord.gather_results(files1, args.jobs, print_progress)
54 all_remarks2, _, _ = optrecord.gather_results(files2, args.jobs, print_progress)
Adam Nemet6ab2d482017-03-02 17:00:59 +000055
56 added = set(all_remarks2.values()) - set(all_remarks1.values())
57 removed = set(all_remarks1.values()) - set(all_remarks2.values())
58
59 for r in added:
60 r.Added = True
61 for r in removed:
62 r.Added = False
Adam Nemet817e90f2017-07-19 22:04:59 +000063
64 result = added | removed
65 for r in result:
66 r.recover_yaml_structure()
67
Brian Gesiak9473db42017-06-27 16:46:50 +000068 with open(args.output, 'w') as stream:
Adam Nemet817e90f2017-07-19 22:04:59 +000069 yaml.dump_all(result, stream)