summaryrefslogtreecommitdiffstats
path: root/tools/opt-viewer/opt-diff.py
blob: f39432c8bc9dddd8f955000b64ade65e4179c123 (plain)
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
#!/usr/bin/env python2.7

from __future__ import print_function

desc = '''Generate the difference of two YAML files into a new YAML file (works on
pair of directories too).  A new attribute 'Added' is set to True or False
depending whether the entry is added or removed from the first input to the
next.

The tools requires PyYAML.'''

import yaml
# Try to use the C parser.
try:
    from yaml import CLoader as Loader
except ImportError:
    from yaml import Loader

import optrecord
import argparse
from collections import defaultdict
from multiprocessing import cpu_count, Pool

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument(
        'yaml_dir_or_file_1',
        help='An optimization record file or a directory searched for optimization '
             'record files that are used as the old version for the comparison')
    parser.add_argument(
        'yaml_dir_or_file_2',
        help='An optimization record file or a directory searched for optimization '
             'record files that are used as the new version for the comparison')
    parser.add_argument(
        '--jobs',
        '-j',
        default=cpu_count(),
        type=int,
        help='Max job count (defaults to %(default)s, the current CPU count)')
    parser.add_argument(
        '--no-progress-indicator',
        '-n',
        action='store_true',
        default=False,
        help='Do not display any indicator of how many YAML files were read.')
    parser.add_argument('--output', '-o', default='diff.opt.yaml')
    args = parser.parse_args()

    files1 = optrecord.find_opt_files([args.yaml_dir_or_file_1])
    files2 = optrecord.find_opt_files([args.yaml_dir_or_file_2])

    print_progress = not args.no_progress_indicator
    all_remarks1, _, _ = optrecord.gather_results(files1, args.jobs, print_progress)
    all_remarks2, _, _ = optrecord.gather_results(files2, args.jobs, print_progress)

    added = set(all_remarks2.values()) - set(all_remarks1.values())
    removed = set(all_remarks1.values()) - set(all_remarks2.values())

    for r in added:
        r.Added = True
    for r in removed:
        r.Added = False
    with open(args.output, 'w') as stream:
        yaml.dump_all(added | removed, stream)