Adam Nemet | 6ab2d48 | 2017-03-02 17:00:59 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | |
| 3 | from __future__ import print_function |
| 4 | |
| 5 | desc = '''Generate the difference of two YAML files into a new YAML file (works on |
| 6 | pair of directories too). A new attribute 'Added' is set to True or False |
| 7 | depending whether the entry is added or removed from the first input to the |
| 8 | next. |
| 9 | |
| 10 | The tools requires PyYAML.''' |
| 11 | |
| 12 | import yaml |
| 13 | # Try to use the C parser. |
| 14 | try: |
| 15 | from yaml import CLoader as Loader |
| 16 | except ImportError: |
| 17 | from yaml import Loader |
| 18 | |
| 19 | import optrecord |
| 20 | import argparse |
| 21 | from collections import defaultdict |
| 22 | from multiprocessing import cpu_count, Pool |
Adam Nemet | 6ab2d48 | 2017-03-02 17:00:59 +0000 | [diff] [blame] | 23 | |
| 24 | if __name__ == '__main__': |
| 25 | parser = argparse.ArgumentParser(description=desc) |
Adam Nemet | 659d7db | 2017-07-17 18:00:41 +0000 | [diff] [blame] | 26 | 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 Nemet | 6ab2d48 | 2017-03-02 17:00:59 +0000 | [diff] [blame] | 34 | parser.add_argument( |
| 35 | '--jobs', |
| 36 | '-j', |
| 37 | default=cpu_count(), |
| 38 | type=int, |
Brian Gesiak | 701386d | 2017-06-10 21:33:27 +0000 | [diff] [blame] | 39 | help='Max job count (defaults to %(default)s, the current CPU count)') |
Brian Gesiak | 5e0a946 | 2017-06-29 18:56:25 +0000 | [diff] [blame] | 40 | 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 Nemet | 6ab2d48 | 2017-03-02 17:00:59 +0000 | [diff] [blame] | 46 | parser.add_argument('--output', '-o', default='diff.opt.yaml') |
| 47 | args = parser.parse_args() |
| 48 | |
Adam Nemet | 9d57dc6 | 2017-09-29 05:20:53 +0000 | [diff] [blame] | 49 | files1 = optrecord.find_opt_files(args.yaml_dir_or_file_1) |
| 50 | files2 = optrecord.find_opt_files(args.yaml_dir_or_file_2) |
Adam Nemet | 6ab2d48 | 2017-03-02 17:00:59 +0000 | [diff] [blame] | 51 | |
Brian Gesiak | 5e0a946 | 2017-06-29 18:56:25 +0000 | [diff] [blame] | 52 | 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 Nemet | 6ab2d48 | 2017-03-02 17:00:59 +0000 | [diff] [blame] | 55 | |
| 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 Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 63 | |
| 64 | result = added | removed |
| 65 | for r in result: |
| 66 | r.recover_yaml_structure() |
| 67 | |
Brian Gesiak | 9473db4 | 2017-06-27 16:46:50 +0000 | [diff] [blame] | 68 | with open(args.output, 'w') as stream: |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 69 | yaml.dump_all(result, stream) |