Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

181 lines
4.8 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python
2020-09-22 11:59:19 +02:00
import argparse
2020-09-22 11:39:35 +02:00
import glob
2020-10-14 10:43:04 +02:00
import subprocess
from pathlib import Path
import yaml
def flatten(d, path):
"""
Flatten a dictionary into ('some/path/to/key', value)
>>> flatten({'a': {'b': 2}, 'q': 3}, [])
[('a.b', 2), ('q', 3)]
"""
if isinstance(d, dict):
for k, v in d.items():
yield from flatten(v, path + [k])
else:
yield (".".join(path), d)
def flat_dict(d):
return dict(flatten(d, []))
# Load without the includes since we can't follow those across git revisions.
2020-10-14 10:43:04 +02:00
class MockOrderedLoader(yaml.SafeLoader):
def include(self, node):
return {}
MockOrderedLoader.add_constructor("!include", MockOrderedLoader.include)
2020-09-22 11:39:35 +02:00
def diff_files(old, new):
# Flatten them
old_kv = flat_dict(old)
new_kv = flat_dict(new)
# Compare them
old_k = set(old_kv.keys())
new_k = set(new_kv.keys())
added = []
for item in new_k - old_k:
parent = ".".join(item.split(".")[0:-1])
if parent in new_k and parent not in old_k:
added.append(item)
else:
added.append(parent)
added = set(added)
removed = []
for item in old_k - new_k:
parent = ".".join(item.split(".")[0:-1])
if parent in old_k and parent not in new_k:
removed.append(item)
else:
removed.append(parent)
removed = set(removed)
2020-09-22 11:39:35 +02:00
shared = old_k & new_k
changed = [(k, old_kv[k], new_kv[k]) for k in shared if old_kv[k] != new_kv[k]]
return added, removed, changed
2020-09-22 11:59:19 +02:00
def _report_dict(title, subheading, data, mapper):
print(title)
print("-" * len(title))
print()
2020-09-22 11:59:19 +02:00
print(subheading)
print()
2020-09-22 11:59:19 +02:00
for fn in data:
2020-09-22 11:39:35 +02:00
print(fn)
2020-09-22 12:06:06 +02:00
print("~" * len(fn))
2020-09-22 11:39:35 +02:00
print()
2020-09-22 11:59:19 +02:00
for k in data[fn]:
print(mapper(k))
2020-09-22 11:39:35 +02:00
print()
print()
def _indent(s, by=4):
whitespace = " " * by
s = s if isinstance(s, list) else str(s).splitlines()
2022-03-08 13:09:10 +00:00
return "\n".join(f"{whitespace}{line}" for line in s)
2020-09-22 11:59:19 +02:00
def report_diff(added, changed, removed, new_files):
# Print out report
if added or changed or removed:
print("Configuration Changes")
print("=====================")
2020-09-22 11:39:35 +02:00
print()
2020-09-22 11:46:29 +02:00
2020-09-22 11:59:19 +02:00
if added:
2020-09-22 12:06:06 +02:00
_report_dict("Added", "The following configuration options are new", added, lambda x: f"- {x}")
2020-09-22 11:59:19 +02:00
if changed:
2020-09-22 12:06:06 +02:00
_report_dict(
"Changed",
"The following configuration options have been changed",
changed,
2026-06-16 09:22:13 +00:00
lambda x: (
f"- {x[0]} has changed from\n\n ::\n\n{_indent(x[1])}\n\n to\n\n ::\n\n{_indent(x[2])}\n\n"
),
2020-09-22 12:06:06 +02:00
)
2020-09-22 11:59:19 +02:00
if removed:
2020-09-22 12:06:06 +02:00
_report_dict(
"Removed", "The following configuration options have been completely removed", removed, lambda x: f"- {x}"
)
2020-09-22 11:59:19 +02:00
if new_files:
print("New Configuration Files")
print("-----------------------")
print()
2020-10-14 10:43:04 +02:00
print("The following files are new, or recently converted to yaml")
2020-09-22 11:59:19 +02:00
print()
for k in new_files:
print(f"- ``{k}``")
def load_at_time(path, revision=None):
if revision is not None:
2020-09-22 12:06:06 +02:00
return subprocess.check_output(["git", "show", f"{revision}:{path}"], stderr=subprocess.STDOUT)
2020-09-22 11:59:19 +02:00
else:
2020-11-18 13:56:56 +00:00
with open(path) as handle:
2020-09-22 11:59:19 +02:00
return handle.read()
def main(old_revision, new_revision=None):
2022-01-14 14:27:59 +01:00
globs = (
"config/*.yml.sample",
"lib/galaxy/config/schemas/*schema.yml",
)
files_to_diff = [f for g in globs for f in glob.glob(g)]
2020-09-22 11:59:19 +02:00
added = {}
removed = {}
changed = {}
new_files = []
for file in files_to_diff:
2021-06-10 18:59:56 +02:00
filename = file
if "config_schema.yml" in file:
filename = "config/galaxy.yml.sample:galaxy"
2020-09-22 11:59:19 +02:00
real_path = Path(file).resolve().relative_to(Path.cwd())
try:
2020-09-22 12:06:06 +02:00
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)
2020-09-22 11:59:19 +02:00
2026-01-19 22:33:34 +00:00
a, r, c = diff_files(old_contents, new_contents)
2020-09-22 11:59:19 +02:00
if a:
2021-06-10 18:59:56 +02:00
added[filename] = sorted(a)
2020-09-22 11:59:19 +02:00
if r:
2021-06-10 18:59:56 +02:00
removed[filename] = sorted(r)
2020-09-22 11:59:19 +02:00
if c:
2021-06-10 18:59:56 +02:00
changed[filename] = sorted(c)
2020-09-22 11:59:19 +02:00
except subprocess.CalledProcessError:
new_files.append(file)
2020-09-22 12:01:07 +02:00
report_diff(added, changed, removed, new_files)
2020-09-22 11:59:19 +02:00
2020-09-22 12:06:06 +02:00
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Diff yaml configuration files between two points in time.")
2020-09-22 11:59:19 +02:00
parser.add_argument("old_revision", help="Old revision")
2020-09-22 12:06:06 +02:00
parser.add_argument(
"--new_revision",
help="New revision (defaults to whatever is currently in tree)",
)
2020-09-22 11:59:19 +02:00
args = parser.parse_args()
main(args.old_revision, args.new_revision)