blob: 9e921f8488d36f6f50dc81e82e6182814391a74a [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
23import os, os.path
Adam Nemetb1d026f2017-03-06 19:15:22 +000024import fnmatch
Adam Nemet6ab2d482017-03-02 17:00:59 +000025
26def find_files(dir_or_file):
27 if os.path.isfile(dir_or_file):
28 return [dir_or_file]
29
30 all = []
31 for dir, subdirs, files in os.walk(dir_or_file):
32 for file in files:
Adam Nemetb1d026f2017-03-06 19:15:22 +000033 if fnmatch.fnmatch(file, "*.opt.yaml"):
34 all.append( os.path.join(dir, file))
Adam Nemet6ab2d482017-03-02 17:00:59 +000035 return all
36
37if __name__ == '__main__':
38 parser = argparse.ArgumentParser(description=desc)
39 parser.add_argument('yaml_dir_or_file_1')
40 parser.add_argument('yaml_dir_or_file_2')
41 parser.add_argument(
42 '--jobs',
43 '-j',
44 default=cpu_count(),
45 type=int,
Brian Gesiak701386d2017-06-10 21:33:27 +000046 help='Max job count (defaults to %(default)s, the current CPU count)')
Brian Gesiak5e0a9462017-06-29 18:56:25 +000047 parser.add_argument(
48 '--no-progress-indicator',
49 '-n',
50 action='store_true',
51 default=False,
52 help='Do not display any indicator of how many YAML files were read.')
Adam Nemet6ab2d482017-03-02 17:00:59 +000053 parser.add_argument('--output', '-o', default='diff.opt.yaml')
54 args = parser.parse_args()
55
Adam Nemet6ab2d482017-03-02 17:00:59 +000056 files1 = find_files(args.yaml_dir_or_file_1)
57 files2 = find_files(args.yaml_dir_or_file_2)
58
Brian Gesiak5e0a9462017-06-29 18:56:25 +000059 print_progress = not args.no_progress_indicator
60 all_remarks1, _, _ = optrecord.gather_results(files1, args.jobs, print_progress)
61 all_remarks2, _, _ = optrecord.gather_results(files2, args.jobs, print_progress)
Adam Nemet6ab2d482017-03-02 17:00:59 +000062
63 added = set(all_remarks2.values()) - set(all_remarks1.values())
64 removed = set(all_remarks1.values()) - set(all_remarks2.values())
65
66 for r in added:
67 r.Added = True
68 for r in removed:
69 r.Added = False
Brian Gesiak9473db42017-06-27 16:46:50 +000070 with open(args.output, 'w') as stream:
71 yaml.dump_all(added | removed, stream)