From 721ad64a61412daae3737e5b4e44619dc3d2ff2b Mon Sep 17 00:00:00 2001 From: Helena Rasche Date: Tue, 22 Sep 2020 11:59:19 +0200 Subject: [PATCH] improvements --- scripts/release-diff.py | 152 ++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 77 deletions(-) diff --git a/scripts/release-diff.py b/scripts/release-diff.py index 353156d771c..ba064177b80 100644 --- a/scripts/release-diff.py +++ b/scripts/release-diff.py @@ -1,12 +1,12 @@ #!/usr/bin/env python import subprocess +import argparse import sys from pathlib import Path import glob import yaml from yaml import SafeLoader -old_version = sys.argv[1] def flatten(d, path): """ @@ -58,90 +58,88 @@ def diff_files(old, new): return added, removed, changed - -files_to_diff = glob.glob('config/*.yml.sample') -added = {} -removed = {} -changed = {} -new_files = [] - -for file in files_to_diff: - real_path = Path(file).resolve().relative_to(Path.cwd()) - try: - contents = subprocess.check_output(['git', 'show', f'{old_version}:{real_path}'], stderr=subprocess.STDOUT) - old = yaml.load(contents, Loader=MockOrderedLoader) - with open(real_path, 'r') as handle: - new = yaml.load(handle, Loader=MockOrderedLoader) - - (a, r, c) = diff_files(old, new) - if a: - added[file] = a - - if r: - removed[file] = r - - if c: - changed[file] = c - - except subprocess.CalledProcessError: - new_files.append(file) - -# Print out report -if added or changed or removed: - print("Configuration Changes") - print("=====================") +def _report_dict(title, subheading, data, mapper): + print(title) + print("-" * len(title)) print() - -if added: - print("Added") - print("-----") + print(subheading) print() - print("The following configuration options are new") - print() - for fn in added: + for fn in data: print(fn) print('~' * len(fn)) print() - for k in added[fn]: - print(f"- {k}") + for k in data[fn]: + print(mapper(k)) print() print() -if changed: - print("Changed") - print("-------") - print() - print("The following configuration options have been changed") - print() - for fn in changed: - print(fn) - print('~' * len(fn)) - print() - for (k, o, n) in changed[fn]: - print(f"- {k} has changed from ``{o}`` to ``{n}``") - print() - print() -if removed: - print("Removed") - print("-------") - print() - print("The following configuration options have been completely removed") - print() - for fn in removed: - print(fn) - print('~' * len(fn)) +def report_diff(added, changed, removed, new_files): + # Print out report + if added or changed or removed: + print("Configuration Changes") + print("=====================") print() - for k in removed[fn]: - print(f"- {k}") - print() - print() -if new_files: - print("New Configuration Files") - print("-----------------------") - print() - print(f"The following files are new, or recently converted to yaml since the {old_version}") - print() - for k in new_files: - print(f"- ``{k}``") + if added: + _report_dict("Added", "The following configuration options are new", added, lambda x: f'- {x}') + + if changed: + _report_dict("Changed", "The following configuration options have been changed", changed, lambda x: f'- {x[0]} has changed from ``{x[1]}`` to ``{x[2]}``') + + if removed: + _report_dict("Removed", "The following configuration options have been completely removed", removed, lambda x: f'- {x}') + + if new_files: + print("New Configuration Files") + print("-----------------------") + print() + print(f"The following files are new, or recently converted to yaml") + print() + for k in new_files: + print(f"- ``{k}``") + + +def load_at_time(path, revision=None): + if revision is not None: + return subprocess.check_output(['git', 'show', f'{revision}:{path}'], stderr=subprocess.STDOUT) + else: + with open(path, 'r') as handle: + return handle.read() + + +def main(old_revision, new_revision=None): + files_to_diff = glob.glob('config/*.yml.sample') + added = {} + removed = {} + changed = {} + new_files = [] + + for file in files_to_diff: + real_path = Path(file).resolve().relative_to(Path.cwd()) + try: + old_contents = yaml.load(load_at_time(real_path, old_revision), Loader=MockOrderedLoader) + new_contents = yaml.load(load_at_time(real_path, new_revision), Loader=MockOrderedLoader) + + (a, r, c) = diff_files(old_contents, new_contents) + if a: + added[file] = a + + if r: + removed[file] = r + + if c: + changed[file] = c + + except subprocess.CalledProcessError: + new_files.append(file) + + report_diff(added, removed, changed, new_files) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Diff yaml configuration files between two points in time.') + parser.add_argument("old_revision", help="Old revision") + parser.add_argument("--new_revision", help="New revision (defaults to whatever is currently in tree)") + args = parser.parse_args() + main(args.old_revision, args.new_revision)